todo.controller.js 4.37 KB
Newer Older
1
import { Todo } 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
19
const findbyId = async (req, res, next) => {
  try {
    const userId = req.userId
    const { todoId } = req.query
    if (todoId) {
      console.log(" findbyId todoId가 있을 때 실행", todoId)
      const findTodo = await Todo.findOne({ where: { [Op.and]: [{ id: todoId }, { userId: userId }] }, attributes: ['id', ['title', 'todoTitle'], ['date', 'todoDate'], 'done'] })
      if (!findTodo) throw new Error("해당 todo를 찾지 못했습니다.")
      req.todoOne = findTodo
    }
    next()
  } catch (error) {
    return res.status(500).send(error.message || "todo 가져오는 중 에러 발생")
  }
20
21
}

Kim, Subin's avatar
Kim, Subin committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const findbyDate = async (req, res, next) => {
  try {
    const userId = req.userId
    const { date } = req.query
    if (date) {
      console.log(" findbydate 날짜가 있을 때 실행", date, userId)
      const findList = await Todo.findAll({ where: { [Op.and]: [{ date: { [Op.eq]: date } }, { userId: userId }] }, attributes: ['id', ['title', 'todoTitle'], ['date', 'todoDate'], 'done'] })
      console.log("find==", findList)
      req.todoList = findList
    }
    next()
  } catch (error) {
    return res.status(500).send(error.message || "todo 가져오는 중 에러 발생")
  }
}

Kim, Subin's avatar
Kim, Subin committed
38
39
40
41
42
43
44
45
const findforPercent = async (req, res) => {
  try {
    let doneTodo = null
    const userId = req.userId
    const { start, end } = req.query
    if (end) {
      const { count, rows } = await Todo.findAndCountAll({  where: { [Op.and]: [{ date: { [Op.eq]: start } }, { userId: userId }] } })
    } else {
Kim, Subin's avatar
Kim, Subin committed
46
      let percent = 0
Kim, Subin's avatar
Kim, Subin committed
47
      console.log("findforPercent end 없음")
Kim, Subin's avatar
Kim, Subin committed
48
49
50
51
52
53
54
55
      const nonCheck = await Todo.findAndCountAll({  where: { [Op.and]: [{ date: { [Op.eq]: start } }, { userId: userId }, { done: false }] } })
      const check = await Todo.findAndCountAll({  where: { [Op.and]: [{ date: { [Op.eq]: start } }, { userId: userId }, { done: true }] } })
      let total = nonCheck.count + check.count
      check.rows.forEach(el => nonCheck.rows.push(el.dataValues))
      console.log("non",nonCheck)
      if (total === 0) percent = 0
      else percent = Math.round((check.count / total)*100) 
      return res.json({ percent: percent, list: nonCheck.rows })
Kim, Subin's avatar
Kim, Subin committed
56
57
58
59
60
61
    }
  } catch (error) {
    return res.status(500).send(error.message || "todo 가져오는 중 에러 발생")
  }
}

Kim, Subin's avatar
Kim, Subin committed
62
63
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
110
111
112
113
114
115
116
117
118
119
120
121
const create = async (req, res) => {
  try {
    const userId = req.userId
    const { todoTitle, todoDate } = req.body
    const newTodo = await Todo.create({ title: todoTitle, date: todoDate, userId: userId })
    return res.json(newTodo)
  } catch (error) {
    return res.status(500).send(error.message || "todo 등록 중 에러 발생")
  }
}

const edit = async (req, res) => {
  try {
    let updated = null
    const userId = req.userId
    const { todoId } = req.query
    const { todoTitle, todoDate } = req.body
    if (todoTitle) updated = await Todo.update({ title: todoTitle, date: todoDate }, { where: { [Op.and]: [{ id: todoId }, { userId: userId }] } })
    else updated = await Todo.update({ date: todoDate }, { where: { [Op.and]: [{ id: todoId }, { userId: userId }] } })
    if (!updated) throw new Error("해당 todo의 일부 정보를 수정하는데 실패하였습니다.")
    else return res.send(200)
  } catch (error) {
    return res.status(500).send(error.message || "todo 수정 중 에러 발생")
  }
}

const remove = async (req, res) => {
  try {
    const userId = req.userId
    const { todoId } = req.query
    const deleted = await Todo.destroy({ where: { [Op.and]: [{ id: todoId }, { userId: userId }] } })
    if (!deleted) throw new Error("해당 todo를 삭제하는데 실패하였습니다.")
    else return res.send(200)
  } catch (error) {
    return res.status(500).send(error.message || "todo 삭제 중 에러 발생")
  }
}

const getParams = async (req, res, next) => {
  try {
    const { userId } = req.params
    req.userId = userId
    next()
  } catch (error) {
    return res.status(500).send(error.message || "todo 가져오는 중 에러 발생")
  }
}

const send = async (req, res) => {
  try {
    const result = req.todoOne || req.todoList
    return res.json(result)
  } catch (error) {
    return res.status(500).send(error.message || "todo 가져오는 중 에러 발생")
  }
}

export default {
  findbyId,
  findbyDate,
Kim, Subin's avatar
Kim, Subin committed
122
  findforPercent,
Kim, Subin's avatar
Kim, Subin committed
123
124
125
126
127
  create,
  edit,
  remove,
  getParams,
  send
128
}