plan.controller.js 3.07 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
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

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) => {
Choi Ga Young's avatar
Choi Ga Young committed
34
35
  const { info, id } = req.body
  console.log('editPlan info', info, '|', id)
Choi Ga Young's avatar
Choi Ga Young committed
36
  try {
Choi Ga Young's avatar
Choi Ga Young committed
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
    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)
    }
Choi Ga Young's avatar
Choi Ga Young committed
65
66
67
68
69
70
71
72
73
  } 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 {
Choi Ga Young's avatar
Choi Ga Young committed
74
75
76
    let deadlineStr = null
    let endTimeStr = null

Choi Ga Young's avatar
Choi Ga Young committed
77
78
    const { planId } = req.params;
    const findInfo = await Plan.findOne({ where: { id: planId } })
Choi Ga Young's avatar
Choi Ga Young committed
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
    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
      })
    }
Choi Ga Young's avatar
Choi Ga Young committed
109
110
111
112
113
114
115
116
117
118
119
  } catch (error) {
    console.log(error)
    return res.status(500).send(error.message || "계획 가져오기 에러발생")
  }
}

export default {
  addPlan,
  editPlan,
  getInfo
}