Skip to content

Commit

Permalink
feat(produtos): criar rota de busca de produto por ID
Browse files Browse the repository at this point in the history
closes #211
  • Loading branch information
PauloGoncalvesBH committed Mar 17, 2021
1 parent 8f2632d commit 938da13
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 16 deletions.
13 changes: 11 additions & 2 deletions docs/agilizei.html

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions docs/localhost.html

Large diffs are not rendered by default.

15 changes: 12 additions & 3 deletions docs/serverest.dev.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test": "cross-env NODE_ENV=serverest-test nyc mocha --config test/.mocharc.js",
"lint": "standard",
"lint:fix": "standard --fix",
"generate-doc": "npx aglio@2 -i serverest.apib --theme-variables flatly --theme-template triple -o docs/index.html"
"generate-doc": "npx aglio -i serverest.apib --theme-variables flatly --theme-template triple -o docs/serverest.dev.html"
},
"husky": {
"hooks": {
Expand Down
9 changes: 9 additions & 0 deletions src/controllers/produtos-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ exports.get = async (req, res) => {
res.status(200).send({ quantidade: produtos.length, produtos })
}

exports.getOne = async (req, res) => {
const { id } = req.params
const produto = await service.getOne(id)
if (!produto) {
return res.status(400).json({ message: constant.PRODUTO_NAO_ENCONTRADO })
}
return res.status(200).send(produto)
}

exports.post = async (req, res) => {
if (await service.existeProduto({ nome: req.body.nome.trim() })) {
return res.status(400).send({ message: constant.NOME_JA_USADO })
Expand Down
6 changes: 6 additions & 0 deletions src/models/produtos-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ exports.schemaGet = {
})
}

exports.schemaGetOne = {
params: Joi.object({
id: Joi.string().required()
})
}

exports.schemaPost = {
body: Joi.object({
nome: Joi.string().required(),
Expand Down
1 change: 1 addition & 0 deletions src/routes/produtos-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const validateSchema = require('../services/validateSchema-service')

const router = express.Router()
router.get('/', validateSchema(model.schemaGet), controller.get)
router.get('/:id', validateSchema(model.schemaGetOne), controller.getOne)
router.post('/', authentication.checkAdm, validateSchema(model.schemaPost), controller.post)
router.delete('/:id', authentication.checkAdm, validateSchema(model.schemaDelete), controller.delete)
router.put('/:id', authentication.checkAdm, validateSchema(model.schemaPut), controller.put)
Expand Down
10 changes: 10 additions & 0 deletions src/services/produtos-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ exports.getAll = queryString => {
})
}

exports.getOne = id => {
return new Promise((resolve, reject) => {
datastore.findOne({ _id: id }, (err, produto) => {
/* istanbul ignore if */
if (err) reject(err)
resolve(produto)
})
})
}

exports.getDadosDoProduto = queryString => {
return new Promise((resolve, reject) => {
datastore.findOne(queryString, (err, resultado) => {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
SEM_CARRINHO: 'Não foi encontrado carrinho para esse usuário',
ESTOQUE_REABASTECIDO: 'Estoque dos produtos reabastecido',
USUARIO_NAO_ENCONTRADO: 'Usuário não encontrado',
CARRINHO_NAO_ENCONTRADO: 'Carrinho não encontrado'
CARRINHO_NAO_ENCONTRADO: 'Carrinho não encontrado',
PRODUTO_NAO_ENCONTRADO: 'Produto não encontrado'
}
23 changes: 23 additions & 0 deletions test/produtos/getOne.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const chai = require('chai')

const rotaProdutos = '/produtos'
const utils = require('../utils')

describe(`${rotaProdutos}/:id GET`, () => {
it('Busca um produto específico', async () => {
const { email, password } = await utils.cadastrarUsuario({ administrador: 'true' })
const { authorization } = await utils.login(email, password)
const produto = await utils.cadastrarProduto({ authorization })

const { body } = await request.get(`${rotaProdutos}/${produto._id}`).expect(200)

chai.assert.deepEqual(body, produto)
})

it('Nenhum produto encontrado', async () => {
const { body } = await request.get(`${rotaProdutos}/123`).expect(400)
chai.assert.deepEqual(body, {
message: 'Produto não encontrado'
})
})
})
8 changes: 1 addition & 7 deletions test/usuarios/getOne.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ const utils = require('../utils')
describe(`${rotaUsuarios}/:id GET`, () => {
it('Param - Busca um usuário', async () => {
const usuario = await utils.cadastrarUsuario()
const { body } = await request.get(`${rotaUsuarios}/${usuario._id}`).query({
nome: usuario.nome,
email: usuario.email,
password: usuario.password,
administrador: usuario.administrador,
_id: usuario._id
}).expect(200)
const { body } = await request.get(`${rotaUsuarios}/${usuario._id}`).expect(200)

chai.assert.deepEqual(body, {
nome: usuario.nome,
Expand Down

0 comments on commit 938da13

Please sign in to comment.