-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.controller.ts
41 lines (32 loc) · 1.04 KB
/
inventory.controller.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
import { InventoryAttributes, InventoryController, InventoryCreationAttributes } from "../@types/inventory.types";
import { Inventory, Weapon } from "../models";
const findAll = async () => {
return Inventory.findAll({include: Weapon})
}
const create = async (inventory: InventoryCreationAttributes) => {
return Inventory.create(inventory)
}
const findById = async (id: number) => {
const inventory = await Inventory.findOne({
where: { id },
include: Weapon
})
if(!inventory){
throw new Error(`Inventory id ${id} not found!`)
}
return inventory
}
const update = async (inventory: InventoryAttributes) => {
const existingInventory = await Inventory.findOne({where: {id: inventory.id}})
if(!existingInventory){
throw new Error(`Inventory id ${inventory.id} not found!`)
}
return await existingInventory.update(inventory)
}
const inventory: InventoryController = {
findAll,
update,
create,
findById
}
export { inventory }