Commit 52f6e103 authored by Choi Ga Young's avatar Choi Ga Young
Browse files

Merge remote-tracking branch 'origin/kimpen' into rkyoung7

parents ec8f789e dbfd910e
import { Todo } from '../db/index.js';
import sequelize from 'sequelize';
const AddTodo = async(req, res)=>{
const { Op } = sequelize
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 가져오는 중 에러 발생")
}
}
export default{
AddTodo,
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 가져오는 중 에러 발생")
}
}
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,
create,
edit,
remove,
getParams,
send
}
\ No newline at end of file
......@@ -33,6 +33,7 @@ const Plan = PlanModel(sequelize)
Schedule.belongsTo(User)
Subject.belongsTo(User)
Todo.belongsTo(User)
Plan.belongsTo(Subject)
export {
......
......@@ -2,7 +2,8 @@ import express from "express";
import userRouter from './user.route.js';
import scheduleRouter from "./schedule.route.js";
import subjectRouter from './subject.route.js';
import planRouter from './plan.route.js'
import planRouter from './plan.route.js';
import todoRouter from "./todo.route.js";
const router = express.Router();
......@@ -10,5 +11,6 @@ router.use('/auth', userRouter)
router.use('/schedule', scheduleRouter)
router.use('/subject', subjectRouter)
router.use('/plan', planRouter)
router.use('/todo', todoRouter)
export default router;
\ No newline at end of file
import express from 'express';
import todoCtrl from "../controllers/todo.controller.js";
const router = express.Router();
router
.route("/:userId")
.get(todoCtrl.findbyId, todoCtrl.findbyDate, todoCtrl.send)
.post(todoCtrl.create)
.put(todoCtrl.edit)
.delete(todoCtrl.remove)
router.param("userId", todoCtrl.getParams)
export default router;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment