-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
106 lines (74 loc) · 3.04 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const bodyParser = require('body-parser');
const mongodb = require('mongodb');
const ObjectId = mongodb.ObjectId;
// Criando um bloco assíncrono
(async () => {
// Se for uma aplicação real e um banco que você quer proteger a senha, não coloque essas informações no
// GitHub. Procure sempre utilizar as variáveis de ambiente para isso.
const connectionString = 'mongodb+srv://admin:[email protected]/ocean_mongodb?retryWrites=true&w=majority';
// async/await
console.info('Conectando ao banco de dados MongoDB!');
const client = await mongodb.MongoClient.connect(connectionString, {
useUnifiedTopology: true
});
console.info('Banco de dados conectado com sucesso!');
const app = express();
const port = process.env.PORT || 3000;
const jsonParser = bodyParser.json();
app.use(jsonParser);
app.get('/', (req, res) => {
res.send('Hello world com MongoDB!');
});
// Endpoints de envio de mensagens
// CRUD -> Create, Read (Read All e Read Single), Update and Delete
// CRUD -> Criar, Ler (Ler tudo e ler individualmente), atualizar e remover
const db = await client.db('ocean_mongodb');
const mensagens = await db.collection('mensagens');
// Read All
app.get('/mensagens', async (req, res) => {
const findResult = await mensagens.find({}).toArray();
res.json(findResult);
});
// Create
app.post('/mensagens', async (req, res) => {
// Obtendo a mensagem que foi recebida através do body da requisição
const mensagem = req.body;
// Insiro a mensagem na collection de mensagens do MongoDB
const resultado = await mensagens.insertOne(mensagem);
const objetoInserido = resultado.ops[0];
// Envio a mensagem de sucesso, informando o ID obtido
res.json(objetoInserido);
});
// Read Single
app.get('/mensagens/:id', async (req, res) => {
// Pega o ID através dos parâmetros da requisição
const id = req.params.id;
// Acessamos a mensagem de acordo com o ID informado
const mensagem = await mensagens.findOne({ _id: ObjectId(id) });
res.json(mensagem);
});
// Update
app.put('/mensagens/:id', async (req, res) => {
// Acessa o ID pelos parâmetros
const id = req.params.id;
// Obtém a mensagem que foi enviada pelo usuário no corpo (body) da requisição
const novaMensagem = req.body;
const mensagemAtual = await mensagens.findOne({ _id: ObjectId(id) });
mensagemAtual.texto = novaMensagem.texto;
// Atualiza a mensagem direto na lista de mensagens, acessando pelo ID que foi informado
const resultado = await mensagens.updateOne({ _id: ObjectId(id) }, { $set: mensagemAtual });
// Envia uma mensagem de sucesso.
res.json(mensagemAtual);
});
// Delete
app.delete('/mensagens/:id', async (req, res) => {
const id = req.params.id;
const resultado = await mensagens.deleteOne({ _id: ObjectId(id) });
res.send(`Mensagem com o ID ${id} foi removida com sucesso.`);
});
app.listen(port, () => {
console.log(`App rodando em http://localhost:${port}`);
});
// Fecho o bloco assíncrono e executo
})();