forked from tiagoamaro/toti-nodejs-exercicio-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (44 loc) · 1.39 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 { Sequelize, DataTypes} = require('sequelize')
const task = require('./models/task')
const Task = require('./models/task')
const app = express()
const sequelize = new Sequelize({ dialect: 'sqlite', storage: './task-list.db' })
const tasks = Task(sequelize, DataTypes)
// We need to parse JSON coming from requests
app.use(express.json())
// List tasks
app.get('/tasks', async (req, res) => {
const tas = await tasks.findAll()
res.json({ Tasks: tas })
})
// Create task
app.post('/tasks', async (req, res) => {
const body = await tasks.create({
description: req.body.description,
done: req.body.done
})
res.json(body)
})
// Show task
app.get('/tasks/:id', async (req, res) => {
const taskId = req.params.id
const Find = await tasks.findByPk(taskId)
res.json({ Find })
})
// Update task
app.put('/tasks/:id', async (req, res) => {
const taskId = req.params.id
const body = await tasks.update({ description: req.body.description, done: req.body.done },
{ where: {id: taskId}})
res.json({ action: 'Atualizando task', taskId: taskId })
})
// Delete task
app.delete('/tasks/:id', async (req, res) => {
const taskId = req.params.id
const del = await tasks.destroy({ where: { id: taskId }})
res.send({ action: 'Deleting task', taskId: taskId })
})
app.listen(3000, () => {
console.log('Iniciando o ExpressJS na porta 3000')
})