reorg
This commit is contained in:
parent
c8d58db08a
commit
6af9d8619f
6 changed files with 4 additions and 4 deletions
|
@ -1,63 +0,0 @@
|
|||
const express = require("express");
|
||||
const db = require("./db");
|
||||
const port = 6063;
|
||||
const cors = require('cors'); // to remove cors origin error in dev TODO: remove when dockerized
|
||||
|
||||
const app = express();
|
||||
app.use(cors()); // to remove cors origin error in dev TODO: remove when dockerized
|
||||
app.use(express.json());
|
||||
|
||||
// routes
|
||||
app.get("/recipes", async (req, res) => {
|
||||
try {
|
||||
const recipes = await db('recipes').select('id', 'name');
|
||||
res.json(recipes);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/recipe/:id", async (req, res) => {
|
||||
const id = req.params.id
|
||||
try {
|
||||
const recipe = await db('recipes').where('id', '=', id).select('id', 'name', 'cuisine').first();
|
||||
res.json(recipe);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/add-recipe", async (req, res) => {
|
||||
const { name, cuisine } = req.body;
|
||||
try {
|
||||
const [id] = await db('recipes').insert({
|
||||
name: name,
|
||||
cuisine: cuisine
|
||||
}, ['id'])
|
||||
res.status(200).send({ message: "Recipe added", id: id.id });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.delete("/delete-recipe", async (req, res) => {
|
||||
const { id } = req.body;
|
||||
try {
|
||||
await db('recipes').where({ id: id }).del();
|
||||
res.status(200).send({ message: "Recipe deleted" });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => console.log(`Server has started on port: ${port}`));
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Closing database connection...');
|
||||
await db.destroy();
|
||||
process.exit(0);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue