todo.controller.js 4.77 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
const findforPercent = async (req, res) => {
  try {
Kim, Subin's avatar
Kim, Subin committed
40
    let sendList = null
Kim, Subin's avatar
Kim, Subin committed
41
42
43
    const userId = req.userId
    const { start, end } = req.query
    if (end) {
Kim, Subin's avatar
Kim, Subin committed
44
      // weekly에서 불러올 todoList
Kim, Subin's avatar
Todo    
Kim, Subin committed
45
      console.log("week 실행하냐")
Kim, Subin's avatar
Kim, Subin committed
46
47
      // const findList = await Todo.findAndCountAll({ where: { [Op.and]: [{ userId: userId }, { date: [start, end] }] }, order: [[]] group: ['date'] })
      // console.log("week findList", findList)
Kim, Subin's avatar
Kim, Subin committed
48
    } else {
Kim, Subin's avatar
Kim, Subin committed
49
      // Menu
Kim, Subin's avatar
Kim, Subin committed
50
      let percent = 0
Kim, Subin's avatar
Kim, Subin committed
51
      console.log("findforPercent end 없음")
Kim, Subin's avatar
Todo    
Kim, Subin committed
52
53
      const nonCheck = await Todo.findAndCountAll({ where: { [Op.and]: [{ date: { [Op.eq]: start } }, { userId: userId }, { done: false }] }, order: [['updatedAt', "DESC"]] })
      const check = await Todo.findAndCountAll({ where: { [Op.and]: [{ date: { [Op.eq]: start } }, { userId: userId }, { done: true }] }, order: [['updatedAt', "DESC"]] })
Kim, Subin's avatar
Kim, Subin committed
54
55
      let total = nonCheck.count + check.count
      if (total === 0) percent = 0
Kim, Subin's avatar
Todo    
Kim, Subin committed
56
      else percent = Math.round((check.count / total) * 100)
Kim, Subin's avatar
Kim, Subin committed
57
58
59

      if (nonCheck.count < 3) check.rows.forEach(el => nonCheck.rows.push(el.dataValues))
      return res.json({ percent: percent, list: nonCheck.rows.slice(0, 3) })
Kim, Subin's avatar
Kim, Subin committed
60
61
62
63
64
65
    }
  } catch (error) {
    return res.status(500).send(error.message || "todo 가져오는 중 에러 발생")
  }
}

Kim, Subin's avatar
Kim, Subin committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
Kim, Subin's avatar
Kim, Subin committed
82
83
    const { todoTitle, todoDate, done } = req.body
    console.log("done==",done, userId, todoId)
Kim, Subin's avatar
Kim, Subin committed
84
    if (todoTitle) updated = await Todo.update({ title: todoTitle, date: todoDate }, { where: { [Op.and]: [{ id: todoId }, { userId: userId }] } })
Kim, Subin's avatar
Kim, Subin committed
85
86
    else if (todoDate) updated = await Todo.update({ date: todoDate }, { where: { [Op.and]: [{ id: todoId }, { userId: userId }] } })
    else updated = await Todo.update({ done: !done }, { where: { [Op.and]: [{ id: todoId }, { userId: userId }] } })
Kim, Subin's avatar
Kim, Subin committed
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
122
123
124
125
126
127
    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
128
  findforPercent,
Kim, Subin's avatar
Kim, Subin committed
129
130
131
132
133
  create,
  edit,
  remove,
  getParams,
  send
134
}