reservation.controller.js 3.82 KB
Newer Older
한규민's avatar
한규민 committed
1
import jwt from "jsonwebtoken";
한규민's avatar
한규민 committed
2
import { Movie, Reservation, Theater, TheaterType, TimeTable } from '../db/index.js'
한규민's avatar
한규민 committed
3
import config from '../config/app.config.js'
4

5
const findReservedSeats = async (req, res) => {
6
    try {
Jiwon Yoon's avatar
Jiwon Yoon committed
7
        const { timeTable } = req.body
8
9
        const reservedSeats = await Reservation.findAll({
            where: {
Jiwon Yoon's avatar
Jiwon Yoon committed
10
                timetableId: timeTable
11
12
13
14
            }
        })
        res.json(reservedSeats)
    } catch (error) {
Jiwon Yoon's avatar
Jiwon Yoon committed
15
        res.status(500).send(error.message || "이미 예매되어있는 좌석을 찾는 중 오류발생")
16
17
    }
}
18
19
const findReservation = async (req, res) => {
    try {
한규민's avatar
한규민 committed
20
21
        const token = req.cookies.butterStudio;
        const { id } = jwt.verify(token, config.jwtSecret);
22
23
        const reservation = await Reservation.findAll({
            where: {
한규민's avatar
한규민 committed
24
                user: id
한규민's avatar
한규민 committed
25
26
            },
            include: [Theater, TimeTable]
27
28
29
30
31
32
        })
        res.json(reservation)
    } catch (error) {
        res.status(500).send(error.message || "예매 내역들을 찾는 중 오류발생")
    }
}
한규민's avatar
한규민 committed
33
const findOneReservation = async (req, res, next) => {
34
    try {
한규민's avatar
한규민 committed
35
36
        const token = req.cookies.butterStudio;
        const { id, role } = jwt.verify(token, config.jwtSecret);
37
38
        const reservation = await Reservation.findAll({
            where: {
한규민's avatar
한규민 committed
39
40
                user: id,
                userType: role
41
            },
한규민's avatar
한규민 committed
42
            include:[TimeTable, Theater]
한규민's avatar
한규민 committed
43
        });
한규민's avatar
한규민 committed
44
45
        req.reservation = reservation;
        next();
46
47
48
49
    } catch (error) {
        res.status(500).send(error.message || "예매 내역을 찾는 중 오류 발생")
    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
50
51
const saveReservation = async (req, res) => {
    try {
52
        const { movieId, theaterId, timetableId, payment, user, userType, totalFee } = req.body
Jiwon Yoon's avatar
Jiwon Yoon committed
53
54
        const rows = req.body.selectedSeats.map(el => el.split('-')[0])
        const cols = req.body.selectedSeats.map(el => el.split('-')[1])
Jiwon Yoon's avatar
Jiwon Yoon committed
55
56
57
58
59
60
61
        for (let index = 0; index < rows.length; index++) {
            const reservation = await Reservation.create({
                user: user,
                userType: userType,
                movieId: movieId,
                row: rows[index],
                col: cols[index],
Jiwon Yoon's avatar
Jiwon Yoon committed
62
                timetableId: timetableId,
63
                theaterId: theaterId,
Jiwon Yoon's avatar
Jiwon Yoon committed
64
                payment: payment,
Jiwon Yoon's avatar
Jiwon Yoon committed
65
                totalFee: totalFee,
Jiwon Yoon's avatar
Jiwon Yoon committed
66
67
68
69
70
71
72
73
74
            })
        }
        const movie = await Movie.findOne({
            where: {
                movieId: movieId
            }
        })
        movie.ticket_sales++
        await movie.save();
75
        res.json({ message: '200 OK' })
Jiwon Yoon's avatar
Jiwon Yoon committed
76
77
78
79
80
81
    } catch (error) {
        console.log(error)
        res.status(500).send(error.message || "예매DB에 저장 실패")
    }
}

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const saveTid = async (req, res) => {
    try {
        const { tid } = req.body
        const token = req.cookies.butterStudio;
        const { id, role } = jwt.verify(token, config.jwtSecret);
        await Reservation.update({ tid: tid }, {
            where: {
                userType: role,
                user: id
            }
        })
        res.json({ message: 'Tid 저장 OK' })
    } catch (error) {
        console.log(error)
        res.status(500).send(error.message || "예매DB에 Tid 저장 실패")
    }
}

const deleteReservation = async(req,res)=>{
    try {
        const token = req.cookies.butterStudio;
        const { id, role } = jwt.verify(token, config.jwtSecret);
        await Reservation.destroy({
            where: {
                userType: role,
                user: id
            }
한규민's avatar
한규민 committed
109
        });
110
111
112
113
114
115
116
        res.json({ message: '결제실패로 인한 예매DB삭제' })
    } catch (error) {
        console.log(error)
        res.status(500).send(error.message || "예매DB 삭제실패")
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
117
export default {
118
    findReservedSeats,
Jiwon Yoon's avatar
Jiwon Yoon committed
119
    findReservation,
120
    findOneReservation,
121
122
123
    saveReservation,
    saveTid,
    deleteReservation
Jiwon Yoon's avatar
Jiwon Yoon committed
124
}