forked from FullstackAcademy/acme-item-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
133 lines (116 loc) · 2.87 KB
/
server.js
File metadata and controls
133 lines (116 loc) · 2.87 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const { conn, User, Thing } = require('./db');
const express = require('express');
const app = express();
const path = require('path');
app.use(express.json());
app.use('/dist', express.static('dist'));
app.get('/', (req, res)=> res.sendFile(path.join(__dirname, 'index.html')));
app.get('/api/things', async(req, res, next)=> {
try {
res.send(await Thing.findAll());
}
catch(ex){
next(ex);
}
});
app.post('/api/things', async(req, res, next)=> {
try {
res.status(201).send(await Thing.create(req.body));
}
catch(ex){
next(ex);
}
});
app.put('/api/things/rankDown', async(req, res, next)=> {
try {
const thing = await Thing.findByPk(req.body.thingId);
await thing.increment({ rank: 1 }); // higher rank number is a lower rank
res.status(200).send(thing);
}
catch(ex){
console.error(ex);
next(ex);
}
});
app.put('/api/things/rankUp', async(req, res, next)=> {
try {
const thing = await Thing.findByPk(req.body.thingId);
await thing.decrement({ rank: 1 }); // lower rank number is a higher rank
res.status(200).send(thing);
}
catch(ex){
next(ex);
}
});
app.put('/api/things/user', async(req, res, next) => {
try {
const thing = await Thing.findByPk(req.body.thingId);
const oldOwnerId = thing.userId;
const newOwner = await User.findByPk(req.body.newOwnerId);
await thing.setUser(newOwner);
res.send({
thingId: thing.id,
oldOwnerId: oldOwnerId,
newOwnerId: thing.userId,
});
}
catch(ex) {
next(ex)
}
})
app.delete('/api/things/:id', async(req, res, next)=> {
try {
await Thing.destroy({ where: { id: req.params.id } });
res.sendStatus(204);
}
catch(ex){
next(ex);
}
});
app.get('/api/users', async(req, res, next)=> {
try {
res.send(await User.findAll());
}
catch(ex){
next(ex);
}
});
app.post('/api/users', async(req, res, next)=> {
try {
res.status(201).send(await User.create(req.body));
}
catch(ex) {
next(ex);
}
})
app.delete('/api/users/:id', async(req, res, next)=> {
try {
await User.destroy({ where: { id: req.params.id } });
res.sendStatus(204);
}
catch(ex){
next(ex);
}
});
const port = process.env.PORT || 3000;
app.listen(port, ()=> console.log(`listening on port ${port}`));
const init = async()=> {
try {
await conn.sync({ force: true });
const [moe, larry, lucy, ethyl] = await Promise.all(
['moe', 'larry', 'lucy', 'ethyl'].map( name => User.create({ name }))
);
// initially, rank is arbitrarily based on order created
const [foo, bar, bazz, quq, fizz] = await Promise.all(
['foo', 'bar', 'bazz', 'quq', 'fizz'].map((name, index) => Thing.create({ name, rank: ++index }))
);
await moe.addThings([foo, bar]);
await larry.addThing(bazz);
await lucy.addThing(quq);
await ethyl.addThing(fizz);
}
catch(ex){
console.log(ex);
}
};
init();