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 d0a75d4
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 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 Down Expand Up @@ -51,4 +52,26 @@ feeds.get('/:id', async (req, res) => {
}
});

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

module.exports = feeds;

0 comments on commit d0a75d4

Please sign in to comment.