-
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
ebd8068
commit 28de35b
Showing
7 changed files
with
196 additions
and
3 deletions.
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 plano", async () => { | ||
|
||
const resposta = await request(app) | ||
.post("/plano") | ||
.send({ | ||
descricao: faker.company.bs(), | ||
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("/plano") | ||
.send({}) | ||
|
||
expect(resposta.status).toBe(400) | ||
}) | ||
|
||
it("deve editar um plano já criado", async () => { | ||
const plano = await factory.create("Plano") | ||
|
||
const resposta = await request(app) | ||
.put(`/plano/${plano.id}`) | ||
.send({ | ||
descricao: faker.company.bs(), | ||
valor: faker.finance.amount() | ||
}) | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
|
||
it("deve deletar um plano já criado", async () => { | ||
const plano = await factory.create("Plano") | ||
|
||
const resposta = await request(app) | ||
.delete(`/plano/${plano.id}`) | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
|
||
it("deve buscar todos os planos", async()=> { | ||
await factory.create("Plano") | ||
await factory.create("Plano") | ||
|
||
const resposta = await request(app) | ||
.get("/planos") | ||
|
||
expect(resposta.status).toBe(200) | ||
}) | ||
|
||
it("deve buscar os dados de um plano", async()=> { | ||
const plano = await factory.create("Plano") | ||
|
||
const resposta = await request(app) | ||
.get(`/plano/${plano.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 {Plano} = require("../models"); | ||
|
||
class PlanoController { | ||
async cadastrar(req, res) { | ||
let {descricao, valor} = req.body | ||
|
||
if (!descricao || !valor) | ||
return res.status(400).json({message: "Dados obrigatorios faltando"}) | ||
|
||
let plano = await Plano.create({descricao, valor}) | ||
|
||
return res.status(200).json({plano: plano}) | ||
} | ||
|
||
async editar(req, res) { | ||
let {descricao, valor} = req.body | ||
let {id} = req.params | ||
|
||
let plano = await Plano.findOne({where: {id}}) | ||
|
||
await plano.update({descricao, valor}) | ||
|
||
return res.status(200).json({plano: plano}) | ||
} | ||
|
||
async deletar(req, res) { | ||
let {id} = req.params | ||
|
||
let plano = await Plano.findOne({where: {id}}) | ||
|
||
await plano.destroy() | ||
|
||
return res.status(200).json({plano: plano}) | ||
} | ||
|
||
async buscarTodos(req, res) { | ||
let planos = await Plano.findAll() | ||
|
||
return res.status(200).json({planos: planos}) | ||
} | ||
|
||
async buscar(req, res) { | ||
let {id} = req.params | ||
|
||
let plano = await Plano.findOne({where: {id}}) | ||
|
||
return res.status(200).json({plano: plano}) | ||
} | ||
} | ||
|
||
module.exports = new PlanoController() |
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,34 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
return queryInterface.createTable("Plano", { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true | ||
}, | ||
descricao:{ | ||
type: Sequelize.STRING, | ||
allowNull:false, | ||
}, | ||
valor:{ | ||
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('Plano') | ||
} | ||
}; |
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,12 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Plano = sequelize.define("Plano", { | ||
descricao: DataTypes.STRING, | ||
valor: DataTypes.STRING, | ||
}, | ||
{ | ||
freezeTableName: true, | ||
} | ||
) | ||
|
||
return Plano | ||
} |
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 PlanoController = require("../controllers/PlanoController") | ||
|
||
routes.post('/plano', PlanoController.cadastrar) | ||
|
||
routes.put("/plano/:id", PlanoController.editar) | ||
|
||
routes.delete("/plano/:id", PlanoController.deletar) | ||
|
||
routes.get("/planos", PlanoController.buscarTodos) | ||
|
||
routes.get("/plano/:id", PlanoController.buscar) | ||
|
||
module.exports = routes |