theater.controller.js 2.19 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
import { Theater, TheaterType } from "../db/index.js";
Kim, Subin's avatar
theater    
Kim, Subin committed
2
3
4

const getAll = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
5
        const findList = await Theater.findAll({ attributes: { exclude: ['createdAt', 'updatedAt'] }, include: [ TheaterType ], order: [['theaterName']] })
Kim, Subin's avatar
theater    
Kim, Subin committed
6
7
8
9
10
11
        return res.json(findList)
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 가져오는 중 에러 발생")
    }
}

Kim, Subin's avatar
Kim, Subin committed
12
13
14
15
16
17
18
19
20
21
22
const getOne = async (req, res) => {
    try {
        const { theaterId } = req.params
        const find = await Theater.findOne({ where: { id: theaterId } , attributes: { exclude: ['createdAt', 'updatedAt'] } })
        if (!find) throw new Error("해당 정보를 찾지 못했습니다.");
        return res.json(find)
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 가져오는 중 에러 발생")
    }
}

Kim, Subin's avatar
theater    
Kim, Subin committed
23
24
const getTypes = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
25
        const findTypes = await TheaterType.findAll({ attributes: { exclude: ['createdAt', 'updatedAt'] } })
Kim, Subin's avatar
theater    
Kim, Subin committed
26
27
28
29
30
31
32
33
        return res.json(findTypes)
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 가져오는 중 에러 발생")
    }
}

const submit = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
34
        const { id, theatertypeId, theaterName, rows, columns } = req.body
Kim, Subin's avatar
theater    
Kim, Subin committed
35
        let response = null
Kim, Subin's avatar
Kim, Subin committed
36
37
        if (id) response = await Theater.update({ theatertypeId, theaterName, rows, columns }, { where: { id: id } })
        else response = await Theater.create({ theatertypeId, theaterName, rows, columns })
Kim, Subin's avatar
theater    
Kim, Subin committed
38
39
40
41
42
43
44
45
        return res.json(response)
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 저장 중 에러 발생")
    }
}

const remove = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
46
47
48
49
        const { theaterId } = req.params
        const delNum = await Theater.destroy({ where: { id: theaterId } })
        if (delNum) res.json(delNum)
        else throw new Error("해당 정보를 서버에서 삭제하는데 실패했습니다.");
Kim, Subin's avatar
theater    
Kim, Subin committed
50
51
52
53
54
55
56
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 삭제 중 에러 발생")
    }
}

export default {
    getAll,
Kim, Subin's avatar
Kim, Subin committed
57
    getOne,
Kim, Subin's avatar
theater    
Kim, Subin committed
58
59
60
61
    getTypes,
    submit,
    remove
}