schedule.controller.js 7.67 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
import { KU, Schedule } from "../db/index.js";
import sequelize from 'sequelize';
3

Kim, Subin's avatar
Kim, Subin committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const { Op } = sequelize

const findbyId = async (req, res, next) => {
    try {
        const id = req.scheduleId
        if (id) {
            const findSchedule = await KU.findOne({ where: { id: id } })
            if (!findSchedule) throw new Error("해당 일정을 찾지 못했습니다.")
            else {
                const { title, start, end, memo } = findSchedule
                const startDate = dateToString(start, "full")
                const endDate = dateToString(end, "full")
                req.schedule = { title, startDate: startDate, endDate: endDate, memo }
            }
            next()
        } else next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

const findbyDate = async (req, res, next) => {
26
    try {
Kim, Subin's avatar
Kim, Subin committed
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        if (req.date || req.month) {
            let date = ""
            let findList = []
            if (req.date) {
                date = new Date(req.date)
                findList = await KU.findAll({
                    where: {
                        [Op.and]: [
                            {
                                start: {
                                    [Op.lte]: date
                                }
                            }, {
                                end: {
                                    [Op.gte]: date
                                }
                            }
                        ]
                    }, order: [['updatedAt', 'DESC']]
                })
                findList.forEach(schedule => {
                    schedule.dataValues.start = dateToString(schedule.start, "twoYear")
                    schedule.dataValues.end = dateToString(schedule.end, "twoYear")
                })
            } else {
                date = new Date(req.month)
                const year = dateToString(date, "year")
                const month = dateToString(date, "month")
                findList = await KU.findAll({
                    where: {
                        [Op.or]: [
                            {
                                [Op.and]: [
                                    sequelize.where(sequelize.fn('date_part', 'year', sequelize.col('start')), year),
                                    sequelize.where(sequelize.fn('date_part', 'month', sequelize.col('start')), month)
                                ]
                            }, {
                                [Op.and]: [
                                    sequelize.where(sequelize.fn('date_part', 'year', sequelize.col('end')), year),
                                    sequelize.where(sequelize.fn('date_part', 'month', sequelize.col('end')), month)
                                ]
                            }
                        ]
                    }, attributes: ['id', 'title', 'start', 'end']
                    , order: [['start']]
                })
                // console.log("list==",findList)
                findList.forEach(schedule => {
                    // const find = findList.find(el => {
                    //     if (el.dataValues.start <= schedule.dataValues.start) {
                    //         if (el.dataValues.end >= schedule.dataValues.start) return el
                    //     } else if (el.dataValues.start <= schedule.dataValues.end) return el
                    // })
                    // console.log("find==",find.dataValues)
                    if (dateToString(schedule.start, "full") !== dateToString(schedule.end, "full")) schedule.dataValues.end = schedule.dataValues.end.setDate(schedule.dataValues.end.getDate() + 1)
                    schedule.dataValues.allDay = true
                    schedule.dataValues.className = 'text'
                })
            }
            req.scheduleList = findList
            next()
        } else next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}
93

Kim, Subin's avatar
Kim, Subin committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const create = async (req, res) => {
    try {
        let newSchedule = null
        const userId = req.userId
        if (userId === "ku") {
            const { title, startDate, endDate, memo } = req.body
            const start = new Date(startDate)
            const end = new Date(endDate)
            newSchedule = await KU.create({ title: title, start: start, end: end, memo: memo })
        } else {
            const { title, startDate, endDate, startTime, endTime, allDay, location, memo } = req.body
            newSchedule = await Schedule.create({ title: title, location: location, memo: memo })
        }
        return res.json(newSchedule)
108
109
110
111
112
113
114
    } catch (error) {
        return res.status(500).send(error.message || "일정 등록 중 에러 발생")
    }
}

const edit = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
115
116
117
118
119
120
121
122
123
124
125
126
        let updated = null
        const userId = req.userId
        const { scheduleId } = req.query
        if (userId === "ku") {
            const { title, startDate, endDate, memo } = req.body
            updated = await KU.update({ title: title, start: startDate, end: endDate, memo: memo }, { where: { id: scheduleId } })
        } else {
            const { title, startDate, endDate, startTime, endTime, allDay, location, memo } = req.body
            updated = await Schedule.update({ title: title, memo: memo }, { where: { id: scheduleId } })
        }
        if (!updated) throw new Error("해당 일정의 일부 정보를 수정하는데 실패하였습니다.")
        else return res.send(200)
127
128
129
130
131
132
133
    } catch (error) {
        return res.status(500).send(error.message || "일정 수정 중 에러 발생")
    }
}

const remove = async (req, res) => {
    try {
Kim, Subin's avatar
Kim, Subin committed
134
135
136
137
138
139
140
        let deleted = null
        const userId = req.userId
        const { scheduleId } = req.query
        if (userId === "ku") deleted = await KU.destroy({ where: { id: scheduleId } })
        else deleted = await Schedule.destroy({ where: { id: scheduleId } })
        if (!deleted) throw new Error("해당 일정을 삭제하는데 실패하였습니다.")
        else return res.send(200)
141
142
143
144
145
    } catch (error) {
        return res.status(500).send(error.message || "일정 삭제 중 에러 발생")
    }
}

Kim, Subin's avatar
Kim, Subin committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const getParams = async (req, res, next) => {
    try {
        const { userId } = req.params
        console.log("getParams", userId)
        req.userId = userId
        next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

const querySeparation = async (req, res, next) => {
    try {
        const { scheduleId, date, dateMonth } = req.query
        req.scheduleId = scheduleId
        req.date = date
        req.month = dateMonth
        next()
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

const send = async (req, res) => {
    try {
        const result = req.schedule || req.scheduleList
        return res.json(result)
    } catch (error) {
        return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
    }
}

function dateToString(dateObj, method) {
    const year = dateObj.getFullYear()
    const year_disit = String(year).substring(2, 4)
    const month = dateObj.getMonth() + 1
    const date = dateObj.getDate()

    if (method === "full") return [year, (month > 9 ? "" : "0") + month, (date > 9 ? "" : "0") + date].join("-")
    else if (method === "twoYear") return [year_disit, (month > 9 ? "" : "0") + month, (date > 9 ? "" : "0") + date].join("-")
    else if (method === "year") return year
    else return month
}

190
export default {
Kim, Subin's avatar
Kim, Subin committed
191
192
    findbyId,
    findbyDate,
193
194
    create,
    edit,
Kim, Subin's avatar
Kim, Subin committed
195
196
197
198
    remove,
    getParams,
    querySeparation,
    send
199
}