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

Kim, Subin's avatar
Kim, Subin committed
4
const { Op } = sequelize
5

Kim, Subin's avatar
Kim, Subin committed
6
7
8
9
10
11
12
13
14
15
16
17
18
const findAll = async (req, res) => {
  try {
    let findList = null
    const { subjectId } = req.query
    const userId = req.userId
    if (subjectId) findList = await Subject.findAll({ where: { [Op.and]: [{ id: subjectId }, { userId: userId }] }, order: [['updatedAt', 'DESC']] })
    else findList = await Subject.findAll({ where: { userId: userId }, order: [['updatedAt', 'DESC']] })
    const subjectAndPlan = await Promise.all(findList.map(async (subjectInfo) => {
      const resPlan = await Plan.findAll({ where: { subjectId: subjectInfo.id } })
      subjectInfo.dataValues.planList = resPlan
      return subjectInfo
    }))
    return res.json(subjectAndPlan)
19
  } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
20
    return res.status(500).send(error.message || "과목 및 해당 과목 관련 학업계획 조회 에러 발생")
21
  }
22
}
23

Kim, Subin's avatar
Kim, Subin committed
24
const findSubject = async (req, res) => {
25
  try {
Kim, Subin's avatar
Kim, Subin committed
26
27
28
29
30
31
32
    let find = null
    const { subjectId } = req.query
    const userId = req.userId
    if (subjectId) find = await Subject.findOne({ where: { [Op.and]: [{ id: subjectId }, { userId: userId }] }, attributes: ['id', ['name', 'lectureName'], 'prof', ['room', 'classRoom']] })
    else find = await Subject.findAll({ attributes: ['id', 'name'] })
    if (!find) throw new Error("과목 정보를 찾지 못했습니다.")
    return res.json(find)
33
  } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
34
    return res.status(500).send(error.message || "과목 조회 에러 발생")
35
36
  }
}
37

Kim, Subin's avatar
Kim, Subin committed
38
const create = async (req, res) => {
39
  try {
Kim, Subin's avatar
Kim, Subin committed
40
41
42
43
    const userId = req.userId
    const { lectureName, prof, classRoom } = req.body
    const newSubject = await Subject.create({ name: lectureName, prof: prof, room: classRoom, userId: userId })
    return res.json(newSubject)
44
  } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
45
    return res.status(500).send(error.message || "과목 생성 에러 발생")
46
  }
47
48
}

Kim, Subin's avatar
Kim, Subin committed
49
const edit = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
50
  try {
Kim, Subin's avatar
Kim, Subin committed
51
52
53
54
55
56
57
58
59
60
    const { subjectId } = req.query
    const userId = req.userId
    const { lectureName, prof, classRoom } = req.body
    const updated = await Subject.update({ name: lectureName, prof: prof, room: classRoom }, { where: { [Op.and]: [{ id: subjectId }, { userId: userId }] } })
    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
61

Kim, Subin's avatar
Kim, Subin committed
62
63
64
65
66
67
68
const remove = async (req, res) => {
  try {
    const { subjectId } = req.query
    const userId = req.userId
    const deleted = await Subject.destroy({ where: { [Op.and]: [{ id: subjectId }, { userId: userId }] } })
    if (!deleted) throw new Error("해당 과목을 삭제하는데 실패하였습니다.")
    else return res.send(200)
Choi Ga Young's avatar
Choi Ga Young committed
69
  } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
70
    return res.status(500).send(error.message || "과목 삭제 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
71
72
73
  }
}

Kim, Subin's avatar
Kim, Subin committed
74
const getParams = async (req, res, next) => {
Choi Ga Young's avatar
Choi Ga Young committed
75
76
  try {
    const { userId } = req.params
Kim, Subin's avatar
Kim, Subin committed
77
78
    req.userId = userId
    next()
Choi Ga Young's avatar
Choi Ga Young committed
79
  } catch (error) {
Kim, Subin's avatar
Kim, Subin committed
80
    return res.status(500).send(error.message || "사용자 정보 조회 에러 발생")
Choi Ga Young's avatar
Choi Ga Young committed
81
82
83
  }
}

84
export default {
Kim, Subin's avatar
Kim, Subin committed
85
86
87
88
89
90
  findAll,
  findSubject,
  create,
  edit,
  remove,
  getParams
91
}