Skip to content

Commit

Permalink
fix Seneca-CDOT#707 adding post route.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grommers00 committed Feb 16, 2020
1 parent 8b5bc0e commit c134f24
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/backend/web/routes/feeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const Feed = require('../../data/feed');
const { getFeeds } = require('../../utils/storage');
const { logger } = require('../../utils/logger');
const { protect } = require('../authentication');

const feeds = express.Router();

Expand All @@ -10,8 +11,8 @@ feeds.get('/', async (req, res) => {

try {
ids = await getFeeds();
} catch (err) {
logger.error({ err }, 'Unable to get feeds from Redis');
} catch (error) {
logger.error({ error }, 'Unable to get feeds from Redis');
res.status(503).json({
message: 'Unable to connect to database',
});
Expand Down Expand Up @@ -43,12 +44,34 @@ feeds.get('/:id', async (req, res) => {
} else {
res.json(feed);
}
} catch (err) {
logger.error({ err }, 'Unable to get feeds from Redis');
} catch (error) {
logger.error({ error }, 'Unable to get feeds from Redis');
res.status(503).json({
message: 'Unable to connect to database',
});
}
});

feeds.post('/', protect, async (req, res) => {
const feedData = req.body;

try {
if (!(feedData.url && feedData.author)) {
return res.status(400).json({ message: `URL and Author must be submitted` });
}
if (await Feed.byUrl(feedData.url)) {
return res.status(409).json({ message: `Feed for url ${feedData.url} already exists.` });
}
const feedId = await Feed.create(feedData);
return res
.status(201)
.json({ message: `Feed was successfully added.`, id: feedId, url: `/feeds/${feedId}` });
} catch (error) {
logger.error({ error }, 'Unable to add feed to Redis');
return res.status(503).json({
message: 'Unable to add to Feed',
});
}
});

module.exports = feeds;

0 comments on commit c134f24

Please sign in to comment.