/
person-router.js
55 lines (39 loc) · 1.5 KB
/
person-router.js
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
import { Router } from 'express'
import { personRepository } from '../om/person.js'
export const router = Router()
router.put('/', async (req, res) => {
/* create the Person from the request body which just
*happens* to match our Schema */
const person = await personRepository.createAndSave(req.body)
/* return the newly created Person */
res.send(person)
})
router.get('/:id', async (req, res) => {
/* fetch the Person */
const person = await personRepository.fetch(req.params.id)
/* return the fetched Person */
res.send(person)
})
router.post('/:id', async (req, res) => {
/* fetch the Person we are updating */
const person = await personRepository.fetch(req.params.id)
/* set all the properties, converting missing properties to null */
person.firstName = req.body.firstName ?? null
person.lastName = req.body.lastName ?? null
person.age = req.body.age ?? null
person.verified = req.body.verified ?? null
person.location = req.body.location ?? null
person.locationUpdated = req.body.locationUpdated ?? null
person.skills = req.body.skills ?? null
person.personalStatement = req.body.personalStatement ?? null
/* save the updated Person */
await personRepository.save(person)
/* return the newly updated Person we just updated */
res.send(person)
})
router.delete('/:id', async (req, res) => {
/* delete the Person with its id */
await personRepository.remove(req.params.id)
/* return the id of the deleted person */
res.send({ id: req.params.id })
})