plan.controller.js 5.78 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
2
import { Plan, Subject } from "../db/index.js";
import * as ConvertDate from "./schedule.controller.js";
Choi Ga Young's avatar
Choi Ga Young committed
3

Kim, Subin's avatar
plan    
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
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// const addPlan = async (req, res) => {
//   console.log('server/addPlan req.body', req.body)
//   try {
//     let end = null;
//     let tf = false;
//     const { info } = req.body
//     const { studyplanTitle, endDate, endTime, deadline, memo, selected } = info
//     console.log('제목확인', studyplanTitle)
//     if (deadline === "on") {
//       end = new Date(endDate + " " + endTime)
//       tf = true
//     } else {
//       end = new Date(endDate)
//     }
//     const result = await Plan.create({
//       subjectId: selected,
//       title: studyplanTitle,
//       deadline: end,
//       memo: memo,
//       timeChecked: tf,
//       checked: false
//     })
//     return res.json(result)
//   } catch (error) {
//     console.log(error)
//     return res.status(500).send(error.message || "계획저장 에러발생")
//   }
// }

// const editPlan = async (req, res) => {
//   const { info, id } = req.body
//   console.log('editPlan info', info, '|', id)
//   try {
//     let result = null
//     if (info.deadline === "on") {
//       result = await Plan.update({
//         subjectId: info.selected,
//         title: info.studyplanTitle,
//         deadline: new Date(info.endDate + " " + info.endTime),
//         memo: info.memo,
//         timeChecked: true,
//         checked: false
//       }, { where: { id: id } })
//       res.send(200)
//     } else {
//       result = await Plan.update({
//         subjectId: info.selected,
//         title: info.studyplanTitle,
//         deadline: new Date(info.endDate),
//         memo: info.memo,
//         timeChecked: false,
//         checked: false
//       }, {
//         where: { id: id }
//       })
//     }
//     if (!result) {
//       throw new Error("과목정보 수정 에러발생")
//     } else {
//       return res.send(200)
//     }
//   } catch (error) {
//     console.log(error)
//     return res.status(500).send(error.message || "계획수정 에러발생")
//   }
// }

// const getInfo = async (req, res) => {
//   console.log('server/getInfo req.params', req.params)
//   try {
//     let deadlineStr = null
//     let endTimeStr = null

//     const { planId } = req.params;
//     const findInfo = await Plan.findOne({ where: { id: planId } })
//     const Info = findInfo.dataValues;

//     const getSubTitle = await Subject.findAll({
//       attributes: ['name'],
//       where: { id: Info.subjectId }
//     })

//     deadlineStr = ConvertDate.dateToString(Info.deadline, "full")
//     endTimeStr = ConvertDate.dateToString(Info.deadline, "time")

//     if (Info.timeChecked) {
//       res.json({
//         subjectId: Info.subjectId,
//         subjectName: getSubTitle[0].dataValues.name,
//         title: Info.title,
//         endDate: deadlineStr,
//         endTime: endTimeStr,
//         deadline: "on",
//         memo: Info.memo
//       })
//     } else {
//       res.json({
//         subjectId: Info.subjectId,
//         subjectName: getSubTitle[0].dataValues.name,
//         title: Info.title,
//         endDate: deadlineStr,
//         deadline: "",
//         memo: Info.memo
//       })
//     }
//   } catch (error) {
//     console.log(error)
//     return res.status(500).send(error.message || "계획 가져오기 에러발생")
//   }
// }

const getOne = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
116
  try {
Kim, Subin's avatar
plan    
Kim, Subin committed
117
118
119
120
121
122
    let findPlan = null
    const { planId } = req.query
    console.log("get One", planId)
    if (planId) findPlan = await Plan.findOne({ where: { id: planId } })
    if (!findPlan) throw new Error("학업 계획 정보를 찾지 못했습니다.")
    return res.json(find)
Choi Ga Young's avatar
Choi Ga Young committed
123
  } catch (error) {
Kim, Subin's avatar
plan    
Kim, Subin committed
124
    return res.status(500).send(error.message || "학업계획 조회 중 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
125
126
127
  }
}

Kim, Subin's avatar
plan    
Kim, Subin committed
128
const create = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
129
  try {
Kim, Subin's avatar
plan    
Kim, Subin committed
130
131
132
133
134
135
136
137
138
    let date = null
    let check_v = false
    const { studyplanTitle, endDate, endTime, memo, deadline, selected } = req.body
    if (deadline === "on") {
      date = new Date(endDate + " " + endTime)
      check_v = true
    } else date = new Date(endDate)
    const newPlan = await Plan.create({ title: studyplanTitle, deadline: date, memo: memo, timeChecked: check_v, subjectId: selected })
    return res.json(newPlan)
Choi Ga Young's avatar
Choi Ga Young committed
139
  } catch (error) {
Kim, Subin's avatar
plan    
Kim, Subin committed
140
    return res.status(500).send(error.message || "학업계획 생성 중 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
141
142
143
  }
}

Kim, Subin's avatar
plan    
Kim, Subin committed
144
const edit = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
145
  try {
Kim, Subin's avatar
plan    
Kim, Subin committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    const planId = req.planId
    let date = null
    let check_v = false
    const { studyplanTitle, endDate, endTime, memo, deadline, selected } = req.body
    if (deadline === "on") {
      date = new Date(endDate + " " + endTime)
      check_v = true
    } else date = new Date(endDate)
    const updated = await Plan.updated({ title: studyplanTitle, deadline: date, memo: memo, timeChecked: check_v, subjectId: selected }, { where: { id: planId } })
    if (!updated) throw new Error("해당 학업계획의 일부 정보를 수정하는데 실패하였습니다.")
    else return res.send(200)
  } catch (error) {
    return res.status(500).send(error.message || "학업계획 수정 중 에러 발생")
  }
}
Choi Ga Young's avatar
Choi Ga Young committed
161

Kim, Subin's avatar
plan    
Kim, Subin committed
162
163
164
165
166
167
168
169
170
171
const remove = async (req, res) => {
  try {
    const planId = req.planId
    const deleted = await Plan.destroy({ where: { id: planId } })
    if (!deleted) throw new Error("해당 과목을 삭제하는데 실패하였습니다.")
    else return res.send(200)
  } catch (error) {
    return res.status(500).send(error.message || "학업계획 삭제 중 에러 발생")
  }
}
Choi Ga Young's avatar
Choi Ga Young committed
172

Kim, Subin's avatar
plan    
Kim, Subin committed
173
174
175
176
177
const getParams = async (req, res, next) => {
  try {
    const { planId } = req.params
    req.planId = planId
    next()
Choi Ga Young's avatar
Choi Ga Young committed
178
  } catch (error) {
Kim, Subin's avatar
plan    
Kim, Subin committed
179
    return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
180
181
182
183
  }
}

export default {
Kim, Subin's avatar
plan    
Kim, Subin committed
184
185
186
187
188
189
190
191
  getOne,
  create,
  edit,
  remove,
  getParams
  // addPlan,
  // editPlan,
  // getInfo
Choi Ga Young's avatar
Choi Ga Young committed
192
}