Skip to content

Instantly share code, notes, and snippets.

@catalinpit
Last active April 27, 2021 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catalinpit/191cbb0db15a52121e28461b31bc0a35 to your computer and use it in GitHub Desktop.
Save catalinpit/191cbb0db15a52121e28461b31bc0a35 to your computer and use it in GitHub Desktop.
The controllers for the routes
const client = require("../db/db_config");
const getCourses = async (req, res) => {
const allCourses = await client.query('SELECT * FROM mydb.courses');
res.send({ allCourses });
};
const getSpecificCourse = async (req, res) => {
const course = await client.query(`SELECT * FROM mydb.courses WHERE id="${req.params.id}"`);
res.send({ course });
};
const addCourse = async (req, res) => {
const allProperties = Object.keys(req.body).length;
if (allProperties !== 4) {
res.code(400).send({
error: 'Some properties are missing! Add the name, description, author and link!'
});
return;
}
try {
const newCourse = await client.insert({
table: 'courses',
records: [
{
name: req.body.name,
description: req.body.description,
author: req.body.author,
link: req.body.link
}
]
});
res.send({ newCourse });
} catch (error) {
res.send({ error });
}
};
const editCourse = async (req, res) => {
const updates = Object.keys(req.body);
const allowedUpdates = ['name', 'description', 'author', 'link'];
const isValidOperation = updates.every((update) => allowedUpdates.includes(update));
if (!isValidOperation) {
res.code(400).send({ error: 'Not a valid operation! '});
return;
}
try {
const updatedCourse = await client.update({
table: 'courses',
records: [
{
id: req.params.id,
name: req.body.name,
description: req.body.description,
author: req.body.author,
link: req.body.link
}
]
});
res.send({ updatedCourse });
} catch (error) {
res.send({ error });
}
};
const deleteCourse = async (req, res) => {
const course = await client.query(`DELETE FROM mydb.courses WHERE id="${req.params.id}"`);
res.send({
message: 'Course deleted!',
deleteCourse: course
});
};
module.exports = {
getCourses,
getSpecificCourse,
addCourse,
editCourse,
deleteCourse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment