CSE316/hw3/server/controllers/top5list-controller.js

151 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2022-05-23 06:22:34 -04:00
const Top5List = require('../models/top5list-model')
/*
This is our back-end API. It provides all the data services
our database needs. Note that this file contains the controller
functions for each endpoint.
@author McKilla Gorilla
*/
createTop5List = (req, res) => {
const body = req.body;
console.log("createTop5List body: " + body);
if (!body) {
return res.status(400).json({
success: false,
error: 'You must provide a Top 5 List',
})
}
const top5List = new Top5List(body);
console.log("top5List: " + JSON.stringify(body));
if (!top5List) {
return res.status(400).json({ success: false, error: err })
}
top5List
.save()
.then(() => {
return res.status(201).json({
success: true,
top5List: top5List,
message: 'Top 5 List Created!',
})
})
.catch(error => {
return res.status(400).json({
error,
message: 'Top 5 List Not Created!',
})
})
}
updateTop5List = async (req, res) => {
const body = req.body
console.log("updateTop5List: " + JSON.stringify(body));
if (!body) {
return res.status(400).json({
success: false,
error: 'You must provide a body to update',
})
}
Top5List.findOne({ _id: req.params.id }, (err, top5List) => {
if (err) {
return res.status(404).json({
err,
message: 'Top 5 List not found!',
})
}
top5List.name = body.name
top5List.items = body.items
top5List
.save()
.then(() => {
console.log("SUCCESS!!!");
return res.status(200).json({
success: true,
id: top5List._id,
message: 'Top 5 List updated!',
})
})
.catch(error => {
console.log("FAILURE: " + JSON.stringify(error));
return res.status(404).json({
error,
message: 'Top 5 List not updated!',
})
})
})
}
deleteTop5List = async (req, res) => {
await Top5List.findOneAndDelete({ _id: req.params.id }, (err, top5List) => {
if (err) {
return res.status(400).json({ success: false, error: err })
}
if (res === null) {
return res
.status(404)
.json({ success: false, error: `Top 5 List not found` })
}
return res.status(200).json({ success: true, data: top5List })
}).catch(err => console.log(err))
}
getTop5ListById = async (req, res) => {
await Top5List.findOne({ _id: req.params.id }, (err, list) => {
if (err) {
return res.status(400).json({ success: false, error: err })
}
return res.status(200).json({ success: true, top5List: list })
}).catch(err => console.log(err))
}
getTop5Lists = async (req, res) => {
await Top5List.find({}, (err, top5Lists) => {
if (err) {
return res.status(400).json({ success: false, error: err })
}
if (!top5Lists.length) {
return res
.status(404)
.json({ success: false, error: `Top 5 Lists not found` })
}
return res.status(200).json({ success: true, data: top5Lists })
}).catch(err => console.log(err))
}
getTop5ListPairs = async (req, res) => {
await Top5List.find({}, (err, top5Lists) => {
if (err) {
return res.status(400).json({ success: false, error: err})
}
if (!top5Lists.length) {
return res
.status(404)
.json({ success: false, error: 'Top 5 Lists not found'})
}
else {
// PUT ALL THE LISTS INTO ID, NAME PAIRS
let pairs = [];
for (let key in top5Lists) {
let list = top5Lists[key];
let pair = {
_id : list._id,
name : list.name
};
pairs.push(pair);
}
return res.status(200).json({ success: true, idNamePairs: pairs })
}
}).catch(err => console.log(err))
}
module.exports = {
createTop5List,
updateTop5List,
deleteTop5List,
getTop5Lists,
getTop5ListPairs,
getTop5ListById
}