mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
index and create todos and lists
This commit is contained in:
34
routes/todos.js
Normal file
34
routes/todos.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const express = require("express");
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const TodoList = mongoose.model("TodoList");
|
||||
const Todo = mongoose.model("Todo");
|
||||
|
||||
const asyncHelper = require("../asyncHelper");
|
||||
|
||||
// index
|
||||
router.get(
|
||||
"/",
|
||||
asyncHelper(async (req, res) => {
|
||||
const todos = await Todo.find({}).exec();
|
||||
res.json(todos);
|
||||
})
|
||||
);
|
||||
|
||||
// create
|
||||
router.post(
|
||||
"/",
|
||||
asyncHelper(async (req, res) => {
|
||||
const { text, listId } = req.body;
|
||||
const list = await TodoList.findById(listId);
|
||||
const todo = new Todo({ text, list: list._id });
|
||||
await todo.save();
|
||||
list.todos.push(todo.id);
|
||||
await list.save();
|
||||
res.json({ success: true });
|
||||
})
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user