subject.controller.js 1.78 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
14
15
      name: info.lectureName,
      prof: info.prof,
      room: info.classRoom,
      userId: userId
16
17
18
19
20
21
    })

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

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const getSubInfo = async (req, res) => {
  console.log('server/getSubInfo req.body', req.params)
  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
64
}

export default {
65
66
67
  addsubject,
  getSubInfo,
  editSubject
68
}