-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c94083
commit d929565
Showing
9 changed files
with
263 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const request = require("supertest") | ||
const app = require("../../src/app") | ||
const truncate = require("../utils/truncate") | ||
const factory = require("../utils/factories") | ||
const faker = require("faker") | ||
|
||
faker.locale = "pt_BR" | ||
|
||
describe("CRUD", () => { | ||
beforeEach(async () => { | ||
await truncate() | ||
}) | ||
|
||
it("deve cadastrar um novo servico", async () => { | ||
const tipoServico = await factory.create('TipoServico') | ||
|
||
const resposta = await request(app) | ||
.post("/servico") | ||
.send({ | ||
tipo_servico_id: tipoServico.id, | ||
valor: faker.finance.amount() | ||
}) | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
|
||
it("não deve cadastrar com dados obrigatorios faltando", async () => { | ||
const resposta = await request(app) | ||
.post("/servico") | ||
.send({}) | ||
|
||
expect(resposta.status).toBe(400) | ||
}) | ||
|
||
it("deve editar um servico já criado", async () => { | ||
const servico = await factory.create("Servico") | ||
|
||
const resposta = await request(app) | ||
.put(`/servico/${servico.id}`) | ||
.send({ | ||
valor: faker.finance.amount() | ||
}) | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
|
||
it("deve deletar um servico já criado", async () => { | ||
const servico = await factory.create("Servico") | ||
|
||
const resposta = await request(app) | ||
.delete(`/servico/${servico.id}`) | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
|
||
it("deve buscar todos os servico", async()=> { | ||
await factory.create("Servico") | ||
await factory.create("Servico") | ||
|
||
const resposta = await request(app) | ||
.get("/servicos") | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
|
||
it("deve buscar os dados de um servico", async()=> { | ||
const servico = await factory.create("Servico") | ||
|
||
const resposta = await request(app) | ||
.get(`/servico/${servico.id}`) | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const {Servico} = require("../models"); | ||
|
||
class ServicoController{ | ||
async cadastrar(req, res) { | ||
let {valor, tipo_servico_id} = req.body | ||
|
||
if (!valor || !tipo_servico_id) | ||
return res.status(400).json({message: "Dados obrigatorios faltando"}) | ||
|
||
let servico = await Servico.create({valor, tipo_servico_id}) | ||
|
||
return res.status(200).json({servico: servico}) | ||
} | ||
|
||
async editar(req, res) { | ||
let {valor, tipo_servico_id} = req.body | ||
let {id} = req.params | ||
|
||
let servico = await Servico.findOne({where: {id}}) | ||
|
||
await servico.update({valor, tipo_servico_id}) | ||
|
||
return res.status(200).json({servico: servico}) | ||
} | ||
|
||
async deletar(req, res) { | ||
let {id} = req.params | ||
|
||
let servico = await Servico.findOne({where: {id}}) | ||
|
||
await servico.destroy() | ||
|
||
return res.status(200).json({servico: servico}) | ||
} | ||
|
||
async buscarTodos(req, res) { | ||
let servicos = await Servico.findAll() | ||
|
||
return res.status(200).json({servicos: servicos}) | ||
} | ||
|
||
async buscar(req, res) { | ||
let {id} = req.params | ||
|
||
let servico = await Servico.findOne({where: {id}}) | ||
|
||
return res.status(200).json({ servico: servico}) | ||
} | ||
} | ||
|
||
module.exports = new ServicoController() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
return queryInterface.createTable("TipoServico", { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true | ||
}, | ||
descricao: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
created_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
}, | ||
updated_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
}, | ||
}) | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable("TipoServico") | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
return queryInterface.createTable("Servico", { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true | ||
}, | ||
valor: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
}, | ||
tipo_servico_id:{ | ||
type: Sequelize.INTEGER, | ||
references: {model: "TipoServico", key: "id"}, | ||
onDelete: "CASCADE", | ||
allowNull: false | ||
}, | ||
created_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
}, | ||
updated_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false | ||
}, | ||
}) | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable("Servico") | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Servico = sequelize.define("Servico", { | ||
valor: DataTypes.STRING, | ||
}, | ||
{ | ||
freezeTableName: true, | ||
} | ||
) | ||
|
||
Servico.associate = models => { | ||
Servico.belongsTo(models.TipoServico, { | ||
foreignKey: 'tipo_servico_id', | ||
}) | ||
} | ||
|
||
return Servico | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const TipoServico = sequelize.define("TipoServico", { | ||
descricao: DataTypes.STRING, | ||
}, | ||
{ | ||
freezeTableName: true, | ||
} | ||
) | ||
|
||
return TipoServico | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const routes = require("express").Router() | ||
const ServicoController = require("../controllers/ServicoController") | ||
|
||
routes.post('/servico', ServicoController.cadastrar) | ||
|
||
routes.put("/servico/:id", ServicoController.editar) | ||
|
||
routes.delete("/servico/:id", ServicoController.deletar) | ||
|
||
routes.get("/servicos", ServicoController.buscarTodos) | ||
|
||
routes.get("/servico/:id", ServicoController.buscar) | ||
|
||
module.exports = routes |