Skip to content

Commit

Permalink
Merge pull request #5 from marcoaminotto/validation
Browse files Browse the repository at this point in the history
Add validations
  • Loading branch information
marcoaminotto authored May 3, 2020
2 parents 7707cac + 697d694 commit 2fb87d2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Binary file modified backend/src/database/db.sqlite
Binary file not shown.
3 changes: 3 additions & 0 deletions backend/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const express = require('express');
const cors = require('cors');
const { errors } = require('celebrate');
const routes = require('./routes');
const app = express();

app.use(cors())
app.use(express.json());
app.use(routes);

app.use(errors());

app.listen(3333);
43 changes: 38 additions & 5 deletions backend/src/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require('express');
const { celebrate, Segments, Joi} = require('celebrate');

const NgoController = require('./controllers/NgoController');
const IncidentsController = require('./controllers/IncidentController');
Expand All @@ -9,12 +10,44 @@ const routes = express.Router();
routes.post('/sessions', SessionController.create);

routes.get('/ngos', NgoController.index);
routes.post('/ngos', NgoController.create);
routes.post('/ngos', celebrate({
[Segments.BODY]: Joi.object().keys({
name: Joi.string().required(),
email: Joi.string().required().email(),
whatsapp: Joi.number().required().min(10).max(11),
city: Joi.string().required(),
uf: Joi.string().required().min(2).max(3),
country: Joi.string().required()
})
}), NgoController.create);

routes.get('/profile', ProfileController.index);
routes.get('/profile', celebrate({
[Segments.HEADERS]: Joi.object({
authorization: Joi.string().required(),
}).unknown()
}),ProfileController.index);

routes.get('/incidents', IncidentsController.index);
routes.post('/incidents', IncidentsController.create);
routes.delete('/incidents/:id', IncidentsController.delete);
routes.get('/incidents', celebrate({
[Segments.QUERY]: Joi.object().keys({
page: Joi.number(),
})
}),IncidentsController.index);

routes.post('/incidents', celebrate({
[Segments.HEADERS]: Joi.object({
authorization: Joi.string().required(),
}).unknown(),
[Segments.BODY]: Joi.object().keys({
title: Joi.string().required(),
description: Joi.string().required(),
value: Joi.number().required()
})
}), IncidentsController.create);

routes.delete('/incidents/:id', celebrate({
[Segments.PARAMS]: Joi.object().keys({
id: Joi.number().required(),
})
}), IncidentsController.delete);

module.exports = routes;

0 comments on commit 2fb87d2

Please sign in to comment.