subject.controller.js 2.99 KB
Newer Older
1
2
3
4
5
import { Subject } from '../db/index.js';

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
11
    if (findName) {
      throw new Error("이미 있는 과목입니다.")
    }
    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
17
18
19
20
21
    })

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

24
const getSubInfo = async (req, res) => {
Choi Ga Young's avatar
Choi Ga Young committed
25
  console.log('server/getSubInfo req.params', req.params)
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  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 || "과목정보 가져오기 에러발생")
  }
}
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 {
      res.send(200)
    }
  } catch (error) {
    console.log(error)
    return res.status(500).send(error.message || "과목정보 수정 에러발생")
  }
62
63
}

Choi Ga Young's avatar
Choi Ga Young committed
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
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
    })
    // const addplanlist = sublist.map(async(info)=>{
    //   const resplan = await Plan
    // })


  } 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 || "과목명 조회 에러")
  }

}

110
export default {
111
112
  addsubject,
  getSubInfo,
Choi Ga Young's avatar
Choi Ga Young committed
113
114
115
  editSubject,
  allSubject,
  subjectTitle
116
}