-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.ts
67 lines (54 loc) · 2 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { createClient } from '@libsql/client';
import { v4 as uuidV4 } from 'uuid';
import { Ingredient, Recipe } from 'src/components/models';
const PORT = 3000;
const db = createClient({
url: 'file:recipe-book.db',
});
const app = express();
app.use(bodyParser.json());
app.use(cors());
app.get('/recipes', async (req, res) => {
const results = await db.execute(
"select recipes.id, recipes.name, recipes.nutrition_information as nutritionInformation, recipes.instructions, recipes.created_at as createdAt, recipes.updated_at as updatedAt, json_group_array(json_object('id', ingredients.id, 'name', ingredients.name, 'measurements', ingredients.measurements)) as ingredients from recipes join ingredients on ingredients.recipe_id = recipes.id group by recipes.id"
);
res.json({ recipes: results.rows });
});
app.post('/recipe', async (req, res) => {
const { recipe, ingredients } = (await req.body) as unknown as {
recipe: Recipe;
ingredients: Ingredient[];
};
const recipeId = uuidV4();
await db.execute({
sql: 'insert into recipes(id, name, nutrition_information, instructions) values (?, ?, ?, ?)',
args: [
recipeId,
recipe.name,
recipe.nutritionInformation as string,
recipe.instructions as string,
],
});
const statements = ingredients?.map((ingredient) => ({
sql: 'insert into ingredients(id, name, measurements, recipe_id) values (?, ?, ?, ?)',
args: [uuidV4(), ingredient.name, ingredient.measurements, recipeId],
}));
await db.batch(statements, 'write');
res.json({ ok: true });
});
app.delete('/recipe/:id', async (req, res) => {
const { id } = req.params;
await db.execute({
sql: 'delete from ingredients where recipe_id = ?',
args: [id],
});
await db.execute({
sql: 'delete from recipes where id = ?',
args: [id],
});
res.json({ ok: true });
});
app.listen(PORT, () => console.log(`App running at port ${PORT}`));