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

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
109
110
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
111
        });
112
113
114
115
116
117
118
        res.json({ message: '결제실패로 인한 예매DB삭제' })
    } catch (error) {
        console.log(error)
        res.status(500).send(error.message || "예매DB 삭제실패")
    }
}

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