reservation.controller.js 2.52 KB
Newer Older
1
import axios from 'axios'
Jiwon Yoon's avatar
Jiwon Yoon committed
2
import { Movie, Reservation, Theater } from '../db/index.js'
3
4
5
import sequelize from 'sequelize'
const { Op } = sequelize

6
const findReservedSeats = async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
7
    const { timetable } = req.body
8
9
10
    try {
        const reservedSeats = await Reservation.findAll({
            where: {
Jiwon Yoon's avatar
Jiwon Yoon committed
11
                timetable: 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const findReservation = async (req, res) => {
    const { user } = req.body
    try {
        const reservation = await Reservation.findAll({
            where: {
                user: user
            }
        })
        res.json(reservation)
    } catch (error) {
        res.status(500).send(error.message || "예매 내역들을 찾는 중 오류발생")
    }
}
const findOneReservation = async (req, res) => {
    const { userType, user } = req.body
    try {
        const reservation = await Reservation.findAll({
            where: {
                userType: userType,
                user: user
            },
        })
Jiwon Yoon's avatar
Jiwon Yoon committed
41
        // console.log(reservation)
42
43
44
45
46
        res.json(reservation)
    } catch (error) {
        res.status(500).send(error.message || "예매 내역을 찾는 중 오류 발생")
    }
}
Jiwon Yoon's avatar
Jiwon Yoon committed
47
const saveReservation = async (req, res) => {
Jiwon Yoon's avatar
Jiwon Yoon committed
48
    const { movieId, selectedTheater, timetable, payment, user, userType, totalFee } = req.body
Jiwon Yoon's avatar
Jiwon Yoon committed
49
50
51
52
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,
                theater: selectedTheater,
                row: rows[index],
                col: cols[index],
                timetable: timetable,
Jiwon Yoon's avatar
Jiwon Yoon committed
61
62
                payment: payment,
                totalFee: totalFee
Jiwon Yoon's avatar
Jiwon Yoon committed
63
64
65
66
67
68
69
70
71
            })
        }
        const movie = await Movie.findOne({
            where: {
                movieId: movieId
            }
        })
        movie.ticket_sales++
        await movie.save();
72
        res.json({ message: '200 OK' })
Jiwon Yoon's avatar
Jiwon Yoon committed
73
74
75
76
77
78
79
    } catch (error) {
        console.log(error)
        res.status(500).send(error.message || "예매DB에 저장 실패")
    }
}

export default {
80
    findReservedSeats,
Jiwon Yoon's avatar
Jiwon Yoon committed
81
    findReservation,
82
    findOneReservation,
Jiwon Yoon's avatar
Jiwon Yoon committed
83
84
    saveReservation
}