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

const getTheaterInfo = async (req, res) => {
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
5
        const { theaterId } = req.body
6
        const theaterInfo = await Theater.findOne({
Jiwon Yoon's avatar
Jiwon Yoon committed
7
            where: { id: theaterId },
Jiwon Yoon's avatar
Jiwon Yoon committed
8
            attributes: ['theaterName', 'rows', 'columns']
9
10
        })
        return res.json(theaterInfo)
한규민's avatar
한규민 committed
11
    } catch (error) {
Jiwon Yoon's avatar
Jiwon Yoon committed
12
13
14
        console.log(error)
    }
}
Kim, Subin's avatar
theater    
Kim, Subin committed
15
16
const getAll = async (req, res) => {
    try {
17
        const findList = await Theater.findAll({ attributes: { exclude: ['createdAt', 'updatedAt'] }, include: [TheaterType], order: [['theaterName']] })
Kim, Subin's avatar
theater    
Kim, Subin committed
18
        return res.json(findList)
19
20
21
22
23
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 가져오는 중 에러 발생")
    }
}

Kim, Subin's avatar
Kim, Subin committed
24
25
26
const getOne = async (req, res) => {
    try {
        const { theaterId } = req.params
27
        const find = await Theater.findOne({ where: { id: theaterId }, attributes: { exclude: ['createdAt', 'updatedAt'] } })
Kim, Subin's avatar
Kim, Subin committed
28
29
30
31
32
33
34
        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
35
36
const getTypes = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
37
        const findTypes = await TheaterType.findAll({ attributes: { exclude: ['createdAt', 'updatedAt'] } })
Kim, Subin's avatar
theater    
Kim, Subin committed
38
39
40
41
42
43
        return res.json(findTypes)
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 가져오는 중 에러 발생")
    }
}

한규민's avatar
한규민 committed
44
const getTheater = async (req, res) => {
한규민's avatar
한규민 committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    try {
        const reservation = req.reservation;
        const theaterId = reservation.map(movie => movie.theaterId);
        const elements = await Promise.all(
            theaterId.map(async (theaterId) => {
                const theater = await Theater.findOne({ where: { id: theaterId } });
                const theaterData = {
                    theaterId: theater.id,
                    theaterName: theater.theaterName,
                    theatertypeId: theater.theatertypeId
                }
                return theaterData
            })
        );
        reservation.map(reservation => {
            const theaterId = elements.find(el => reservation.theaterId === el.theaterId);
            reservation.dataValues = {
                ...reservation.dataValues,
                theaterName: theaterId.theaterName,
                theatertypeId: theaterId.theatertypeId
            }
        })
        res.json(reservation);
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 불러오기 실패")
    }
}
한규민's avatar
한규민 committed
72

Kim, Subin's avatar
theater    
Kim, Subin committed
73
74
const submit = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
75
        const { id, theatertypeId, theaterName, rows, columns } = req.body
Kim, Subin's avatar
theater    
Kim, Subin committed
76
        let response = null
Kim, Subin's avatar
Kim, Subin committed
77
        if (id) response = await Theater.update({ theatertypeId, theaterName, rows, columns }, { where: { id: id } })
78
79
80
81
82
83
84
85
        else {
            const result = await Theater.findOrCreate({
                where: { theaterName: theaterName },
                defaults: { theatertypeId, theaterName, rows, columns }
            })
            if (!result[1]) throw new Error("이미 존재하는 이름의 상영관입니다. 다시 등록해주세요.");
            else response = result[0]
        }
Kim, Subin's avatar
theater    
Kim, Subin committed
86
87
88
89
90
91
92
93
        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
94
95
96
97
        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
98
99
100
101
102
103
104
    } catch (error) {
        return res.status(500).send(error.message || "상영관 정보 삭제 중 에러 발생")
    }
}

export default {
    getAll,
Kim, Subin's avatar
Kim, Subin committed
105
    getOne,
Kim, Subin's avatar
theater    
Kim, Subin committed
106
    getTypes,
한규민's avatar
한규민 committed
107
    getTheater,
Kim, Subin's avatar
theater    
Kim, Subin committed
108
    submit,
Jiwon Yoon's avatar
Jiwon Yoon committed
109
110
111
    remove,
    getTheaterInfo
}