Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stephanie/backend #33

Merged
merged 6 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
38 changes: 31 additions & 7 deletions server/controllers/feedController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ feedController.getFeed = (req, res, next) => {
db.query(text)
.then(dbResults => {
// store on res.locals
console.log(dbResults)
// console.log(dbResults.rows)
res.locals.feed = dbResults.rows
next()
}) .catch((err) => {
return next({
Expand All @@ -26,22 +27,33 @@ feedController.postGive = (req, res, next) => {
detail,
quantity,
location,
contact
contact,
} = req.body;

const text = 'INSTERT INTO feed (material, detail, quantity,location, contact) VALUES ($1, $2, $3, $4, $5'
// js date program
let post_date = new Date();
let dd = post_date.getDate();
let mm = post_date.getMonth()+1;
const yyyy = post_date.getFullYear();
if(dd<10) { dd=`0${dd}`}
if(mm<10) { mm=`0${mm}`}
post_date = `${mm}-${dd}-${yyyy}`;


const text = 'INSERT INTO feed (material, detail, quantity, location, contact, post_date) VALUES ($1, $2, $3, $4, $5, $6);'
const values = [
material,
detail,
quantity,
location,
contact
contact,
post_date
];

db.query(text, values)
.then(dbResults => {
// store on res.locals
console.log(dbResults)
res.locals.confirmation = dbResults
// console.log(dbResults)
next()
}).catch((err) => {
return next({
Expand All @@ -51,8 +63,20 @@ feedController.postGive = (req, res, next) => {
})
};

// delete entry from DB via params
feedController.fulfilledGive = (req,res, next) => {
const { givenItem } = req.params

const text = `DELETE FROM feed WHERE feed_id = ${givenItem};`;

db.query(text)
.then(next())
.catch((err) => {
return next({
log: `feedController.fulfilledGive: ERROR: ${err}`,
message: {err: 'Error occured in feedController.fulfilledGive'}
})
})
};

module.exports = controller;
module.exports = feedController;
25 changes: 15 additions & 10 deletions server/routes/feedRouter.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
const express = require('express');
const feedController = ('../controllers/feedController')
const feedController = ('../controllers/feedController');
const feedRouter = express.Router();

/** Get from Database
* @Route GET feed/
* @Desc Gets the feedItems from database to populate the Feed.jsx
* @Access Public
*/
feedRouter.get('/',
// feedController.getFeed,
(req,res) => res.status(200).json('something'))
// feedRouter.get('/',
// feedController.getFeed,
// (req, res) => res.status(200).json(res.locals.feed)
// );

/** Insert into Database
* @Route POST feed/give
* @Desc Inserts data from frontend form into database
* @Desc Inserts data from frontend form into database & returns the
* @Access Public
*/
feedRouter.post('/give',
// feedController.postGive,
(req,res) => res.status(200).json('something from the giveController'))
feedController.postGive,
feedController.getFeed,
(req,res) => {
res.status(200).json(res.locals.feed)
}
);

/** Delete from Database
* @Route Delete feed/delete/:id
* @Desc Delete data from database by id
* @Access Private
*/
feedRouter.delete('/delete',
// feedController.fulfilledGive,
(req,res) => res.status(200).json('something from the giveDele/te'))
feedRouter.delete('/delete/:id',
feedController.fulfilledGive,
(req,res) => res.status(200).json('something from the giveDelete'))

module.exports = feedRouter;
4 changes: 2 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const app = express();
const feedRouter = require('./routes/feedRouter')
const feedRouter = require('./routes/feedRouter');
const PORT = 3000;

// parses objects or strings from client (Req.body)
Expand All @@ -11,7 +11,7 @@ app.use(express.json());
// all incoming feed requests go to router
app.use('/feed', feedRouter)

//unkown route handler
// unknown route handler
app.use((req, res) => res.sendStatus(404));

// global error handler
Expand Down