-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
96 lines (73 loc) · 1.94 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import bodyParser from 'body-parser';
import express from "express";
import { PrismaClient } from '@prisma/client'
import path from "path";
const app = express();
const port = 3000; // default port to listen
app.use(bodyParser.json());
const prisma = new PrismaClient();
app.post('/api/stores', async (req, res) => {
const { body } = req;
const dataToAdd = {
storename: body.storename,
location: body.location,
coordinates: body.coordinates,
date: new Date(Number(body.date)),
amount: body.queue,
Item: {
create: body.items,
},
};
const store = await prisma.store.create({ data: dataToAdd});
res.status(200).json(store);
});
app.get('/api/items', (req, res) => {
prisma.item.findMany().then(results => {
res.status(200).json({ results });
})
})
app.get('/api/stores', (req, res) => {
const {
query: {missing, date },
method,
} = req
const fulldate = new Date(Number(date));
prisma.store.findMany(
{
include: {
StoresOnItems: true,
Item: true
},
where:
{
date: fulldate,
Item: {
some:
{ item: missing as string }
}
}
}).then(results => {
res.status(200).json({ results });
})
})
app.put('/api/stores/:id', async (req, res) => {
const reqid = req.params.id;
const storename= req.body.storename;
console.log(reqid);
console.log(storename);
const updatedStore = await prisma.store.update({
data: { storename: storename },
where: { id: Number(reqid) },
})
res.status(200).json({ updatedStore });
})
app.delete('/api/stores/:id', async (req, res) => {
const reqid = req.params.id;
const deletedStore = await prisma.store.delete({
where: { id: Number(reqid) },
});
res.status(200).json({ deletedStore });
})
app.listen(port, function () {
console.log(`Example app listening on port ${process.env.PORT}!`);
});