-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweapon.routes.ts
48 lines (42 loc) · 1.47 KB
/
weapon.routes.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
import { Router } from "express";
import { weapons } from "../controllers";
import { WeaponCreationAttributes } from "../@types/weapon.types";
const router = Router()
router.get('/', async (req, res, next) => {
const allWeapons = await weapons.findAll()
if(!allWeapons.length){
return res.status(204).send()
}
return res.status(200).json(allWeapons)
})
router.get('/:id', async (req, res, next) => {
const { id } = req.params
try {
const weapon = await weapons.findById(+id)
res.status(200).json(weapon)
} catch (error) {
console.error(error)
res.status(404).send(`weapon with id ${id} not found`)
}
})
router.post('/', async (req, res, next) => {
const weapon: WeaponCreationAttributes = req.body
try {
const createdWeapon = await weapons.create(weapon)
res.status(201).json(createdWeapon)
} catch(error) {
console.error(error)
res.status(500).send(`error creating weapon ${JSON.stringify(weapon)}`)
}
})
router.post('/inventory', async (req, res, next) => {
const { id, inventoryId } = req.body as {id: number, inventoryId: number}
try {
const updatedWeapon = await weapons.addToInventory(id, inventoryId)
res.status(200).json(updatedWeapon)
} catch (error){
console.error(error)
res.status(500).send(`error adding weapon ${id} to inventory ${inventoryId}`)
}
})
export default router