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

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

export default {
84
    findReservedSeats,
Jiwon Yoon's avatar
Jiwon Yoon committed
85
    findReservation,
86
    findOneReservation,
Jiwon Yoon's avatar
Jiwon Yoon committed
87
88
    saveReservation
}