From 4275d321e0a0d313ce8ba7147101d20085724fc2 Mon Sep 17 00:00:00 2001 From: kaiwalyakoparkar Date: Sat, 4 Sep 2021 15:17:20 +0530 Subject: [PATCH] feat: :rocket: Post request handled --- controllers/operationsController.js | 20 ++++ dev-data/data/feed.json | 168 +--------------------------- index.js | 1 + routes/operationsRoute.js | 5 +- 4 files changed, 26 insertions(+), 168 deletions(-) diff --git a/controllers/operationsController.js b/controllers/operationsController.js index e053e73..242db91 100644 --- a/controllers/operationsController.js +++ b/controllers/operationsController.js @@ -1,4 +1,6 @@ +const path = require('path'); const feed = require('../dev-data/data/feed.json'); +const fs = require('fs'); exports.getFeed = (req, res) => { res.status(200).json({ @@ -9,3 +11,21 @@ exports.getFeed = (req, res) => { }, }); }; + +exports.createFeed = (req, res) => { + const newFeed = req.body; + feed.push(newFeed); + + fs.writeFile( + path.join(__dirname, '../dev-data/data/feed.json'), + JSON.stringify(feed), + (err) => { + res.status(201).json({ + status: 'success', + data: { + feed, + }, + }); + } + ); +}; diff --git a/dev-data/data/feed.json b/dev-data/data/feed.json index 5e2b66a..c41301b 100644 --- a/dev-data/data/feed.json +++ b/dev-data/data/feed.json @@ -1,167 +1 @@ -[ - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - }, - { - "profilePicture": "https://i.imgur.com/R4ikJd1.jpg", - "username": "Andreas Brixen", - "posts": [ - { - "postImg": "https://i.imgur.com/l8rN74g.png", - "caption": "A good old book." - } - ], - "points": 80 - } -] +[{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"https://i.imgur.com/R4ikJd1.jpg","username":"Andreas Brixen","posts":[{"postImg":"https://i.imgur.com/l8rN74g.png","caption":"A good old book."}],"points":80},{"profilePicture":"trial","username":"trial trial","posts":[{"postImg":"trial","caption":"trial."}],"points":80}] \ No newline at end of file diff --git a/index.js b/index.js index 28bb23e..8e26695 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ require('dotenv').config(); const morgan = require('morgan'); const app = express(); +app.use(express.json()); if (process.env.DEV_ENVIRONMENT === 'development') { app.use(morgan('dev')); diff --git a/routes/operationsRoute.js b/routes/operationsRoute.js index 08dbbc6..50820a6 100644 --- a/routes/operationsRoute.js +++ b/routes/operationsRoute.js @@ -3,6 +3,9 @@ const route = express.Router(); const operationsController = require('../controllers/operationsController.js'); -route.route('/').get(operationsController.getFeed); +route + .route('/') + .get(operationsController.getFeed) + .post(operationsController.createFeed); module.exports = route;