reservation.controller.js 1.65 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

Jiwon Yoon's avatar
Jiwon Yoon committed
6
7
const findReservation = async (req, res) => {
    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
16
            }
        })
        console.log(reservedSeats)
        res.json(reservedSeats)
    } catch (error) {
Jiwon Yoon's avatar
Jiwon Yoon committed
17
        res.status(500).send(error.message || "이미 예매되어있는 좌석을 찾는 중 오류발생")
18
19
20
    }
}

Jiwon Yoon's avatar
Jiwon Yoon committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const saveReservation = async (req, res) => {
    const { movieId, selectedTheater, timetable, payment,user, userType } = req.body
    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,
                payment: payment
            })
            res.json(reservation)
        }
        const movie = await Movie.findOne({
            where: {
                movieId: movieId
            }
        })
        movie.ticket_sales++
        await movie.save();
    } catch (error) {
        console.log(error)
        res.status(500).send(error.message || "예매DB에 저장 실패")
    }
}

export default {
    findReservation,
    saveReservation
}