-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (43 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
require('dotenv').config()
const app = express()
app.use(cors())
app.use(express.json())
mongoose.set("strictQuery", false);
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log("DB Connetion Successfull");
}).catch((err) => {
console.log(err.message);
})
const TodoList = require('./model/TodoList')
app.get('/todolists', async (_req, res) => {
const todoList = await TodoList.find()
res.json(todoList)
})
app.post('/todolists/new', (req, res) => {
const todo = new TodoList({
title: req.body.title,
description: req.body.description,
taskStatus: req.body.status
})
todo.save()
res.json(todo)
})
app.delete('/todolists/delete/:uniqueIdentifier', async (req, res) => {
const result = await TodoList.findByIdAndDelete(req.params.uniqueIdentifier)
res.json(result)
})
app.put('/todolists/complete/:id', async (req, res) => {
const todo = await TodoList.findById(req.params.id)
todo.taskStatus = !todo.taskStatus
todo.save()
res.json(todo)
})
const server = app.listen(process.env.PORT, () =>
console.log(`Server started on ${process.env.PORT}`)
);