reservation.controller.js 2.85 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 {
Jiwon Yoon's avatar
Jiwon Yoon committed
7
8
        const { timeTable } = req.body
        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
26
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
                userType: role,
                user: id
41
            },
한규민's avatar
한규민 committed
42
43
44
45
46
47
            include: [Theater, TimeTable]
        });
        console.log(reservation);
        req.reservation = reservation
        next()
        // res.json(reservation);
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 {
Jiwon Yoon's avatar
Jiwon Yoon committed
54
55
56
        const { movieId, selectedTheater, timetable, payment, user, userType, totalFee } = req.body
        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
        for (let index = 0; index < rows.length; index++) {
            const reservation = await Reservation.create({
                user: user,
                userType: userType,
                movieId: movieId,
한규민's avatar
한규민 committed
62
                theaterId: selectedTheater,
Jiwon Yoon's avatar
Jiwon Yoon committed
63
64
                row: rows[index],
                col: cols[index],
한규민's avatar
한규민 committed
65
                timetableId: timetable,
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
84
    } catch (error) {
        console.log(error)
        res.status(500).send(error.message || "예매DB에 저장 실패")
    }
}

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