-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.controller.ts
53 lines (42 loc) · 1.32 KB
/
character.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
42
43
44
45
46
47
48
49
50
51
52
53
import { CharacterAttributes, CharacterController, CharacterCreationAttributes } from "../@types/character.types";
import { Inventory, Weapon } from "../models";
import { Character } from "../models/character.model";
const create = async(character: CharacterCreationAttributes) => {
return await Character.create(character)
}
const update = async (character: CharacterAttributes) => {
const existingCharacter = await Character.findOne({where: {id: character.id}})
if(!existingCharacter){
throw new Error(`Character id ${character.id} not found!`)
}
return await existingCharacter.update(character)
}
const findById = async (id: number) => {
const character = await Character.findOne({
where: { id },
include: [{
model: Inventory,
include: [ Weapon ]
}]
})
if(!character){
throw new Error(`Character id ${id} not found!`)
}
return character
}
const findAll = async () => {
const allCharacters = await Character.findAll({
include: {
model: Inventory,
include: [ Weapon ]
}
})
return allCharacters
}
const characters: CharacterController = {
create,
update,
findById,
findAll
}
export { characters }