plan.controller.js 3 KB
Newer Older
Kim, Subin's avatar
Kim, Subin committed
1
2
import { Plan } from "../db/index.js";
import { dateToString } from "./schedule.controller.js";
Kim, Subin's avatar
plan    
Kim, Subin committed
3
4

const getOne = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
5
  try {
Kim, Subin's avatar
Kim, Subin committed
6
7
8
    let sendPlan = null
    const planId = req.planId
    const findPlan = await Plan.findOne({ where: { id: planId } })
Kim, Subin's avatar
plan    
Kim, Subin committed
9
    if (!findPlan) throw new Error("학업 계획 정보를 찾지 못했습니다.")
Kim, Subin's avatar
Kim, Subin committed
10
11
12
13
14
15
16
17
18
    else {
      const { id, title, deadline, memo, timeChecked, checked, subjectId } = findPlan
      const endDate = dateToString(deadline, "full")
      if (timeChecked) {
        const endTime = dateToString(deadline, "time")
        sendPlan = { id: id, studyplanTitle: title, endDate: endDate, endTime: endTime, deadline: timeChecked ? "on" : "off", memo: memo, selected: subjectId }
      } else sendPlan = { id: id, studyplanTitle: title, endDate: endDate, deadline: timeChecked ? "on" : "off", memo: memo, selected: subjectId }
    }
    return res.json(sendPlan)
Choi Ga Young's avatar
Choi Ga Young committed
19
  } catch (error) {
Kim, Subin's avatar
plan    
Kim, Subin committed
20
    return res.status(500).send(error.message || "학업계획 조회 중 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
21
22
23
  }
}

Kim, Subin's avatar
plan    
Kim, Subin committed
24
const create = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
25
  try {
Kim, Subin's avatar
plan    
Kim, Subin committed
26
27
28
29
30
31
32
33
34
    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
35
  } catch (error) {
Kim, Subin's avatar
plan    
Kim, Subin committed
36
    return res.status(500).send(error.message || "학업계획 생성 중 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
37
38
39
  }
}

Kim, Subin's avatar
plan    
Kim, Subin committed
40
const edit = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
41
  try {
Kim, Subin's avatar
plan    
Kim, Subin committed
42
43
44
45
46
47
48
49
    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)
Kim, Subin's avatar
Kim, Subin committed
50
    const updated = await Plan.update({ title: studyplanTitle, deadline: date, memo: memo, timeChecked: check_v, subjectId: selected }, { where: { id: planId } })
Kim, Subin's avatar
plan    
Kim, Subin committed
51
52
53
54
55
56
    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
57

Kim, Subin's avatar
plan    
Kim, Subin committed
58
59
60
61
62
63
64
65
66
67
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
68

Kim, Subin's avatar
plan    
Kim, Subin committed
69
70
71
72
73
const getParams = async (req, res, next) => {
  try {
    const { planId } = req.params
    req.planId = planId
    next()
Choi Ga Young's avatar
Choi Ga Young committed
74
  } catch (error) {
Kim, Subin's avatar
plan    
Kim, Subin committed
75
    return res.status(500).send(error.message || "일정 가져오는 중 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
76
77
78
79
  }
}

export default {
Kim, Subin's avatar
plan    
Kim, Subin committed
80
81
82
83
84
  getOne,
  create,
  edit,
  remove,
  getParams
Choi Ga Young's avatar
Choi Ga Young committed
85
}