From 11554fe9cc74f70c3bc348b490df645d5f25cdc9 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 21 Sep 2021 19:23:50 -0300 Subject: [PATCH 01/21] fix: migrations for relationship Co-authored-by: isadoragalvaoss --- ...-Tronco.js => 20210905150630-createTable-Tronco.js} | 0 ...-Lingua.js => 20210905150640-createTable-Lingua.js} | 10 ++++++++++ ...ade.js => 20210905150650-createTable-Localidade.js} | 0 3 files changed, 10 insertions(+) rename src/database/migrations/{20210905150716-createTable-Tronco.js => 20210905150630-createTable-Tronco.js} (100%) rename src/database/migrations/{20210905150615-createTable-Lingua.js => 20210905150640-createTable-Lingua.js} (78%) rename src/database/migrations/{20210905150616-createTable-Localidade.js => 20210905150650-createTable-Localidade.js} (100%) diff --git a/src/database/migrations/20210905150716-createTable-Tronco.js b/src/database/migrations/20210905150630-createTable-Tronco.js similarity index 100% rename from src/database/migrations/20210905150716-createTable-Tronco.js rename to src/database/migrations/20210905150630-createTable-Tronco.js diff --git a/src/database/migrations/20210905150615-createTable-Lingua.js b/src/database/migrations/20210905150640-createTable-Lingua.js similarity index 78% rename from src/database/migrations/20210905150615-createTable-Lingua.js rename to src/database/migrations/20210905150640-createTable-Lingua.js index 4917201..e377456 100644 --- a/src/database/migrations/20210905150615-createTable-Lingua.js +++ b/src/database/migrations/20210905150640-createTable-Lingua.js @@ -21,6 +21,16 @@ module.exports = { onUpdate: "CASCADE", onDelete: "CASCADE", }, + id_tronco: { + type: Sequelize.DataTypes.INTEGER, + allowNull: true, + references: { + model: "Tronco", + key: "id_tronco", + }, + onUpdate: "RESTRICT", + onDelete: "RESTRICT", + }, nome: { type: Sequelize.DataTypes.STRING, allowNull: false, diff --git a/src/database/migrations/20210905150616-createTable-Localidade.js b/src/database/migrations/20210905150650-createTable-Localidade.js similarity index 100% rename from src/database/migrations/20210905150616-createTable-Localidade.js rename to src/database/migrations/20210905150650-createTable-Localidade.js From 09b0b5ca20025fbdc37737820ca597b6451090d2 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 21 Sep 2021 21:09:03 -0300 Subject: [PATCH 02/21] fix: add id_tronco in create lingua method Co-authored-by: isadoragalvaoss --- src/controllers/Lingua/createLingua.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/controllers/Lingua/createLingua.js b/src/controllers/Lingua/createLingua.js index 1227d77..75229ae 100644 --- a/src/controllers/Lingua/createLingua.js +++ b/src/controllers/Lingua/createLingua.js @@ -2,9 +2,9 @@ import Lingua from "../../models/Lingua"; import { HttpException } from "../../error/HttpException"; export async function create(request, response) { - const { nome } = request.body; - if (!nome) { - throw new HttpException(400, `Nome inválido - Lingua - ${nome}`); + const { nome, id_tronco } = request.body; + if (!nome || !id_tronco) { + throw new HttpException(400, `Credenciais inválido - Lingua - ${nome}`); } const nameAlreadyExists = await Lingua.searchByName(nome); @@ -14,6 +14,7 @@ export async function create(request, response) { const lingua = await Lingua.create({ nome, + id_tronco, }); response.send(lingua); From 794e7f91c53b187e95db95ab94923f78fd3f031f Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 21 Sep 2021 21:11:26 -0300 Subject: [PATCH 03/21] feat: relationship lingua with tronco Co-authored-by: isadoragalvaoss --- src/models/Lingua/Lingua.js | 26 +++++++++++++++++++++- src/models/Tronco/Tronco.js | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/models/Tronco/Tronco.js diff --git a/src/models/Lingua/Lingua.js b/src/models/Lingua/Lingua.js index ac56357..1a540df 100644 --- a/src/models/Lingua/Lingua.js +++ b/src/models/Lingua/Lingua.js @@ -1,5 +1,6 @@ const databaseConfig = require("../../config/database"); const Conteudo = require("../Conteudo/Conteudo"); +const Tronco = require("../Tronco/Tronco"); const { Sequelize, DataTypes } = require("sequelize"); const sequelize = new Sequelize(databaseConfig); @@ -13,6 +14,16 @@ const Lingua = sequelize.define( primaryKey: true, allowNull: false, }, + id_tronco: { + type: Sequelize.DataTypes.INTEGER, + allowNull: true, + references: { + model: "Tronco", + key: "id_tronco", + }, + onUpdate: "RESTRICT", + onDelete: "RESTRICT", + }, id_conteudo: { type: DataTypes.INTEGER, allowNull: false, @@ -34,5 +45,18 @@ Lingua.hasOne(Conteudo, { onUpdate: "CASCADE", sourceKey: "id_conteudo", }); - +Lingua.hasOne(Tronco, { + foreignKey: "id_tronco", + onDelete: "RESTRICT", + onUpdate: "RESTRICT", + sourceKey: "id_tronco", + as: "tronco", +}); +Tronco.hasMany(Lingua, { + foreignKey: "id_tronco", + onDelete: "RESTRICT", + onUpdate: "RESTRICT", + sourceKey: "id_tronco", + as: "linguas", +}); module.exports = Lingua; diff --git a/src/models/Tronco/Tronco.js b/src/models/Tronco/Tronco.js new file mode 100644 index 0000000..df01652 --- /dev/null +++ b/src/models/Tronco/Tronco.js @@ -0,0 +1,44 @@ +const databaseConfig = require("../../config/database"); +const { Sequelize, DataTypes } = require("sequelize"); +const sequelize = new Sequelize(databaseConfig); +const Conteudo = require("../Conteudo/Conteudo"); +const Lingua = require("../Lingua/Lingua"); + +const Tronco = sequelize.define( + "Tronco", + { + id_tronco: { + type: DataTypes.INTEGER, + autoIncrement: true, + primaryKey: true, + allowNull: false, + }, + id_conteudo: { + type: DataTypes.INTEGER, + allowNull: false, + references: { + model: "Conteudo", + key: "id_conteudo", + }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, + nome: { + type: DataTypes.STRING, + allowNull: false, + }, + }, + { + tableName: "Tronco", + timestamps: false, + } +); + +Tronco.hasOne(Conteudo, { + foreignKey: "id_conteudo", + onDelete: "CASCADE", + onUpdate: "CASCADE", + sourceKey: "id_conteudo", +}); + +module.exports = Tronco; From 44be1454238a228eeeb3692fae13749dbb723799 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 22 Sep 2021 16:27:14 -0300 Subject: [PATCH 04/21] feat: CRUD tronco with relationship Co-authored-by: isadoragalvaoss --- src/controllers/Tronco/createTronco.js | 20 +++++++++ src/controllers/Tronco/deleteTronco.js | 23 ++++++++++ src/controllers/Tronco/getAllTronco.js | 11 +++++ src/controllers/Tronco/getOneTronco.js | 20 +++++++++ src/controllers/Tronco/index.js | 5 +++ src/controllers/Tronco/updateTronco.js | 27 ++++++++++++ src/models/Tronco/Tronco.js | 1 - src/models/Tronco/index.js | 61 ++++++++++++++++++++++++++ src/routes/Tronco/tronco.routes.js | 20 +++++++++ src/routes/index.js | 2 + 10 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 src/controllers/Tronco/createTronco.js create mode 100644 src/controllers/Tronco/deleteTronco.js create mode 100644 src/controllers/Tronco/getAllTronco.js create mode 100644 src/controllers/Tronco/getOneTronco.js create mode 100644 src/controllers/Tronco/index.js create mode 100644 src/controllers/Tronco/updateTronco.js create mode 100644 src/models/Tronco/index.js create mode 100644 src/routes/Tronco/tronco.routes.js diff --git a/src/controllers/Tronco/createTronco.js b/src/controllers/Tronco/createTronco.js new file mode 100644 index 0000000..4c29140 --- /dev/null +++ b/src/controllers/Tronco/createTronco.js @@ -0,0 +1,20 @@ +import Tronco from "../../models/Tronco"; +import { HttpException } from "../../error/HttpException"; + +export async function create(request, response) { + const { nome } = request.body; + if (!nome) { + throw new HttpException(400, `Nome inválido - Tronco - ${nome}`); + } + + const nameAlreadyExists = await Tronco.searchByName(nome); + if (nameAlreadyExists) { + throw new HttpException(400, `Nome já existente - Tronco - ${nome}`); + } + + const tronco = await Tronco.create({ + nome, + }); + + response.send(tronco); +} diff --git a/src/controllers/Tronco/deleteTronco.js b/src/controllers/Tronco/deleteTronco.js new file mode 100644 index 0000000..7075875 --- /dev/null +++ b/src/controllers/Tronco/deleteTronco.js @@ -0,0 +1,23 @@ +import Tronco from "../../models/Tronco"; +import { HttpException } from "../../error/HttpException"; +import Conteudo from "../../models/Conteudo"; + +export async function deleteOne(request, response) { + const { id_tronco } = request.params; + if (!id_tronco) { + throw new HttpException(400, `ID inválido - Tronco - ID ${id_tronco}`); + } + + const troncoFound = await Tronco.searchById(id_tronco); + if (!troncoFound) { + throw new HttpException( + 404, + `Tronco não encontrada - Tronco - ID ${id_tronco}` + ); + } + Conteudo.delete(troncoFound.id_conteudo); + + response.send({ + Result: "Tronco deletado com sucesso", + }); +} diff --git a/src/controllers/Tronco/getAllTronco.js b/src/controllers/Tronco/getAllTronco.js new file mode 100644 index 0000000..b179bcd --- /dev/null +++ b/src/controllers/Tronco/getAllTronco.js @@ -0,0 +1,11 @@ +import Tronco from "../../models/Tronco"; +import { HttpException } from "../../error/HttpException"; + +export async function getAll(request, response) { + const etniasEncontrada = await Tronco.getAll(); + if (!etniasEncontrada.length) { + throw new HttpException(404, "Nenhum tronco encontrado - Tronco"); + } + + response.send(etniasEncontrada); +} diff --git a/src/controllers/Tronco/getOneTronco.js b/src/controllers/Tronco/getOneTronco.js new file mode 100644 index 0000000..15c65fe --- /dev/null +++ b/src/controllers/Tronco/getOneTronco.js @@ -0,0 +1,20 @@ +import Tronco from "../../models/Tronco"; +import { HttpException } from "../../error/HttpException"; + +export async function getOne(request, response) { + const { id_tronco } = request.params; + + if (!id_tronco) { + throw new HttpException(400, `ID inválido - Tronco - ID ${id_tronco}`); + } + + const troncoFound = await Tronco.searchById(id_tronco); + if (!troncoFound) { + throw new HttpException( + 404, + `Tronco não encontrado - Tronco - ID ${id_tronco}` + ); + } + + response.send(troncoFound); +} diff --git a/src/controllers/Tronco/index.js b/src/controllers/Tronco/index.js new file mode 100644 index 0000000..c02ad49 --- /dev/null +++ b/src/controllers/Tronco/index.js @@ -0,0 +1,5 @@ +export { create } from "./createTronco"; +export { getOne } from "./getOneTronco"; +export { getAll } from "./getAllTronco"; +export { update } from "./updateTronco"; +export { deleteOne } from "./deleteTronco"; diff --git a/src/controllers/Tronco/updateTronco.js b/src/controllers/Tronco/updateTronco.js new file mode 100644 index 0000000..3344f26 --- /dev/null +++ b/src/controllers/Tronco/updateTronco.js @@ -0,0 +1,27 @@ +import Tronco from "../../models/Tronco"; +import { HttpException } from "../../error/HttpException"; + +export async function update(request, response) { + const { nome } = request.body; + if (!nome) { + throw new HttpException(400, "Nome inválido - Tronco"); + } + + const nameAlreadyExists = await Tronco.searchByName(nome); + if (nameAlreadyExists) { + throw new HttpException(400, `Nome já existente - Tronco - ${nome}`); + } + + const { id_tronco } = request.params; + const idValido = await Tronco.searchById(id_tronco); + + if (!id_tronco || !idValido) { + throw new HttpException(400, `ID inválido - Tronco - ID ${id_tronco}`); + } + + await Tronco.editById({ nome }, id_tronco); + + const tronco = await Tronco.searchById(id_tronco); + + response.send(tronco); +} diff --git a/src/models/Tronco/Tronco.js b/src/models/Tronco/Tronco.js index df01652..6b427e3 100644 --- a/src/models/Tronco/Tronco.js +++ b/src/models/Tronco/Tronco.js @@ -2,7 +2,6 @@ const databaseConfig = require("../../config/database"); const { Sequelize, DataTypes } = require("sequelize"); const sequelize = new Sequelize(databaseConfig); const Conteudo = require("../Conteudo/Conteudo"); -const Lingua = require("../Lingua/Lingua"); const Tronco = sequelize.define( "Tronco", diff --git a/src/models/Tronco/index.js b/src/models/Tronco/index.js new file mode 100644 index 0000000..33365c1 --- /dev/null +++ b/src/models/Tronco/index.js @@ -0,0 +1,61 @@ +const TroncoModel = require("./Tronco"); +const LinguaModel = require("../Lingua/Lingua"); +const Conteudo = require("../Conteudo"); + +exports.getAll = async () => { + return TroncoModel.findAll({ + attributes: ["id_tronco", "id_conteudo", "nome"], + include: [ + { + model: LinguaModel, + as: "linguas", + attributes: ["id_lingua", "id_conteudo", "nome"], + }, + ], + }); +}; + +exports.getAllLang = async (idTronco) => { + return LinguaModel.findAll({ + where: { + id_tronco: idTronco, + }, + }); +}; + +exports.searchByName = async (nome) => { + return TroncoModel.findOne({ + where: { + nome, + }, + }); +}; +exports.create = async (body) => { + const conteudoCreated = await Conteudo.create(); + body.id_conteudo = conteudoCreated.id_conteudo; + return TroncoModel.create(body); +}; +exports.searchById = async (idTronco) => { + return TroncoModel.findOne({ + where: { + id_tronco: idTronco, + }, + include: [ + { + model: LinguaModel, + as: "linguas", + attributes: ["id_lingua", "id_conteudo", "nome"], + }, + ], + }); +}; +exports.searchAll = async () => { + return TroncoModel.findAll(); +}; +exports.editById = async (body, idTronco) => { + return TroncoModel.update(body, { + where: { + id_tronco: idTronco, + }, + }); +}; diff --git a/src/routes/Tronco/tronco.routes.js b/src/routes/Tronco/tronco.routes.js new file mode 100644 index 0000000..05f181d --- /dev/null +++ b/src/routes/Tronco/tronco.routes.js @@ -0,0 +1,20 @@ +import { Router } from "express"; +const router = Router(); + +import { + getOne, + create, + getAll, + update, + deleteOne, +} from "../../controllers/Tronco"; + +const idTronco = "/:id_tronco"; + +router.get(idTronco, getOne); +router.get("/", getAll); +router.post("/", create); +router.put(idTronco, update); +router.delete(idTronco, deleteOne); + +export default router; diff --git a/src/routes/index.js b/src/routes/index.js index e78c98b..ded9be7 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,9 +1,11 @@ import etniaRouters from "./Etnia/etnia.routes"; import palavraRouters from "./Palavra/palavra.routes"; import linguaRouters from "./Lingua/lingua.routes"; +import troncoRouters from "./Tronco/tronco.routes"; export function setUpRoutes(app) { app.use("/etnia", etniaRouters); app.use("/palavra", palavraRouters); app.use("/lingua", linguaRouters); + app.use("/tronco", troncoRouters); } From 1d1730aa02f5fe66d334e9745ca219763e063987 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 22 Sep 2021 16:29:57 -0300 Subject: [PATCH 05/21] fix: the tronco can set be null Co-authored-by: isadoragalvaoss --- src/controllers/Lingua/createLingua.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/Lingua/createLingua.js b/src/controllers/Lingua/createLingua.js index 75229ae..dccad26 100644 --- a/src/controllers/Lingua/createLingua.js +++ b/src/controllers/Lingua/createLingua.js @@ -3,7 +3,7 @@ import { HttpException } from "../../error/HttpException"; export async function create(request, response) { const { nome, id_tronco } = request.body; - if (!nome || !id_tronco) { + if (!nome) { throw new HttpException(400, `Credenciais inválido - Lingua - ${nome}`); } From d7fa8f47983125ff9451073f8aeadf89aaf32e94 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 22 Sep 2021 16:35:24 -0300 Subject: [PATCH 06/21] fix: update id_tronco to null if deleted Co-authored-by: isadoragalvaoss --- .../migrations/20210905150640-createTable-Lingua.js | 4 ++-- src/models/Lingua/Lingua.js | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/database/migrations/20210905150640-createTable-Lingua.js b/src/database/migrations/20210905150640-createTable-Lingua.js index e377456..9f526b4 100644 --- a/src/database/migrations/20210905150640-createTable-Lingua.js +++ b/src/database/migrations/20210905150640-createTable-Lingua.js @@ -28,8 +28,8 @@ module.exports = { model: "Tronco", key: "id_tronco", }, - onUpdate: "RESTRICT", - onDelete: "RESTRICT", + onUpdate: "SET NULL", + onDelete: "SET NULL", }, nome: { type: Sequelize.DataTypes.STRING, diff --git a/src/models/Lingua/Lingua.js b/src/models/Lingua/Lingua.js index 1a540df..4f99eff 100644 --- a/src/models/Lingua/Lingua.js +++ b/src/models/Lingua/Lingua.js @@ -21,8 +21,8 @@ const Lingua = sequelize.define( model: "Tronco", key: "id_tronco", }, - onUpdate: "RESTRICT", - onDelete: "RESTRICT", + onUpdate: "SET NULL", + onDelete: "SET NULL", }, id_conteudo: { type: DataTypes.INTEGER, @@ -47,15 +47,15 @@ Lingua.hasOne(Conteudo, { }); Lingua.hasOne(Tronco, { foreignKey: "id_tronco", - onDelete: "RESTRICT", - onUpdate: "RESTRICT", + onDelete: "SET NULL", + onUpdate: "SET NULL", sourceKey: "id_tronco", as: "tronco", }); Tronco.hasMany(Lingua, { foreignKey: "id_tronco", - onDelete: "RESTRICT", - onUpdate: "RESTRICT", + onDelete: "SET NULL", + onUpdate: "SET NULL", sourceKey: "id_tronco", as: "linguas", }); From a2cce7fc2d48486b409c0637a884090fcecf2d25 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 22 Sep 2021 16:42:07 -0300 Subject: [PATCH 07/21] test: cleaning database of tests Co-authored-by: isadoragalvaoss --- database.sqlite | Bin 40960 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/database.sqlite b/database.sqlite index a647e019a172d18c6f2acc37bdbf126d2f61a670..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 GIT binary patch literal 0 HcmV?d00001 literal 40960 zcmeI)&u`mg7zc3w$d9_Td%0OtiS$m0rS31;Ey-vF5+QXVT9S5&vysEpyiMH7kUCrA z0OCYjF5GtK2nR0k4@?{o{{rH&6VfjH0Z2RY+Odn8!tT2)o?^35Po({mjb zJSg!rgSIeB+YcJ|`Tw8f1({2$OH1PAAV~6nb=hZUEPR@aCmObfsXiEnzCy|J^WAq& zT<6$nGylq(a@vnmlh=3IaZd(p_?U`&3>!m^ZV>khX=vxUWLnK+#EbWWQy&eb=s6Kj zHfj#j9gVaX8qj@@d4qn7TIR=m5Jv-f*W4>K$`;K&u-%T$$9tBap$_xdVSjLR>h$}J zpCo_4TBV9P+Dto0q}0__u^IHb|FFvo$-}<=aA3P_`^suEmcx}7mtA&hukkH4^6Y#o zomOw$5HE~iujO8wb)63D*yE*iY+tSM3(*BI+UCVBf^N?}alAoi+WQZ;=rZql5!ygZ ztJ$o0t_7X0`vzU-OS@uwEVd%WyB>SZ@4XK(WjN4AwRERpPQG1h+fqucWJST@XYGp+ zl?}XJuzncZ`=JqAmH7wCod2P+$mbN97s&7ACvriWWRtI8f&c^{009U<00Izz00bZa z0SL^jz|EA@*wi--eOu2NIsKNAUvHn;%(E^3?&|vR9_q#?{kM`*eKJnJ6^jdQLh`U| zLb^8*me0j^7v93-fh)3fe=_jOamTkeci&8VT&$nZ7(Jdq% z=@>WF2tWV=5P$##AOHafKmY=>D6lLy#EEiwYU!WK%Jo>_ zl-fV3$@eDXrWgN-HTixl@_E(2e5IdL%YR9eo61B&d1Gq*?|=UP;N0SG_<0uX=z1Rwwb2tWV=5SV>|sQxEU$ Date: Wed, 22 Sep 2021 16:49:49 -0300 Subject: [PATCH 08/21] test: add id_tronco in the lingua test Co-authored-by: isadoragalvaoss --- src/__tests__/units/Lingua/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/__tests__/units/Lingua/index.js b/src/__tests__/units/Lingua/index.js index 5a08563..192f000 100644 --- a/src/__tests__/units/Lingua/index.js +++ b/src/__tests__/units/Lingua/index.js @@ -37,6 +37,7 @@ describe("\n## TESTES LINGUA\n", () => { expect(lingua.dataValues).toEqual({ id_conteudo: 3, id_lingua: 1, + id_tronco: null, nome: "Aikanã", }); }); @@ -46,6 +47,7 @@ describe("\n## TESTES LINGUA\n", () => { expect(lingua.dataValues).toEqual({ id_conteudo: 4, id_lingua: 2, + id_tronco: null, nome: "Aikewara", }); }); From bcf1d26f3c85069228232bb9c2bc804a51cae803 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sun, 26 Sep 2021 11:46:53 -0300 Subject: [PATCH 09/21] feat: implemented method get trunk by id_lingua Co-authored-by: isadoragalvaoss --- src/controllers/Tronco/getTroncoByLang.js | 20 ++++++++++++++++++++ src/controllers/Tronco/index.js | 1 + src/models/Tronco/index.js | 18 +++++++++++------- src/routes/Tronco/tronco.routes.js | 2 ++ 4 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 src/controllers/Tronco/getTroncoByLang.js diff --git a/src/controllers/Tronco/getTroncoByLang.js b/src/controllers/Tronco/getTroncoByLang.js new file mode 100644 index 0000000..4847bab --- /dev/null +++ b/src/controllers/Tronco/getTroncoByLang.js @@ -0,0 +1,20 @@ +import Tronco from "../../models/Tronco"; +import { HttpException } from "../../error/HttpException"; + +export async function getTroncoByLang(request, response) { + const { id_lingua } = request.params; + + if (!id_lingua) { + throw new HttpException(400, `ID inválido - ID Lingua - ${id_lingua}`); + } + + const troncoFound = await Tronco.getTrunkByLanguage(id_lingua); + if (!troncoFound) { + throw new HttpException( + 404, + `Tronco não encontrado - ID Lingua - ${id_lingua}` + ); + } + + response.send(troncoFound); +} diff --git a/src/controllers/Tronco/index.js b/src/controllers/Tronco/index.js index c02ad49..9e7f352 100644 --- a/src/controllers/Tronco/index.js +++ b/src/controllers/Tronco/index.js @@ -3,3 +3,4 @@ export { getOne } from "./getOneTronco"; export { getAll } from "./getAllTronco"; export { update } from "./updateTronco"; export { deleteOne } from "./deleteTronco"; +export { getTroncoByLang } from "./getTroncoByLang"; diff --git a/src/models/Tronco/index.js b/src/models/Tronco/index.js index 33365c1..f091c88 100644 --- a/src/models/Tronco/index.js +++ b/src/models/Tronco/index.js @@ -14,15 +14,19 @@ exports.getAll = async () => { ], }); }; - -exports.getAllLang = async (idTronco) => { - return LinguaModel.findAll({ - where: { - id_tronco: idTronco, - }, +exports.getTrunkByLanguage = async (id_lingua) => { + return LinguaModel.findOne({ + where: { id_lingua: id_lingua }, + attributes: ["id_lingua", "nome"], + include: [ + { + model: TroncoModel, + as: "tronco", + attributes: ["id_tronco", "nome"], + }, + ], }); }; - exports.searchByName = async (nome) => { return TroncoModel.findOne({ where: { diff --git a/src/routes/Tronco/tronco.routes.js b/src/routes/Tronco/tronco.routes.js index 05f181d..2a1f03a 100644 --- a/src/routes/Tronco/tronco.routes.js +++ b/src/routes/Tronco/tronco.routes.js @@ -7,11 +7,13 @@ import { getAll, update, deleteOne, + getTroncoByLang, } from "../../controllers/Tronco"; const idTronco = "/:id_tronco"; router.get(idTronco, getOne); +router.get("/lingua/:id_lingua", getTroncoByLang); router.get("/", getAll); router.post("/", create); router.put(idTronco, update); From ad02327e564b89281bc3eae62d7a6818bd426bcc Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sun, 26 Sep 2021 12:30:32 -0300 Subject: [PATCH 10/21] refactor: update relationship lingua and palavra Co-authored-by: isadoragalvaoss --- src/models/Palavra/Palavra.js | 9 +++++++++ src/models/Palavra/index.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/models/Palavra/Palavra.js b/src/models/Palavra/Palavra.js index f5cfb2b..6f55754 100644 --- a/src/models/Palavra/Palavra.js +++ b/src/models/Palavra/Palavra.js @@ -60,6 +60,15 @@ Palavra.hasOne(Lingua, { onDelete: "CASCADE", onUpdate: "CASCADE", sourceKey: "id_lingua", + as: "lingua", +}); + +Lingua.hasMany(Palavra, { + foreignKey: "id_lingua", + onDelete: "CASCADE", + onUpdate: "CASCADE", + sourceKey: "id_lingua", + as: "palavras", }); module.exports = Palavra; diff --git a/src/models/Palavra/index.js b/src/models/Palavra/index.js index fd44f1e..a68f4c8 100644 --- a/src/models/Palavra/index.js +++ b/src/models/Palavra/index.js @@ -1,4 +1,5 @@ const PalavraModel = require("./Palavra"); +const LinguaModel = require("../Lingua/Lingua"); const Conteudo = require("../Conteudo"); exports.getAll = async () => { @@ -6,6 +7,11 @@ exports.getAll = async () => { raw: true, }); }; +exports.getOne = async (idPalavra) => { + return PalavraModel.findOne({ + where: { id_palavra: idPalavra }, + }); +}; exports.searchByName = async (nome, idLingua) => { return PalavraModel.findOne({ where: { @@ -26,13 +32,29 @@ exports.searchById = async (idPalavra, idLingua) => { id_palavra: idPalavra, id_lingua: idLingua, }, + attributes: ["id_palavra", "id_conteudo", "nome", "significado"], + include: [ + { + model: LinguaModel, + as: "lingua", + attributes: ["id_lingua", "nome"], + }, + ], }); }; exports.searchAll = async (idLingua) => { - return PalavraModel.findAll({ + return LinguaModel.findAll({ where: { id_lingua: idLingua, }, + attributes: ["id_lingua", "id_conteudo", "nome"], + include: [ + { + model: PalavraModel, + as: "palavras", + attributes: ["id_palavra", "id_conteudo", "nome", "significado"], + }, + ], }); }; exports.editById = async (body, idPalavra, idLingua) => { @@ -41,5 +63,13 @@ exports.editById = async (body, idPalavra, idLingua) => { id_palavra: idPalavra, id_lingua: idLingua, }, + attributes: ["id_palavra", "nome", "significado"], + include: [ + { + model: LinguaModel, + as: "lingua", + attributes: ["id_lingua", "nome"], + }, + ], }); }; From 1905a7e60f439f630b4d4ea3e4b5ef8a2e5f5f19 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sun, 26 Sep 2021 14:47:42 -0300 Subject: [PATCH 11/21] test: refactoring tests with methos toEquals Co-authored-by: isadoragalvaoss --- src/__tests__/units/Etnia/index.js | 8 ++++---- src/__tests__/units/Lingua/index.js | 8 ++++---- src/__tests__/units/Palavra/index.js | 24 ++++++++++++++++-------- src/models/Palavra/index.js | 12 +++--------- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/__tests__/units/Etnia/index.js b/src/__tests__/units/Etnia/index.js index 6f73750..bf52ecb 100644 --- a/src/__tests__/units/Etnia/index.js +++ b/src/__tests__/units/Etnia/index.js @@ -15,7 +15,7 @@ describe("\n## TESTES ETNIA\n", () => { describe("Criação de Etnia", () => { it("Criando etnia com o metodo create() - 1", async () => { const etnias = await modelEtnia.create({ nome: "tupi-guarani" }); - expect(etnias.dataValues).toEqual({ + expect(etnias).toMatchObject({ id_conteudo: 1, id_etnia: 1, nome: "tupi-guarani", @@ -23,7 +23,7 @@ describe("\n## TESTES ETNIA\n", () => { }); it("Criando etnia com o metodo create() - 2", async () => { const etnias = await modelEtnia.create({ nome: "tupi" }); - expect(etnias.dataValues).toEqual({ + expect(etnias).toMatchObject({ id_conteudo: 2, id_etnia: 2, nome: "tupi", @@ -34,7 +34,7 @@ describe("\n## TESTES ETNIA\n", () => { it("Atualizando etnia, com o metodo editById(1) na tupla de ID = 1", async () => { await modelEtnia.editById({ nome: "Aikanã" }, 1); const etnia = await modelEtnia.searchById(1); - expect(etnia.dataValues).toEqual({ + expect(etnia).toMatchObject({ id_conteudo: 1, id_etnia: 1, nome: "Aikanã", @@ -43,7 +43,7 @@ describe("\n## TESTES ETNIA\n", () => { it("Atualizando etnia, com o metodo editById(2) na tupla de ID = 2", async () => { await modelEtnia.editById({ nome: "Aikewara" }, 2); const etnia = await modelEtnia.searchById(2); - expect(etnia.dataValues).toEqual({ + expect(etnia).toMatchObject({ id_conteudo: 2, id_etnia: 2, nome: "Aikewara", diff --git a/src/__tests__/units/Lingua/index.js b/src/__tests__/units/Lingua/index.js index 192f000..2317d90 100644 --- a/src/__tests__/units/Lingua/index.js +++ b/src/__tests__/units/Lingua/index.js @@ -15,7 +15,7 @@ describe("\n## TESTES LINGUA\n", () => { describe("Criação de Lingua", () => { it("Criando lingua com o metodo create() - 1", async () => { const linguas = await modelLingua.create({ nome: "tupi-guarani" }); - expect(linguas.dataValues).toEqual({ + expect(linguas).toMatchObject({ id_conteudo: 3, id_lingua: 1, nome: "tupi-guarani", @@ -23,7 +23,7 @@ describe("\n## TESTES LINGUA\n", () => { }); it("Criando lingua com o metodo create() - 2", async () => { const linguas = await modelLingua.create({ nome: "tupi" }); - expect(linguas.dataValues).toEqual({ + expect(linguas).toMatchObject({ id_conteudo: 4, id_lingua: 2, nome: "tupi", @@ -34,7 +34,7 @@ describe("\n## TESTES LINGUA\n", () => { it("Atualizando lingua, com o metodo editById(1) na tupla de ID = 1", async () => { await modelLingua.editById({ nome: "Aikanã" }, 1); const lingua = await modelLingua.searchById(1); - expect(lingua.dataValues).toEqual({ + expect(lingua).toMatchObject({ id_conteudo: 3, id_lingua: 1, id_tronco: null, @@ -44,7 +44,7 @@ describe("\n## TESTES LINGUA\n", () => { it("Atualizando lingua, com o metodo editById(2) na tupla de ID = 2", async () => { await modelLingua.editById({ nome: "Aikewara" }, 2); const lingua = await modelLingua.searchById(2); - expect(lingua.dataValues).toEqual({ + expect(lingua).toMatchObject({ id_conteudo: 4, id_lingua: 2, id_tronco: null, diff --git a/src/__tests__/units/Palavra/index.js b/src/__tests__/units/Palavra/index.js index 684f7ba..a83dfb1 100644 --- a/src/__tests__/units/Palavra/index.js +++ b/src/__tests__/units/Palavra/index.js @@ -7,7 +7,7 @@ describe("\n## TESTES PALAVRA\n", () => { describe("Criação de lingua para cadastrar palavras", () => { it("Criando lingua com o metodo create() - ID 3", async () => { const palavra = await modelLingua.create({ nome: "tupi-guarani1" }); - expect(palavra.dataValues).toEqual({ + expect(palavra).toMatchObject({ id_conteudo: 5, id_lingua: 3, nome: "tupi-guarani1", @@ -17,7 +17,7 @@ describe("\n## TESTES PALAVRA\n", () => { describe("Listagem de Palavra", () => { it("Listando com metodo searchAll() com banco vazio", async () => { const palavra = await modelPalavra.searchAll(3); - expect(palavra.length).toEqual(0); + expect(palavra.length).toEqual(1); }); it("Listando com metodo searchByID(1) com banco vazio", async () => { const lingua = await modelPalavra.searchById(1, 3); @@ -31,7 +31,7 @@ describe("\n## TESTES PALAVRA\n", () => { id_lingua: 3, significado: "Onça", }); - expect(palavra.dataValues).toEqual({ + expect(palavra).toMatchObject({ id_palavra: 1, id_conteudo: 6, nome: "txãwãrã", @@ -45,7 +45,7 @@ describe("\n## TESTES PALAVRA\n", () => { id_lingua: 3, significado: "Pé", }); - expect(palavra.dataValues).toEqual({ + expect(palavra).toMatchObject({ id_palavra: 2, id_conteudo: 7, nome: "par", @@ -65,12 +65,16 @@ describe("\n## TESTES PALAVRA\n", () => { 3 ); const lingua = await modelPalavra.searchById(2, 3); - expect(lingua.dataValues).toEqual({ + expect(lingua).toEqual({ id_palavra: 2, id_conteudo: 7, nome: "teste123", - id_lingua: 3, significado: "Test", + lingua: { + id_conteudo: 5, + id_lingua: 3, + nome: "tupi-guarani1", + }, }); }); it("Atualizando Palavra, com o metodo editById(1,3) na tupla de ID = 3", async () => { @@ -83,12 +87,16 @@ describe("\n## TESTES PALAVRA\n", () => { 3 ); const lingua = await modelPalavra.searchById(1, 3); - expect(lingua.dataValues).toEqual({ + expect(lingua).toEqual({ id_palavra: 1, id_conteudo: 6, nome: "teste1234", - id_lingua: 3, significado: "Test1", + lingua: { + id_conteudo: 5, + id_lingua: 3, + nome: "tupi-guarani1", + }, }); }); }); diff --git a/src/models/Palavra/index.js b/src/models/Palavra/index.js index a68f4c8..e2f0cb2 100644 --- a/src/models/Palavra/index.js +++ b/src/models/Palavra/index.js @@ -28,6 +28,8 @@ exports.create = async (body) => { exports.searchById = async (idPalavra, idLingua) => { return PalavraModel.findOne({ + raw: true, + nest: true, where: { id_palavra: idPalavra, id_lingua: idLingua, @@ -37,7 +39,7 @@ exports.searchById = async (idPalavra, idLingua) => { { model: LinguaModel, as: "lingua", - attributes: ["id_lingua", "nome"], + attributes: ["id_lingua", "id_conteudo", "nome"], }, ], }); @@ -63,13 +65,5 @@ exports.editById = async (body, idPalavra, idLingua) => { id_palavra: idPalavra, id_lingua: idLingua, }, - attributes: ["id_palavra", "nome", "significado"], - include: [ - { - model: LinguaModel, - as: "lingua", - attributes: ["id_lingua", "nome"], - }, - ], }); }; From 1ea0fcfc7f8721baf4c9618d9cc75f1061564bff Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sun, 26 Sep 2021 14:48:32 -0300 Subject: [PATCH 12/21] test: making tests for trunk methods Co-authored-by: isadoragalvaoss --- src/__tests__/units/Tronco/index.js | 136 ++++++++++++++++++++++++++++ src/__tests__/units/index.spec.js | 1 + 2 files changed, 137 insertions(+) create mode 100644 src/__tests__/units/Tronco/index.js diff --git a/src/__tests__/units/Tronco/index.js b/src/__tests__/units/Tronco/index.js new file mode 100644 index 0000000..6b67156 --- /dev/null +++ b/src/__tests__/units/Tronco/index.js @@ -0,0 +1,136 @@ +const modelTronco = require("../../../models/Tronco"); +const modelLingua = require("../../../models/Lingua"); +const modelConteudo = require("../../../models/Conteudo"); +require("../../../database"); + +describe("\n## TESTES TRONCO\n", () => { + describe("Listagem de Tronco", () => { + it("Listando com metodo getAll() com banco vazio", async () => { + const tronco = await modelTronco.getAll(); + expect(tronco.length).toEqual(0); + }); + it("Listando com metodo searchByID(1) com banco vazio", async () => { + const tronco = await modelTronco.searchById(1); + expect(tronco).toEqual(null); + }); + }); + describe("Criação de Tronco", () => { + it("Criando tronco com o metodo create() - 1", async () => { + const tronco = await modelTronco.create({ nome: "Macro-Jê" }); + expect(tronco).toMatchObject({ + id_tronco: 1, + nome: "Macro-Jê", + id_conteudo: 8, + }); + }); + it("Criando tronco com o metodo create() - 2", async () => { + const tronco = await modelTronco.create({ nome: "Macro-Jê1" }); + expect(tronco).toMatchObject({ + id_tronco: 2, + nome: "Macro-Jê1", + id_conteudo: 9, + }); + }); + }); + describe("Atualização de Tronco", () => { + it("Atualizando tronco, com o metodo searchById(1)", async () => { + await modelTronco.editById({ nome: "Macro-Jê12" }, 1); + const tronco = await modelTronco.searchById(1); + expect(tronco).toMatchObject({ + id_tronco: 1, + nome: "Macro-Jê12", + id_conteudo: 8, + linguas: [], + }); + }); + it("Atualizando tronco, com o metodo searchById(2)", async () => { + await modelTronco.editById({ nome: "Macro-Jê123" }, 2); + const tronco = await modelTronco.searchById(2); + expect(tronco).toMatchObject({ + id_tronco: 2, + nome: "Macro-Jê123", + id_conteudo: 9, + linguas: [], + }); + }); + }); + describe("Deleção de Tronco", () => { + it("Deletando Tronco, com o metodo delete(8) através do conteudo", async () => { + await modelConteudo.delete(8); + const tronco = await modelTronco.searchById(1); + expect(tronco).toEqual(null); + }); + }); + describe("Relacionamento Lingua e Tronco", () => { + it("Criando lingua com um tronco - 1", async () => { + const linguas = await modelLingua.create({ + nome: "tupi-guarani", + id_tronco: 2, + }); + expect(linguas).toMatchObject({ + id_conteudo: 10, + id_tronco: 2, + id_lingua: 4, + nome: "tupi-guarani", + }); + }); + it("Listando tronco com relacionamento searchById(2) - 1", async () => { + const tronco = await modelTronco.searchById(2); + expect(tronco).toMatchObject({ + id_tronco: 2, + id_conteudo: 9, + nome: "Macro-Jê123", + linguas: [ + { + id_lingua: 4, + id_conteudo: 10, + nome: "tupi-guarani", + }, + ], + }); + }); + it("Criando lingua com um tronco - 2", async () => { + const linguas = await modelLingua.create({ + nome: "tupi-guarani2", + id_tronco: 2, + }); + expect(linguas).toMatchObject({ + id_conteudo: 11, + id_tronco: 2, + id_lingua: 5, + nome: "tupi-guarani2", + }); + }); + it("Listando tronco com relacionamento searchById(2) - 2", async () => { + const tronco = await modelTronco.searchById(2); + expect(tronco).toMatchObject({ + id_tronco: 2, + id_conteudo: 9, + nome: "Macro-Jê123", + linguas: [ + { + id_lingua: 4, + id_conteudo: 10, + nome: "tupi-guarani", + }, + { + id_lingua: 5, + id_conteudo: 11, + nome: "tupi-guarani2", + }, + ], + }); + }); + it("Resgatando tronco com id da lingua", async () => { + const tronco = await modelTronco.getTrunkByLanguage(4); + expect(tronco).toMatchObject({ + id_lingua: 4, + nome: "tupi-guarani", + tronco: { + id_tronco: 2, + nome: "Macro-Jê123", + }, + }); + }); + }); +}); diff --git a/src/__tests__/units/index.spec.js b/src/__tests__/units/index.spec.js index ebc6696..8b339ea 100644 --- a/src/__tests__/units/index.spec.js +++ b/src/__tests__/units/index.spec.js @@ -1,3 +1,4 @@ require("./Etnia"); require("./Lingua"); require("./Palavra"); +require("./Tronco"); From 47c85e0c2b130ecb7061dd3adb8261ee3129070c Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sun, 26 Sep 2021 15:28:38 -0300 Subject: [PATCH 13/21] fix: code smells sonar Co-authored-by: isadoragalvaoss --- src/__tests__/units/Tronco/index.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/__tests__/units/Tronco/index.js b/src/__tests__/units/Tronco/index.js index 6b67156..371d29e 100644 --- a/src/__tests__/units/Tronco/index.js +++ b/src/__tests__/units/Tronco/index.js @@ -4,6 +4,9 @@ const modelConteudo = require("../../../models/Conteudo"); require("../../../database"); describe("\n## TESTES TRONCO\n", () => { + const troncoName = "Macro-Jê123"; + const linguaNome = ["tupi-guarani", "tupi-guarani2"]; + describe("Listagem de Tronco", () => { it("Listando com metodo getAll() com banco vazio", async () => { const tronco = await modelTronco.getAll(); @@ -44,11 +47,11 @@ describe("\n## TESTES TRONCO\n", () => { }); }); it("Atualizando tronco, com o metodo searchById(2)", async () => { - await modelTronco.editById({ nome: "Macro-Jê123" }, 2); + await modelTronco.editById({ nome: troncoName }, 2); const tronco = await modelTronco.searchById(2); expect(tronco).toMatchObject({ id_tronco: 2, - nome: "Macro-Jê123", + nome: troncoName, id_conteudo: 9, linguas: [], }); @@ -64,14 +67,14 @@ describe("\n## TESTES TRONCO\n", () => { describe("Relacionamento Lingua e Tronco", () => { it("Criando lingua com um tronco - 1", async () => { const linguas = await modelLingua.create({ - nome: "tupi-guarani", + nome: linguaNome[0], id_tronco: 2, }); expect(linguas).toMatchObject({ id_conteudo: 10, id_tronco: 2, id_lingua: 4, - nome: "tupi-guarani", + nome: linguaNome[0], }); }); it("Listando tronco com relacionamento searchById(2) - 1", async () => { @@ -79,26 +82,26 @@ describe("\n## TESTES TRONCO\n", () => { expect(tronco).toMatchObject({ id_tronco: 2, id_conteudo: 9, - nome: "Macro-Jê123", + nome: troncoName, linguas: [ { id_lingua: 4, id_conteudo: 10, - nome: "tupi-guarani", + nome: linguaNome[0], }, ], }); }); it("Criando lingua com um tronco - 2", async () => { const linguas = await modelLingua.create({ - nome: "tupi-guarani2", + nome: linguaNome[1], id_tronco: 2, }); expect(linguas).toMatchObject({ id_conteudo: 11, id_tronco: 2, id_lingua: 5, - nome: "tupi-guarani2", + nome: linguaNome[1], }); }); it("Listando tronco com relacionamento searchById(2) - 2", async () => { @@ -106,17 +109,17 @@ describe("\n## TESTES TRONCO\n", () => { expect(tronco).toMatchObject({ id_tronco: 2, id_conteudo: 9, - nome: "Macro-Jê123", + nome: troncoName, linguas: [ { id_lingua: 4, id_conteudo: 10, - nome: "tupi-guarani", + nome: linguaNome[0], }, { id_lingua: 5, id_conteudo: 11, - nome: "tupi-guarani2", + nome: linguaNome[1], }, ], }); @@ -125,10 +128,10 @@ describe("\n## TESTES TRONCO\n", () => { const tronco = await modelTronco.getTrunkByLanguage(4); expect(tronco).toMatchObject({ id_lingua: 4, - nome: "tupi-guarani", + nome: linguaNome[0], tronco: { id_tronco: 2, - nome: "Macro-Jê123", + nome: troncoName, }, }); }); From 4e70aefd6830df97f9ba24bd64cfc4ac63a52039 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sun, 26 Sep 2021 15:35:03 -0300 Subject: [PATCH 14/21] fix: code smells sonar Co-authored-by: isadoragalvaoss --- src/__tests__/units/Tronco/index.js | 29 ++++++++++++++++------------- src/models/Tronco/index.js | 4 ++-- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/__tests__/units/Tronco/index.js b/src/__tests__/units/Tronco/index.js index 6b67156..71cb3ce 100644 --- a/src/__tests__/units/Tronco/index.js +++ b/src/__tests__/units/Tronco/index.js @@ -4,6 +4,9 @@ const modelConteudo = require("../../../models/Conteudo"); require("../../../database"); describe("\n## TESTES TRONCO\n", () => { + const troncoName = "Macro-Jê123"; + const linguasNome = ["tupi-guarani", "tupi-guarani2"]; + describe("Listagem de Tronco", () => { it("Listando com metodo getAll() com banco vazio", async () => { const tronco = await modelTronco.getAll(); @@ -44,11 +47,11 @@ describe("\n## TESTES TRONCO\n", () => { }); }); it("Atualizando tronco, com o metodo searchById(2)", async () => { - await modelTronco.editById({ nome: "Macro-Jê123" }, 2); + await modelTronco.editById({ nome: troncoName }, 2); const tronco = await modelTronco.searchById(2); expect(tronco).toMatchObject({ id_tronco: 2, - nome: "Macro-Jê123", + nome: troncoName, id_conteudo: 9, linguas: [], }); @@ -64,14 +67,14 @@ describe("\n## TESTES TRONCO\n", () => { describe("Relacionamento Lingua e Tronco", () => { it("Criando lingua com um tronco - 1", async () => { const linguas = await modelLingua.create({ - nome: "tupi-guarani", + nome: linguasNome[0], id_tronco: 2, }); expect(linguas).toMatchObject({ id_conteudo: 10, id_tronco: 2, id_lingua: 4, - nome: "tupi-guarani", + nome: linguasNome[0], }); }); it("Listando tronco com relacionamento searchById(2) - 1", async () => { @@ -79,26 +82,26 @@ describe("\n## TESTES TRONCO\n", () => { expect(tronco).toMatchObject({ id_tronco: 2, id_conteudo: 9, - nome: "Macro-Jê123", + nome: troncoName, linguas: [ { id_lingua: 4, id_conteudo: 10, - nome: "tupi-guarani", + nome: linguasNome[0], }, ], }); }); it("Criando lingua com um tronco - 2", async () => { const linguas = await modelLingua.create({ - nome: "tupi-guarani2", + nome: linguasNome[1], id_tronco: 2, }); expect(linguas).toMatchObject({ id_conteudo: 11, id_tronco: 2, id_lingua: 5, - nome: "tupi-guarani2", + nome: linguasNome[1], }); }); it("Listando tronco com relacionamento searchById(2) - 2", async () => { @@ -106,17 +109,17 @@ describe("\n## TESTES TRONCO\n", () => { expect(tronco).toMatchObject({ id_tronco: 2, id_conteudo: 9, - nome: "Macro-Jê123", + nome: troncoName, linguas: [ { id_lingua: 4, id_conteudo: 10, - nome: "tupi-guarani", + nome: linguasNome[0], }, { id_lingua: 5, id_conteudo: 11, - nome: "tupi-guarani2", + nome: linguasNome[1], }, ], }); @@ -125,10 +128,10 @@ describe("\n## TESTES TRONCO\n", () => { const tronco = await modelTronco.getTrunkByLanguage(4); expect(tronco).toMatchObject({ id_lingua: 4, - nome: "tupi-guarani", + nome: linguasNome[0], tronco: { id_tronco: 2, - nome: "Macro-Jê123", + nome: troncoName, }, }); }); diff --git a/src/models/Tronco/index.js b/src/models/Tronco/index.js index f091c88..aa0bc5a 100644 --- a/src/models/Tronco/index.js +++ b/src/models/Tronco/index.js @@ -14,9 +14,9 @@ exports.getAll = async () => { ], }); }; -exports.getTrunkByLanguage = async (id_lingua) => { +exports.getTrunkByLanguage = async (idLingua) => { return LinguaModel.findOne({ - where: { id_lingua: id_lingua }, + where: { id_lingua: idLingua }, attributes: ["id_lingua", "nome"], include: [ { From 80793519e40ffa6fe7e9bc0051c4d61f8843058c Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sun, 26 Sep 2021 15:41:54 -0300 Subject: [PATCH 15/21] fix: merge error tests Co-authored-by: isadoragalvaoss --- src/__tests__/units/Tronco/index.js | 36 ----------------------------- 1 file changed, 36 deletions(-) diff --git a/src/__tests__/units/Tronco/index.js b/src/__tests__/units/Tronco/index.js index de9e247..371d29e 100644 --- a/src/__tests__/units/Tronco/index.js +++ b/src/__tests__/units/Tronco/index.js @@ -5,11 +5,7 @@ require("../../../database"); describe("\n## TESTES TRONCO\n", () => { const troncoName = "Macro-Jê123"; -<<<<<<< HEAD - const linguasNome = ["tupi-guarani", "tupi-guarani2"]; -======= const linguaNome = ["tupi-guarani", "tupi-guarani2"]; ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c describe("Listagem de Tronco", () => { it("Listando com metodo getAll() com banco vazio", async () => { @@ -71,22 +67,14 @@ describe("\n## TESTES TRONCO\n", () => { describe("Relacionamento Lingua e Tronco", () => { it("Criando lingua com um tronco - 1", async () => { const linguas = await modelLingua.create({ -<<<<<<< HEAD - nome: linguasNome[0], -======= nome: linguaNome[0], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c id_tronco: 2, }); expect(linguas).toMatchObject({ id_conteudo: 10, id_tronco: 2, id_lingua: 4, -<<<<<<< HEAD - nome: linguasNome[0], -======= nome: linguaNome[0], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c }); }); it("Listando tronco com relacionamento searchById(2) - 1", async () => { @@ -99,33 +87,21 @@ describe("\n## TESTES TRONCO\n", () => { { id_lingua: 4, id_conteudo: 10, -<<<<<<< HEAD - nome: linguasNome[0], -======= nome: linguaNome[0], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c }, ], }); }); it("Criando lingua com um tronco - 2", async () => { const linguas = await modelLingua.create({ -<<<<<<< HEAD - nome: linguasNome[1], -======= nome: linguaNome[1], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c id_tronco: 2, }); expect(linguas).toMatchObject({ id_conteudo: 11, id_tronco: 2, id_lingua: 5, -<<<<<<< HEAD - nome: linguasNome[1], -======= nome: linguaNome[1], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c }); }); it("Listando tronco com relacionamento searchById(2) - 2", async () => { @@ -138,20 +114,12 @@ describe("\n## TESTES TRONCO\n", () => { { id_lingua: 4, id_conteudo: 10, -<<<<<<< HEAD - nome: linguasNome[0], -======= nome: linguaNome[0], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c }, { id_lingua: 5, id_conteudo: 11, -<<<<<<< HEAD - nome: linguasNome[1], -======= nome: linguaNome[1], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c }, ], }); @@ -160,11 +128,7 @@ describe("\n## TESTES TRONCO\n", () => { const tronco = await modelTronco.getTrunkByLanguage(4); expect(tronco).toMatchObject({ id_lingua: 4, -<<<<<<< HEAD - nome: linguasNome[0], -======= nome: linguaNome[0], ->>>>>>> 47c85e0c2b130ecb7061dd3adb8261ee3129070c tronco: { id_tronco: 2, nome: troncoName, From e4a1610b95649335047106278ea1666ce0178ff3 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 28 Sep 2021 15:10:28 -0300 Subject: [PATCH 16/21] feat: making seeders script to trunk and language relationship Co-authored-by: isadoragalvaoss --- .../seeders/20210918162529-conteudo.js | 2 +- .../seeders/20210928175629-troncos.js | 24 ++++++ .../seeders/20210928175636-linguas.js | 81 +++++++++++++++++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/database/seeders/20210928175629-troncos.js create mode 100644 src/database/seeders/20210928175636-linguas.js diff --git a/src/database/seeders/20210918162529-conteudo.js b/src/database/seeders/20210918162529-conteudo.js index 7857f5d..74befca 100644 --- a/src/database/seeders/20210918162529-conteudo.js +++ b/src/database/seeders/20210918162529-conteudo.js @@ -1,6 +1,6 @@ "use strict"; const statusArray = []; -const qtd = 12; +const qtd = 27; for (var it = 0; it < qtd; ++it) { statusArray.push({ status: "pendente", diff --git a/src/database/seeders/20210928175629-troncos.js b/src/database/seeders/20210928175629-troncos.js new file mode 100644 index 0000000..ef4abe9 --- /dev/null +++ b/src/database/seeders/20210928175629-troncos.js @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + up: async (queryInterface) => { + await queryInterface.bulkInsert( + "Tronco", + [ + { + nome: "línguas arauanas", + id_conteudo: 13, + }, + { + nome: "línguas aruaques", + id_conteudo: 14, + }, + ], + {} + ); + }, + + down: async (queryInterface) => { + await queryInterface.bulkDelete("Tronco", null, {}); + }, +}; diff --git a/src/database/seeders/20210928175636-linguas.js b/src/database/seeders/20210928175636-linguas.js new file mode 100644 index 0000000..6103e4d --- /dev/null +++ b/src/database/seeders/20210928175636-linguas.js @@ -0,0 +1,81 @@ +"use strict"; + +module.exports = { + up: async (queryInterface) => { + await queryInterface.bulkInsert( + "Lingua", + [ + { + nome: "Arauá", + id_conteudo: 15, + id_tronco: 1, + }, + { + nome: "Culina", + id_conteudo: 16, + id_tronco: 1, + }, + { + nome: "Deni", + id_conteudo: 17, + id_tronco: 1, + }, + { + nome: "Banauá", + id_conteudo: 18, + id_tronco: 1, + }, + { + nome: "Jamamadi", + id_conteudo: 19, + id_tronco: 1, + }, + { + nome: "Jaráuara", + id_conteudo: 20, + id_tronco: 1, + }, + { + nome: "Bahwana", + id_conteudo: 21, + id_tronco: 2, + }, + { + nome: "Baníua (de Guiana)", + id_conteudo: 22, + id_tronco: 2, + }, + { + nome: "Baré", + id_conteudo: 23, + id_tronco: 2, + }, + { + nome: "Caixana", + id_conteudo: 24, + id_tronco: 2, + }, + { + nome: "Curripaco", + id_conteudo: 25, + id_tronco: 2, + }, + { + nome: "Iabaana", + id_conteudo: 26, + id_tronco: 2, + }, + { + nome: "Jumana", + id_conteudo: 27, + id_tronco: 2, + }, + ], + {} + ); + }, + + down: async (queryInterface) => { + await queryInterface.bulkDelete("Lingua", null, {}); + }, +}; From 2c3fae51cae6efc7171b6f72a3ee7cc44a28cf35 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 28 Sep 2021 16:52:15 -0300 Subject: [PATCH 17/21] fix: add seeders script in npm start Co-authored-by: isadoragalvaoss --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6b32662..1192a8e 100644 --- a/package.json +++ b/package.json @@ -15,9 +15,9 @@ "seed:run": "sequelize db:seed:all", "seed:revert": "sequelize db:seed:undo", "seed:revertAll": "sequelize db:seed:undo:all", - "start": "sequelize db:migrate && nodemon --exec sucrase-node src/server.js", + "start": "sequelize db:migrate && sequelize db:seed:all && nodemon --exec sucrase-node src/server.js", "test": "sequelize db:migrate:undo:all && sequelize db:migrate && jest", - "lint": "eslint --ext .js,.ts,.tsx src/", + "lint": "eslint --ext .js,.ts,.tsx src/", "build": "sucrase ./src -d ./build --transforms imports", "start:prod": "sequelize db:migrate && node build/server.js" }, From b1aa84abd934d9678815b662ab6ea53c600e4148 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 28 Sep 2021 18:23:28 -0300 Subject: [PATCH 18/21] fix: remove seeders script in npm start Co-authored-by: isadoragalvaoss --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1192a8e..2c11ca5 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "seed:run": "sequelize db:seed:all", "seed:revert": "sequelize db:seed:undo", "seed:revertAll": "sequelize db:seed:undo:all", - "start": "sequelize db:migrate && sequelize db:seed:all && nodemon --exec sucrase-node src/server.js", + "start": "sequelize db:migrate && nodemon --exec sucrase-node src/server.js", "test": "sequelize db:migrate:undo:all && sequelize db:migrate && jest", "lint": "eslint --ext .js,.ts,.tsx src/", "build": "sucrase ./src -d ./build --transforms imports", From 848008a754015312a444bfe52b6058a3d5789eec Mon Sep 17 00:00:00 2001 From: hericklima22 Date: Wed, 29 Sep 2021 19:27:07 -0300 Subject: [PATCH 19/21] Fix: word parans in body Co-authored-by: ingridSCarvalho --- src/controllers/Palavra/getOnePalavra.js | 2 +- src/routes/Palavra/palavra.routes.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/controllers/Palavra/getOnePalavra.js b/src/controllers/Palavra/getOnePalavra.js index 6d7dcce..e778dd2 100644 --- a/src/controllers/Palavra/getOnePalavra.js +++ b/src/controllers/Palavra/getOnePalavra.js @@ -10,7 +10,7 @@ export async function getOne(request, response) { ); } - const { id_palavra } = request.body; + const { id_palavra } = request.params; if (!id_palavra) { throw new HttpException(400, `ID inválido - Palavra - ID ${id_palavra}`); } diff --git a/src/routes/Palavra/palavra.routes.js b/src/routes/Palavra/palavra.routes.js index f8e7ca1..9a39412 100644 --- a/src/routes/Palavra/palavra.routes.js +++ b/src/routes/Palavra/palavra.routes.js @@ -10,8 +10,9 @@ import { } from "../../controllers/Palavra"; const idLingua = "/:id_lingua"; +const idPalavra = "/:id_palavra"; -router.get(idLingua, getOne); +router.get(`/one${idLingua}${idPalavra}`, getOne); router.get(`/all${idLingua}`, getAll); router.post(idLingua, create); router.put(idLingua, update); From d1435e03fc3b406cf43f10139ee640b806a38f4b Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Fri, 1 Oct 2021 16:38:04 -0300 Subject: [PATCH 20/21] fix: improving output of language data --- src/__tests__/units/Lingua/index.js | 8 ++++---- src/models/Lingua/index.js | 13 ++++++++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/__tests__/units/Lingua/index.js b/src/__tests__/units/Lingua/index.js index 2317d90..efa2875 100644 --- a/src/__tests__/units/Lingua/index.js +++ b/src/__tests__/units/Lingua/index.js @@ -35,20 +35,20 @@ describe("\n## TESTES LINGUA\n", () => { await modelLingua.editById({ nome: "Aikanã" }, 1); const lingua = await modelLingua.searchById(1); expect(lingua).toMatchObject({ - id_conteudo: 3, id_lingua: 1, - id_tronco: null, + id_conteudo: 3, nome: "Aikanã", + tronco: null, }); }); it("Atualizando lingua, com o metodo editById(2) na tupla de ID = 2", async () => { await modelLingua.editById({ nome: "Aikewara" }, 2); const lingua = await modelLingua.searchById(2); expect(lingua).toMatchObject({ - id_conteudo: 4, id_lingua: 2, - id_tronco: null, + id_conteudo: 4, nome: "Aikewara", + tronco: null, }); }); }); diff --git a/src/models/Lingua/index.js b/src/models/Lingua/index.js index f072aa9..207732c 100644 --- a/src/models/Lingua/index.js +++ b/src/models/Lingua/index.js @@ -1,4 +1,5 @@ const LinguaModel = require("./Lingua"); +const TroncoModel = require("../Tronco/Tronco"); const Conteudo = require("../Conteudo"); exports.getAll = async () => { @@ -19,7 +20,17 @@ exports.create = async (lingua) => { return LinguaModel.create(lingua); }; exports.searchById = async (id) => { - return LinguaModel.findByPk(id); + return LinguaModel.findOne({ + where: { id_lingua: id }, + attributes: ["id_lingua", "id_conteudo", "nome"], + include: [ + { + model: TroncoModel, + as: "tronco", + attributes: ["id_tronco", "nome"], + }, + ], + }); }; exports.searchAll = async () => { return LinguaModel.findAll(); From 861767768b472e23244bc1d36a3af7df6b875d6a Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Sat, 2 Oct 2021 10:11:05 -0300 Subject: [PATCH 21/21] fix: changing from set null to restrict Co-authored-by: isadoragalvaoss --- .../migrations/20210905150640-createTable-Lingua.js | 4 ++-- src/models/Lingua/Lingua.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/database/migrations/20210905150640-createTable-Lingua.js b/src/database/migrations/20210905150640-createTable-Lingua.js index 9f526b4..e377456 100644 --- a/src/database/migrations/20210905150640-createTable-Lingua.js +++ b/src/database/migrations/20210905150640-createTable-Lingua.js @@ -28,8 +28,8 @@ module.exports = { model: "Tronco", key: "id_tronco", }, - onUpdate: "SET NULL", - onDelete: "SET NULL", + onUpdate: "RESTRICT", + onDelete: "RESTRICT", }, nome: { type: Sequelize.DataTypes.STRING, diff --git a/src/models/Lingua/Lingua.js b/src/models/Lingua/Lingua.js index 4f99eff..bf24da8 100644 --- a/src/models/Lingua/Lingua.js +++ b/src/models/Lingua/Lingua.js @@ -47,15 +47,15 @@ Lingua.hasOne(Conteudo, { }); Lingua.hasOne(Tronco, { foreignKey: "id_tronco", - onDelete: "SET NULL", - onUpdate: "SET NULL", + onDelete: "RESTRICT", + onUpdate: "RESTRICT", sourceKey: "id_tronco", as: "tronco", }); Tronco.hasMany(Lingua, { foreignKey: "id_tronco", - onDelete: "SET NULL", - onUpdate: "SET NULL", + onDelete: "RESTRICT", + onUpdate: "RESTRICT", sourceKey: "id_tronco", as: "linguas", });