todo.controller.js 4.51 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
const findforPercent = async (req, res) => {
  try {
    let doneTodo = null
    const userId = req.userId
    const { start, end } = req.query
    if (end) {
Kim, Subin's avatar
Todo    
Kim, Subin committed
44
45
46
      console.log("week 실행하냐")
      const findList= await Todo.findAndCountAll({ where: { [Op.and]: [{ userId: userId }, { date: [start, end] }] }, group: ['date'] })
      console.log("week findList",findList)
Kim, Subin's avatar
Kim, Subin committed
47
    } else {
Kim, Subin's avatar
Kim, Subin committed
48
      let percent = 0
Kim, Subin's avatar
Kim, Subin committed
49
      console.log("findforPercent end 없음")
Kim, Subin's avatar
Todo    
Kim, Subin committed
50
51
      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
52
53
      let total = nonCheck.count + check.count
      check.rows.forEach(el => nonCheck.rows.push(el.dataValues))
Kim, Subin's avatar
Todo    
Kim, Subin committed
54
      console.log("non", nonCheck)
Kim, Subin's avatar
Kim, Subin committed
55
      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
      return res.json({ percent: percent, list: nonCheck.rows })
Kim, Subin's avatar
Kim, Subin committed
58
59
60
61
62
63
    }
  } catch (error) {
    return res.status(500).send(error.message || "todo 가져오는 중 에러 발생")
  }
}

Kim, Subin's avatar
Kim, Subin 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
124
  findforPercent,
Kim, Subin's avatar
Kim, Subin committed
125
126
127
128
129
  create,
  edit,
  remove,
  getParams,
  send
130
}