Skip to content
Open
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
4 changes: 4 additions & 0 deletions cases/case3/Amaro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
package-lock.json
build
.env
40 changes: 40 additions & 0 deletions cases/case3/Amaro/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "Amaro",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start:dev": "ts-node-dev ./src/index.ts",
"start": "node ./build/index.js",
"build": "tsc"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/knex": "^0.16.1",
"@types/node": "^18.7.1",
"knex": "^2.2.0",
"mysql": "^2.18.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4",
"@types/bcryptjs": "^2.4.2",
"@types/jest": "^25.2.3",
"@types/jsonwebtoken": "^8.5.0",
"@types/uuid": "^8.3.4"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"bcryptjs": "^2.4.3",
"jest": "^26.0.1",
"jsonwebtoken": "^8.5.1",
"nodemon": "^2.0.19",
"process": "^0.11.10",
"ts-jest": "^26.1.0",
"uuid": "^8.3.2"
}
}
38 changes: 38 additions & 0 deletions cases/case3/Amaro/src/business/CategoriasBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { IdGenerator } from "../services/IdGenerator";
import { CategoriaDatabase } from "../data/CategoriasDatabase";
import { categoria, categoriaInputDTO } from "../model/categoria";

const idGenerator = new IdGenerator();

export class CategoriaBusiness {
public adicionarCategoria = async (input: categoriaInputDTO) => {
try {
const { TAG, produto_id } = input;

const id: string = idGenerator.generate();

const categoriaDatabase = new CategoriaDatabase();
const categoria: categoria = {
id,
TAG,
produto_id,
};

await categoriaDatabase.adicionarCategoria({
id,
TAG,
produto_id,
});
} catch (error: any) {
throw new Error(error.sqlMessage || error.message);
}
};

public TodasCategorias = async (categoria: categoria, config: any) => {
try {
return await new CategoriaDatabase().TodasCategorias(categoria, config);
} catch (error: any) {
throw new Error(error.sqlMessage || error.message);
}
};
}
72 changes: 72 additions & 0 deletions cases/case3/Amaro/src/business/ProdutosBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { IdGenerator } from "../services/IdGenerator";
import { ProdutosDatabase } from "../data/ProdutosDatabase";
import { produto, produtoInputDTO } from "../model/produto";

const idGenerator = new IdGenerator();

export class ProdutosBusiness {
public adicionarProduto = async (input: produtoInputDTO) => {
try {
const { nome, marca, preco_compra, icms, lucro } = input;

const calculoPrecoCustoEVenda = (
preco_compra: any,
icms: any,
lucro: any
) => {
let preco_custo: number = (icms / 100) * preco_compra + preco_compra;

let precoVenda: number = (lucro / 100) * preco_custo + preco_custo;

return precoVenda;
};

const preco_venda: number = calculoPrecoCustoEVenda(
input.preco_compra,
input.icms,
input.lucro
);

const id: string = idGenerator.generate();

const produtosDatabase = new ProdutosDatabase();
const produto: produto = {
id,
nome,
marca,
preco_compra,
icms,
lucro,
preco_venda,
};

await produtosDatabase.adicionarProduto({
id,
nome,
marca,
preco_compra,
icms,
lucro,
preco_venda,
});
} catch (error: any) {
throw new Error(error.sqlMessage || error.message);
}
};

public ListarTodosProdutos = async (produto: produto) => {
try {
return await new ProdutosDatabase().ListarTodosProdutos(produto, produto);
} catch (error: any) {
throw new Error(error.sqlMessage || error.message);
}
};

public PesquisaProdutos = async (produto: produto) => {
try {
return await new ProdutosDatabase().PesquisaProdutos(produto, produto);
} catch (error: any) {
throw new Error(error.sqlMessage || error.message);
}
};
}
53 changes: 53 additions & 0 deletions cases/case3/Amaro/src/business/UserBusiness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { UserInputDTO, LoginInputDTO } from "../model/user";
import { UserDatabase } from "../data/UserDatabase";
import { IdGenerator } from "../services/IdGenerator";
import { HashManager } from "../services/HashManager";
import { Authenticator } from "../services/Authenticator";
import { UserNotFoud } from "../error/customError";

export class UserBusiness {
async createUser(user: UserInputDTO) {
const idGenerator = new IdGenerator();
const id = idGenerator.generate();

const hashManager = new HashManager();
const hashPassword = await hashManager.hash(user.password);

const userDatabase = new UserDatabase();
await userDatabase.createUser(
id,
user.email,
user.name,
hashPassword,
user.role
);

const authenticator = new Authenticator();
const accessToken = authenticator.generateToken({ id, role: user.role });

return accessToken;
}

async getUserByEmail(user: LoginInputDTO) {
const userDatabase = new UserDatabase();
const userFromDB = await userDatabase.getUserByEmail(user.email);

const hashManager = new HashManager();
const hashCompare = await hashManager.compare(
user.password,
userFromDB.getPassword()
);

const authenticator = new Authenticator();
const accessToken = authenticator.generateToken({
id: userFromDB.getId(),
role: userFromDB.getRole(),
});

if (!hashCompare) {
throw new UserNotFoud();
}

return accessToken;
}
}
87 changes: 87 additions & 0 deletions cases/case3/Amaro/src/controller/CategoriasController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Request, Response } from "express";
import { CategoriaBusiness } from "../business/CategoriasBusiness";
import { CategoriaDatabase } from "../data/CategoriasDatabase";
import { categoria, categoriaInputDTO } from "../model/categoria";
import { InvalidData, InvalidTAG, InvalidID } from "../error/customError";

export class CategoriaController {
public adicionarCategoria = async (
req: Request,
res: Response
): Promise<void> => {
try {
if (!req.body.TAG || !req.body.produto_id) {
throw new InvalidData();
}

if (typeof req.body.TAG !== "string") {
throw new InvalidTAG();
}
if (typeof req.body.produto_id !== "string") {
throw new InvalidID();
}

const { TAG, produto_id } = req.body;

const input: categoriaInputDTO = { TAG, produto_id };

const categoriaBusiness = new CategoriaBusiness();
categoriaBusiness.adicionarCategoria(input);

res.status(201).send({ message: "Categoria adicionada com sucesso!" });
} catch (error: any) {
res.status(400).send(error.sqlMessage || error.message);
}
};

public TodasCategorias = async (req: Request, res: Response) => {
try {
const input: any = {
id: req.params.id,
search: req.query.search,
sort: req.query.sort as string,
order: req.query.order as string,
page: Number(req.query.page),
size: Number(req.query.size),
};

let offset = input.size * (input.page - 1);
input.offset = offset;

if (!input.resultado) {
input.resultado = "%";
}

if (input.sort !== "tag") {
input.sort = "tag";
}

if (
!input.order ||
(input.order.toUpperCase() !== "ASC" &&
input.order.toUpperCase() !== "DESC")
) {
input.order = "ASC";
}

if (isNaN(input.page) || input.page < 1) {
input.page = 1;
}

if (isNaN(input.size) || input.size < 1) {
input.size = 10;
}

const result = await new CategoriaDatabase().TodasCategorias(
input,
input
);

res.send(result).status(200);
} catch (error: any) {
res
.send({ message: error.message })
.status(error.sqlMessage || error.message);
}
};
}
Loading