Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVC Scaffolding, separando rotas com Id de rotas sem Id. #35

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 60 additions & 9 deletions interface-linha-comando/gerar/gerador-rotas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as caminho from 'path';

import { Classe } from '@designliquido/delegua/fontes/declaracoes';
import { pluralizar } from '@designliquido/flexoes';
import { criarDiretorioSeNaoExiste } from '.';
import { criarDiretorioComIdSeNaoExiste, criarDiretorioSeNaoExiste } from '.';

export class GeradorRotas {
indentacao: number;
Expand Down Expand Up @@ -31,9 +31,9 @@ export class GeradorRotas {

criarDiretorioSeNaoExiste('rotas', nomeModeloPlural);

const rotasCriadas = [];
let rotasCriadas = [];
rotasCriadas.push(this.criarNovasRotasSemId(declaracaoModelo, diretorioRotas));
rotasCriadas.push(this.criarNovasRotasComId(declaracaoModelo, diretorioRotas));
rotasCriadas = rotasCriadas.concat(this.criarNovasRotasComId(declaracaoModelo, diretorioRotas));
return rotasCriadas;
}

Expand Down Expand Up @@ -63,18 +63,35 @@ export class GeradorRotas {
* @param {string} diretorioRotas O diretório onde o arquivo de rotas deve ser salvo.
* @returns O caminho do arquivo de rotas no sistema de arquivos.
*/
private criarNovasRotasComId(declaracaoModelo: Classe, diretorioRotas: string): string {
private criarNovasRotasComId(declaracaoModelo: Classe, diretorioRotas: string): string[] {
const conteudoSelecionarUm = this.criarRotaSelecionarUm(declaracaoModelo);
const conteudoSelecionarParaEdicao = this.criarRotaEditar(declaracaoModelo);
const conteudoAtualizar = `liquido.rotaPut(funcao(requisicao, resposta) {\n resposta.lmht({ "titulo": "Liquido" })\n})\n\n`;
const conteudoSelecionarParaExclusao = this.criarRotaConfirmarExclusao(declaracaoModelo);
const conteudoExcluir = `liquido.rotaDelete(funcao(requisicao, resposta) {\n resposta.lmht({ "titulo": "Liquido" })\n})\n\n`;
const conteudoRotas = `${conteudoSelecionarUm}${conteudoAtualizar}${conteudoExcluir}`;
const conteudoRotas = `${conteudoSelecionarUm}${conteudoSelecionarParaEdicao}${conteudoAtualizar}${conteudoSelecionarParaExclusao}${conteudoExcluir}`;

const caminhoRotas = caminho.join(diretorioRotas, '[id].delegua');
const diretorioRotasComId = criarDiretorioComIdSeNaoExiste(diretorioRotas);

const caminhoRotasId = caminho.join(diretorioRotasComId, 'inicial.delegua');
sistemaArquivos.writeFileSync(
caminhoRotas,
caminhoRotasId,
conteudoRotas
);
return caminhoRotas;

const caminhoRotaEditar = caminho.join(diretorioRotasComId, 'editar.delegua');
sistemaArquivos.writeFileSync(
caminhoRotaEditar,
conteudoRotas
);

const caminhoRotaExcluir = caminho.join(diretorioRotasComId, 'excluir.delegua');
sistemaArquivos.writeFileSync(
caminhoRotaExcluir,
conteudoRotas
);

return [caminhoRotasId, caminhoRotaEditar, caminhoRotaExcluir];
}

private criarRotaSelecionarTudo(declaracaoModelo: Classe) {
Expand All @@ -96,7 +113,7 @@ export class GeradorRotas {
`})\n\n`;
}

private criarRotaSelecionarUm(declaracaoModelo: Classe) {
private criarRotaSelecionarUm(declaracaoModelo: Classe): string {
// Isso aqui não vai ficar assim.
// É preciso montar as partes de dados antes.
const dadosTestes = [];
Expand All @@ -112,4 +129,38 @@ export class GeradorRotas {
`${" ".repeat(this.indentacao)}})\n` +
`})\n\n`;
}

private criarRotaEditar(declaracaoModelo: Classe): string {
// Isso aqui não vai ficar assim.
// É preciso montar as partes de dados antes.
const dadosTestes = [];
for (const propriedade of declaracaoModelo.propriedades) {
dadosTestes.push(`"${propriedade.nome.lexema}": "Teste"`);
}

return `liquido.rotaGet(funcao(requisicao, resposta) {\n` +
`${" ".repeat(this.indentacao)}resposta.lmht("editar", {\n` +
`${" ".repeat(this.indentacao * 2)}${dadosTestes.reduce(
(acumulador, elemento) => acumulador + ', ' + elemento
)}\n` +
`${" ".repeat(this.indentacao)}})\n` +
`})\n\n`;
}

private criarRotaConfirmarExclusao(declaracaoModelo: Classe): string {
// Isso aqui não vai ficar assim.
// É preciso montar as partes de dados antes.
const dadosTestes = [];
for (const propriedade of declaracaoModelo.propriedades) {
dadosTestes.push(`"${propriedade.nome.lexema}": "Teste"`);
}

return `liquido.rotaGet(funcao(requisicao, resposta) {\n` +
`${" ".repeat(this.indentacao)}resposta.lmht("confirmar-exclusao", {\n` +
`${" ".repeat(this.indentacao * 2)}${dadosTestes.reduce(
(acumulador, elemento) => acumulador + ', ' + elemento
)}\n` +
`${" ".repeat(this.indentacao)}})\n` +
`})\n\n`;
}
}
15 changes: 15 additions & 0 deletions interface-linha-comando/gerar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ export function criarDiretorioSeNaoExiste(...partesDiretorio: string[]) {
sistemaArquivos.mkdirSync(caminhoDiretorio);
}
}

/**
* Cria na raiz do projeto um diretório passado por parâmetro, se já não existir, com `[id]` no final.
* Precisa ser separado porque o `caminho.join` aparentemente estraga o nome do diretório.
* @param {string} diretorioRotas O prefixo do diretório a ser verificado/criado.
*/
export function criarDiretorioComIdSeNaoExiste(diretorioRotas: string): string {
const caminhoDiretorio = diretorioRotas + `\\[id]`;

if (!sistemaArquivos.existsSync(caminhoDiretorio)) {
sistemaArquivos.mkdirSync(caminhoDiretorio);
}

return caminhoDiretorio;
}
45 changes: 26 additions & 19 deletions liquido.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,41 @@
return null;
}

try {
const retornoImportador = this.importador.importar(caminhoConfigArquivo.caminho);

for (const declaracao of retornoImportador.retornoAvaliadorSintatico.declaracoes) {
const expressao: DefinirValor = (declaracao as Expressao).expressao as DefinirValor;
const objetoAlvo: AcessoMetodoOuPropriedade = expressao.objeto as AcessoMetodoOuPropriedade;

Check warning on line 122 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const nomePropriedade: string = expressao.nome.lexema;
const informacoesVariavel: VariavelInterface = expressao.valor;

if (expressao.objeto.simbolo.lexema === 'roteador') {
this.roteador.ativarMiddleware(nomePropriedade, informacoesVariavel);
}

if (expressao.objeto.simbolo.lexema === 'autenticacao') {
if (nomePropriedade === 'tecnologia') {
switch (informacoesVariavel.valor) {
case 'jwt':
this.roteador.ativarDesativarPassport(true);
break;
default:
console.error('Tecnologia de autenticação não suportada.');
switch (objetoAlvo.simbolo.lexema) {
case 'roteador':
this.roteador.ativarMiddleware(nomePropriedade, informacoesVariavel);

Check warning on line 128 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
break;

Check warning on line 129 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 129 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
case 'autenticacao':
if (nomePropriedade === 'tecnologia') {
switch (informacoesVariavel.valor) {
case 'jwt':
this.roteador.ativarDesativarPassport(true);

Check warning on line 134 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
break;

Check warning on line 135 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 135 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
default:
console.error('Tecnologia de autenticação não suportada.');

Check warning on line 137 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 137 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

Check warning on line 138 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 139 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 139 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}
}

if (expressao.objeto.objeto.simbolo.lexema === 'dados') {
if (expressao.objeto.simbolo.lexema === 'lincones') {
this.provedorLincones.configurar(nomePropriedade, informacoesVariavel.valor);
}
break;

Check warning on line 140 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 140 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
case 'lincones': {
const objetoLinconesAlvo: AcessoMetodoOuPropriedade = objetoAlvo.objeto as AcessoMetodoOuPropriedade;

Check warning on line 142 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
switch (objetoLinconesAlvo.simbolo.lexema) {
case 'dados':
this.provedorLincones.configurar(nomePropriedade, informacoesVariavel.valor);

Check warning on line 145 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
break;

Check warning on line 146 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 146 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
// Casos futuros aqui.
}

Check warning on line 148 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

break;

Check warning on line 151 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 151 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}

Check warning on line 152 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 153 in liquido.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"swagger": "tsoa spec"
},
"dependencies": {
"@designliquido/delegua-node": "^0.29.2",
"@designliquido/delegua-node": "^0.30.2",
"@designliquido/flexoes": "^0.1.0",
"@designliquido/foles": "^0.6.1",
"@designliquido/lincones-sqlite": "^0.0.2",
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,21 @@
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@designliquido/delegua-node@^0.29.2":
version "0.29.2"
resolved "https://registry.yarnpkg.com/@designliquido/delegua-node/-/delegua-node-0.29.2.tgz#828bd88481ee5e7201a789d0685d8fc44cadde62"
integrity sha512-Xn7lSG2vixj2tfL7QKyhGUONk8QkxmW4mcoYwO3lj/tgIL6NJzx/8LRf0wqR7aBlYBWqrnAnYvMbl0qlDsDcjg==
"@designliquido/delegua-node@^0.30.2":
version "0.30.2"
resolved "https://registry.yarnpkg.com/@designliquido/delegua-node/-/delegua-node-0.30.2.tgz#eb8e2bbf41d98b83c205848e2bd9ee6a4bc8b5fa"
integrity sha512-ia3nrh4wgE6N/a1GFExVy0U1ncYfLwDC0omy37qjOOcWuTpBoeNf11nyuus72Pyc/ZJvNnAlzSNtzrLKWV7u+w==
dependencies:
"@designliquido/delegua" "0.29.2"
"@designliquido/delegua" "0.30.2"
chalk "4.1.2"
commander "^9.4.1"
json-colorizer "^2.2.2"
lodash.clonedeep "^4.5.0"

"@designliquido/delegua@0.29.2":
version "0.29.2"
resolved "https://registry.yarnpkg.com/@designliquido/delegua/-/delegua-0.29.2.tgz#da5eb931deede8a5cbf8fa8b95f65379972ae5ee"
integrity sha512-sdGD+b1gJBZqhfppk2WTHIaQviUylu9907kKHAXRXIEuQgUfi1GA/EFZhvr3CvHemhmbo9kQclN4JQEY+zBKUQ==
"@designliquido/delegua@0.30.2":
version "0.30.2"
resolved "https://registry.yarnpkg.com/@designliquido/delegua/-/delegua-0.30.2.tgz#8af380e6da5cb4bacf89c4aebf0e7115056037a5"
integrity sha512-mvfmq9KaOF6grXZd7MxQnEy3XOlK95uGtdgGti/taVwyvjtbJZR9aw0pwDEtuyDFBHTWAJYvsJmnH+KRWOsGkw==
dependencies:
antlr4ts "^0.5.0-alpha.4"
browser-process-hrtime "^1.0.0"
Expand Down
Loading