subject.controller.js 3.26 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import { Subject, Plan } from '../db/index.js';
2
3
4
5

const addsubject = async (req, res) => {
  console.log('server/addsubject req.body', req.body)
  try {
6
7
    const { info, userId } = req.body;
    const findName = await Subject.findOne({ where: { name: info.lectureName } });
8
9
10
    if (findName) {
      throw new Error("이미 있는 과목입니다.")
    }
Choi Ga Young's avatar
Choi Ga Young committed
11
    const result = await Subject.create({
12
13
      name: info.lectureName,
      prof: info.prof,
Choi Ga Young's avatar
Choi Ga Young committed
14
      room: info.classRoom,
15
      userId: userId
16
    })
Choi Ga Young's avatar
Choi Ga Young committed
17
18
19
20
21
    if (!result) {
      throw new Error("과목추가 에러발생")
    } else {
      return res.send(200)
    }
22
23
24
25
26

  } catch (error) {
    console.log(error)
    return res.status(500).send(error.message || "과목저장 에러발생")
  }
27
}
28

29
const getSubInfo = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
30
  console.log('server/getSubInfo req.params', req.params)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  try {
    const { subjectId } = req.params;
    const findSubInfo = await Subject.findOne({ where: { id: subjectId } })
    if (findSubInfo) {
      res.json({
        name: findSubInfo.dataValues.name,
        prof: findSubInfo.dataValues.prof,
        room: findSubInfo.dataValues.room
      })
    } else {
      throw new Error("과목 찾기 실패")
    }
  } catch (error) {
    console.log(error)
    return res.status(500).send(error.message || "과목정보 가져오기 에러발생")
  }
}
48

49
50
51
52
53
54
55
56
57
58
59
60
const editSubject = async (req, res) => {
  console.log('server/editSubject req.body', req.body)
  try {
    const { info, id } = req.body;
    const result = await Subject.update({
      name: info.lectureName,
      prof: info.prof,
      room: info.classRoom,
    }, { where: { id: id } })
    if (!result) {
      throw new Error("과목정보 수정 에러발생")
    } else {
Choi Ga Young's avatar
Choi Ga Young committed
61
      return res.send(200)
62
63
64
65
66
    }
  } catch (error) {
    console.log(error)
    return res.status(500).send(error.message || "과목정보 수정 에러발생")
  }
67
68
}

Choi Ga Young's avatar
Choi Ga Young committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const allSubject = async (req, res) => {
  console.log('server/allSubject req.params', req.params)
  try {
    const { userId } = req.params;
    const findAllInfo = await Subject.findAll({ where: { userId: userId } })
    const sublist = findAllInfo.map(info => {
      const newList = {
        id: info.id,
        name: info.name,
        prof: info.prof,
        room: info.room,
        time: info.updatedAt
      }
      return newList
    })

Choi Ga Young's avatar
Choi Ga Young committed
85
86
87
88
89
90
91
    const addplanlist = await Promise.all(sublist.map(async (info) => {
      const resplan = await Plan.findAll({ where: { subjectId: info.id } })
      info.planList = resplan
      return info
    }))

    return res.json(addplanlist)
Choi Ga Young's avatar
Choi Ga Young committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

  } catch (error) {
    console.log(error)
    return res.status(500).send(error.message || "전체과목 조회 에러")
  }
}

const subjectTitle = async (req, res) => {
  console.log('server/subjectTitle req.params', req.params)
  try {
    const { userId } = req.params
    const findAllTitle = await Subject.findAll({ where: { userId: userId } })
    const sublist = findAllTitle.map(info => {
      const newList = {
        id: info.id,
        name: info.name,
      }
      return newList
    })
    res.json(sublist)
  } catch (error) {
    console.log(error)
    return res.status(500).send(error.message || "과목명 조회 에러")
  }

}

119
export default {
120
121
  addsubject,
  getSubInfo,
Choi Ga Young's avatar
Choi Ga Young committed
122
123
124
  editSubject,
  allSubject,
  subjectTitle
125
}