diff --git a/database.sqlite b/database.sqlite index a647e01..e69de29 100644 Binary files a/database.sqlite and b/database.sqlite differ diff --git a/package.json b/package.json index 6b32662..2c11ca5 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "seed:revertAll": "sequelize db:seed:undo:all", "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/", + "lint": "eslint --ext .js,.ts,.tsx src/", "build": "sucrase ./src -d ./build --transforms imports", "start:prod": "sequelize db:migrate && node build/server.js" }, 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 5a08563..efa2875 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,19 +34,21 @@ 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({ - id_conteudo: 3, + expect(lingua).toMatchObject({ id_lingua: 1, + 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.dataValues).toEqual({ - id_conteudo: 4, + expect(lingua).toMatchObject({ id_lingua: 2, + id_conteudo: 4, nome: "Aikewara", + 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/__tests__/units/Tronco/index.js b/src/__tests__/units/Tronco/index.js new file mode 100644 index 0000000..371d29e --- /dev/null +++ b/src/__tests__/units/Tronco/index.js @@ -0,0 +1,139 @@ +const modelTronco = require("../../../models/Tronco"); +const modelLingua = require("../../../models/Lingua"); +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(); + 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: troncoName }, 2); + const tronco = await modelTronco.searchById(2); + expect(tronco).toMatchObject({ + id_tronco: 2, + nome: troncoName, + 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: linguaNome[0], + id_tronco: 2, + }); + expect(linguas).toMatchObject({ + id_conteudo: 10, + id_tronco: 2, + id_lingua: 4, + nome: linguaNome[0], + }); + }); + 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: troncoName, + linguas: [ + { + id_lingua: 4, + id_conteudo: 10, + nome: linguaNome[0], + }, + ], + }); + }); + it("Criando lingua com um tronco - 2", async () => { + const linguas = await modelLingua.create({ + nome: linguaNome[1], + id_tronco: 2, + }); + expect(linguas).toMatchObject({ + id_conteudo: 11, + id_tronco: 2, + id_lingua: 5, + nome: linguaNome[1], + }); + }); + 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: troncoName, + linguas: [ + { + id_lingua: 4, + id_conteudo: 10, + nome: linguaNome[0], + }, + { + id_lingua: 5, + id_conteudo: 11, + nome: linguaNome[1], + }, + ], + }); + }); + it("Resgatando tronco com id da lingua", async () => { + const tronco = await modelTronco.getTrunkByLanguage(4); + expect(tronco).toMatchObject({ + id_lingua: 4, + nome: linguaNome[0], + tronco: { + id_tronco: 2, + nome: troncoName, + }, + }); + }); + }); +}); 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"); diff --git a/src/controllers/Lingua/createLingua.js b/src/controllers/Lingua/createLingua.js index 1227d77..dccad26 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; + const { nome, id_tronco } = request.body; if (!nome) { - throw new HttpException(400, `Nome inválido - Lingua - ${nome}`); + 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); 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/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/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 new file mode 100644 index 0000000..9e7f352 --- /dev/null +++ b/src/controllers/Tronco/index.js @@ -0,0 +1,6 @@ +export { create } from "./createTronco"; +export { getOne } from "./getOneTronco"; +export { getAll } from "./getAllTronco"; +export { update } from "./updateTronco"; +export { deleteOne } from "./deleteTronco"; +export { getTroncoByLang } from "./getTroncoByLang"; 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/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 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, {}); + }, +}; diff --git a/src/models/Lingua/Lingua.js b/src/models/Lingua/Lingua.js index ac56357..bf24da8 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: "SET NULL", + onDelete: "SET NULL", + }, 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/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(); 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..e2f0cb2 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: { @@ -22,17 +28,35 @@ exports.create = async (body) => { exports.searchById = async (idPalavra, idLingua) => { return PalavraModel.findOne({ + raw: true, + nest: true, where: { id_palavra: idPalavra, id_lingua: idLingua, }, + attributes: ["id_palavra", "id_conteudo", "nome", "significado"], + include: [ + { + model: LinguaModel, + as: "lingua", + attributes: ["id_lingua", "id_conteudo", "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) => { diff --git a/src/models/Tronco/Tronco.js b/src/models/Tronco/Tronco.js new file mode 100644 index 0000000..6b427e3 --- /dev/null +++ b/src/models/Tronco/Tronco.js @@ -0,0 +1,43 @@ +const databaseConfig = require("../../config/database"); +const { Sequelize, DataTypes } = require("sequelize"); +const sequelize = new Sequelize(databaseConfig); +const Conteudo = require("../Conteudo/Conteudo"); + +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; diff --git a/src/models/Tronco/index.js b/src/models/Tronco/index.js new file mode 100644 index 0000000..aa0bc5a --- /dev/null +++ b/src/models/Tronco/index.js @@ -0,0 +1,65 @@ +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.getTrunkByLanguage = async (idLingua) => { + return LinguaModel.findOne({ + where: { id_lingua: idLingua }, + attributes: ["id_lingua", "nome"], + include: [ + { + model: TroncoModel, + as: "tronco", + attributes: ["id_tronco", "nome"], + }, + ], + }); +}; +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/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); diff --git a/src/routes/Tronco/tronco.routes.js b/src/routes/Tronco/tronco.routes.js new file mode 100644 index 0000000..2a1f03a --- /dev/null +++ b/src/routes/Tronco/tronco.routes.js @@ -0,0 +1,22 @@ +import { Router } from "express"; +const router = Router(); + +import { + getOne, + create, + 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); +router.delete(idTronco, deleteOne); + +export default router; diff --git a/src/routes/index.js b/src/routes/index.js index 610e1fe..04a7293 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -1,10 +1,12 @@ 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); app.use("/teste", (req, res) => res.send("ta funcionando")); }