Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit be5baa2

Browse files
Mark MavromatisMark Mavromatis
Mark Mavromatis
authored and
Mark Mavromatis
committed
Implemented feed router services (Get/Patch)
1 parent 8258749 commit be5baa2

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

course-02/exercises/udacity-c2-restapi/src/aws.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { config } from './config/config';
44
const c = config.dev;
55

66
//Configure AWS
7-
var credentials = new AWS.SharedIniFileCredentials({profile: 'default'});
7+
var credentials = new AWS.SharedIniFileCredentials({profile: c.aws_profile});
88
AWS.config.credentials = credentials;
99

1010
export const s3 = new AWS.S3({

course-02/exercises/udacity-c2-restapi/src/controllers/v0/feed/routes/feed.router.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,55 @@ router.get('/', async (req: Request, res: Response) => {
1818

1919
//@TODO
2020
//Add an endpoint to GET a specific resource by Primary Key
21+
router.get('/:id', async (req: Request, res: Response) => {
22+
let { id } = req.params;
23+
if (!id) {
24+
return res.status(400)
25+
.send(`id is required`);
26+
}
27+
28+
const foundImage = await FeedItem.findByPk(id);
29+
if (!foundImage) {
30+
return res.status(404)
31+
.send(`No image found with ID: ${id}`);
32+
}
33+
34+
res.status(200).send(foundImage);
35+
});
2136

2237
// update a specific resource
2338
router.patch('/:id',
2439
requireAuth,
2540
async (req: Request, res: Response) => {
26-
//@TODO try it yourself
27-
res.send(500).send("not implemented")
28-
});
41+
let { id } = req.params;
42+
let { caption, url } = req.body;
43+
44+
if (!caption && !url) {
45+
return res.status(400).send(`caption or url is required in the request body`)
46+
}
47+
48+
if (!id) {
49+
return res.status(400)
50+
.send(`id is required`);
51+
}
52+
53+
const foundImage = await FeedItem.findByPk(id);
54+
if (!foundImage) {
55+
return res.status(404)
56+
.send(`No image found with ID: ${id}`);
57+
}
58+
59+
if (caption) {
60+
foundImage.caption = caption;
61+
}
62+
if (url) {
63+
foundImage.url = url;
64+
}
65+
foundImage.save()
66+
67+
res.status(200).send(foundImage);
68+
69+
});
2970

3071

3172
// Get a signed url to put a new item in the bucket

0 commit comments

Comments
 (0)