-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (48 loc) · 1.69 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
const express = require('express')
const mongoose = require('mongoose')
const app = express()
app.use(express.json())
const PORT = 3333
const HOST = '0.0.0.0'
// construção do banco de dados(models)
const Film = mongoose.model('Film',{
title: String,
description: String,
image_url: String,
trailer_url: String,
date: {type: Date, default: Date.now()},
});
app.get("/", async (req, res) =>{
const films = await Film.find()
return res.send(films)
})
app.put("/:id", async (req, res) => {
const options = { new: true } // retorna o novo documento após a atualização // default: false (retorna o antigo documento) // https://mongoosejs.com/docs/api/query.html#query_Query_new
const film = await Film.findByIdAndUpdate(req.params.id, {
title: req.body.title,
description: req.body.description,
image_url: req.body.image_url,
trailer_url: req.body.trailer_url,
}, options);
return res.send(film)
})
app.post("/", async (req, res) =>{
const film = new Film({
title: req.body.title,
description: req.body.description,
image_url: req.body.image_url,
trailer_url: req.body.trailer_url,
})
await film.save()
//res.send(film)
return res.send(film)
})
app.delete("/:id", async (req, res) => {
const film = await Film.findByIdAndDelete(req.params.id)
res.send("Filme deletado com sucesso!")
})
app.listen(PORT, HOST, () => {
// connexão com mongodb
mongoose.connect('mongodb+srv://alairtonpereiradev:[email protected]/?retryWrites=true&w=majority&appName=starwars-api')
console.log(`Server running at http://${HOST}:${PORT}/`);
})