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

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

const getOne = async (req, res) => {
    try {
14
15
16
        const { theaterTypeId } = req.params
        const find = await TicketFee.findOne({ where: { theatertypeId: theaterTypeId }, attributes: { exclude: ['createdAt', 'updatedAt'] }, include: [ TheaterType ] })
        find.dataValues.theaterTypeName = find.dataValues.theatertype.dataValues.theaterTypeName
Kim, Subin's avatar
Kim, Subin committed
17
18
19
20
21
22
23
24
25
        if (!find) throw new Error("해당 정보를 찾지 못했습니다.");
        return res.json(find)
    } catch (error) {
        return res.status(500).send(error.message || "관람료 정보 가져오는 중 에러 발생")
    }
}

const edit = async (req, res) => {
    try {
26
        const { theatertypeId, theaterTypeName, defaultPrice, weekdays, weekend, morning, day, night, youth, adult, senior } = req.body
Kim, Subin's avatar
Kim, Subin committed
27
        let response = null
28
29
30
        const result = await TheaterType.findOrCreate({
            where: { id: theatertypeId },
            defaults: { theaterTypeName: theaterTypeName }
Kim, Subin's avatar
Kim, Subin committed
31
        })
32
33
34
35
36
37
        if (result[1]) {
            response = await TicketFee.create({ theatertypeId: result[0].id, defaultPrice, weekdays, weekend, morning, day, night, youth, adult, senior })
        } else {
            await TheaterType.update({ theaterTypeName: theaterTypeName }, { where: { id: theatertypeId } })
            response = await TicketFee.update({ defaultPrice, weekdays, weekend, morning, day, night, youth, adult, senior }, { where: { theatertypeId: result[0].id } })
        }
Kim, Subin's avatar
Kim, Subin committed
38
39
        return res.json(response)
    } catch (error) {
40
        return res.status(500).send(error.message || "관람료 정보 추가 및 수정 중 에러 발생")
Kim, Subin's avatar
Kim, Subin committed
41
42
43
44
45
    }
}

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

export default {
    getAll,
    getOne,
    edit,
    remove
}