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

Dev #5

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ae4d5d3
feat: create project base
gabrielsoaresm94 May 30, 2023
2a4a9e8
refactor: changes code to fit an architecture of modules in intentions
gabrielsoaresm94 May 31, 2023
052771c
feat: adds new architecture in modules
gabrielsoaresm94 Jun 2, 2023
ba576d0
refactor: add own image for bank service
gabrielsoaresm94 Jun 2, 2023
2c09e3e
fix: return to postgres image
gabrielsoaresm94 Jun 2, 2023
611ce4f
feat: add models and migrations
gabrielsoaresm94 Jun 3, 2023
1733778
feat: add new modules
gabrielsoaresm94 Jun 3, 2023
1c69389
refactor: allocate models in their correct modules and change the fir…
gabrielsoaresm94 Jun 3, 2023
be6d09f
feat: add new documentation for intentions service
gabrielsoaresm94 Jun 4, 2023
cefc044
feat: add repositories and services for modules in intentions service
gabrielsoaresm94 Jun 4, 2023
86e3cf3
refactor: change server.ts to app.ts in products service
gabrielsoaresm94 Jun 4, 2023
1bea607
feat(products): add new architecture for service
gabrielsoaresm94 Jun 4, 2023
69aaeda
fix(products): put providers in the correct folders
gabrielsoaresm94 Jun 4, 2023
65ad8ac
feat(products): add proxy provider and product listing
gabrielsoaresm94 Jun 5, 2023
15c996a
feat(products): add endpoint for find product
gabrielsoaresm94 Jun 5, 2023
d18625d
feat(intentions): try to run migrations when build the service
gabrielsoaresm94 Jun 5, 2023
2479220
feat(intentions): finalize client creation
gabrielsoaresm94 Jun 6, 2023
97e7bf6
feat(intentions): save intentions on database
gabrielsoaresm94 Jun 6, 2023
68b23cd
refactor(intentions): remove update controller for address
gabrielsoaresm94 Jun 6, 2023
e1c3155
feat(products): add intentions service
gabrielsoaresm94 Jun 6, 2023
a3e8928
feat(intentions): add last endpints to save products
gabrielsoaresm94 Jun 7, 2023
9f0ca67
feat: finish project
gabrielsoaresm94 Jun 7, 2023
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
Prev Previous commit
Next Next commit
feat(products): add intentions service
gabrielsoaresm94 committed Jun 6, 2023
commit e1c315572ebd34a8243effb2f10f974ab817fcdc
58 changes: 58 additions & 0 deletions products/src/modules/intentions/controllers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Router, Request, Response } from 'express';
import UpdateIntention from './services/update-intention';
import FindProduct from '../products/services/found-products';
import { IProduct, IntentionStatus } from './dtos';
import { IResponse } from '../../shared/dtos';

const intentionsRouter = Router();

/**
* @swagger
* /intentions/<intention_id>:
* put:
* summary: Seleciona produtos e atualiza intenção.
* description: Seleciona produtos dá intenção de compra e atualiza status.
* tags:
* - Intentions
* responses:
* 200:
* description: Success
*/
intentionsRouter.put(
'/',
async (req: Request, res: Response): Promise<Response> => {
const intentionId: Number = req.body.intention_id;
const productsIds: Array<Number> = req.body.products_ids;

if (!intentionId) {}

if (!productsIds || (productsIds && productsIds.length <= 0)) {}

const products: Array<IProduct> = []
for (const productId of productsIds) {
const product = await FindProduct.execute(productId);
if (!product) {
continue;
}
products.push(product);
}

const intentionUpdated = await UpdateIntention.execute(
intentionId,
"SELECIONADO" as IntentionStatus,
products
);

if (!intentionUpdated) {
return res.status(400).json({
message: 'Problemas para selecionar produtos para a intenção de compra.',
status: false,
});
}

return res.status(200).json({
message: 'Produtos selecionados para a intenção de compra com sucesso!',
status: true,
});
}
);
30 changes: 30 additions & 0 deletions products/src/modules/intentions/dtos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
interface IRating {
rate: Number;
count: Number;
}

export interface IProduct {
id: Number;
title: String;
price: Number;
category: String;
description: String;
image: String;
rating: IRating
}

export enum IntentionStatus {
EM_SELECAO = 'EM_SELECAO',
SELECIONADO = 'SELECIONADO',
}

export interface IIntentionProduct {
product_id: Number;
title: String;
price: Number;
category: String;
description: String;
image: String;
quantity: Number | null;
intention_id: Number;
}
44 changes: 44 additions & 0 deletions products/src/modules/intentions/services/update-intention.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { axiosProvider, HTTPMethod } from "../../../shared/providers/proxy-provider/implementations/axios-provider";
import { IIntentionProduct, IntentionStatus, IProduct } from "../dtos";

class UpdateIntention {
public static async execute(
intentionId: Number,
intentionStatus: IntentionStatus,
products: Array<IProduct>
): Promise<Object | undefined> {
const intentionProducts: Array<IIntentionProduct> = [];

for (const product of products) {
const intentionProduct = {
product_id: product.id,
title: product.title,
price: product.price,
category: product.category,
description: product.description,
image: product.image,
quantity: null,
intention_id: intentionId,
};
intentionProducts.push(intentionProduct);
}

const intentionRequestBody = {
intention_status: intentionStatus,
intention_products: intentionProducts,
};
const PATCH = 'patch' as HTTPMethod;
const url = 'http://intentions-service:5000/v1/intentions/'
const intentionUpdated = await axiosProvider(PATCH, `${url}/${intentionId}/`, intentionRequestBody);

if (!intentionUpdated) {
return intentionUpdated;
}

const intentionReturn = intentionUpdated.data;

return intentionReturn;
}
}

export default UpdateIntention;
9 changes: 7 additions & 2 deletions products/src/modules/products/controllers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router, Request, Response } from 'express';
import ListProducts from './services/list-products';
import FoundProduct from './services/found-products';
import FindProduct from './services/found-products';

const productsRouter = Router();

@@ -25,11 +25,13 @@ productsRouter.get(
if (!productsListed) {
return res.status(400).json({
message: 'Problemas para listar produtos.',
status: false,
});
}

return res.status(200).json({
message: 'Produtos listados com sucesso!',
status: true,
data: productsListed,
});
}
@@ -61,19 +63,22 @@ productsRouter.get(
if (!productId) {
return res.status(400).json({
message: 'O campo "product_id" deve ser do tipo inteiro.',
status: false,
});
}

const productsListed = await FoundProduct.execute(productId);
const productsListed = await FindProduct.execute(productId);

if (!productsListed) {
return res.status(400).json({
message: 'Problemas para encontrar produto.',
status: false,
});
}

return res.status(200).json({
message: 'Produtos listados com sucesso!',
status: true,
data: productsListed,
});
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
import axiosProvider from '../../../../../shared/providers/proxy-provider/implementations/axios-provider';

enum HTTPMethod {
GET = 'get',
POST = 'post',
PUT = 'put',
DELETE = 'delete',
PATCH = 'patch',
}
import { axiosProvider, HTTPMethod } from '../../../../../shared/providers/proxy-provider/implementations/axios-provider';
import { IProduct } from '../../../../intentions/dtos';

const URL = 'https://fakestoreapi.com';

class ProductRepository {
public static async list() {
public static async list(): Promise<Array<IProduct> | undefined> {
const GET = 'get' as HTTPMethod;
const productsResponse = await axiosProvider(GET, `${URL}/products`);

if (!productsResponse) {
return productsResponse;
}

const productsListed = productsResponse.data;
const productsListed = productsResponse.data as Array<IProduct>;

return productsListed;
}

public static async find(productId: number) {
public static async find(productId: Number): Promise<IProduct | undefined> {
const GET = 'get' as HTTPMethod;
const productResponse = await axiosProvider(GET, `${URL}/products/${productId}`);

if (!productResponse) {
return productResponse;
}

const productFound = productResponse.data;
const productFound = productResponse.data as IProduct;

return productFound;
}
7 changes: 4 additions & 3 deletions products/src/modules/products/services/found-products.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IProduct } from '../../intentions/dtos';
import ProductRepository from '../infra/fake-store-api/repositories/product-repository';

class FoundProduct {
public static async execute(productId: number): Promise<Object | undefined> {
class FindProduct {
public static async execute(productId: Number): Promise<IProduct | undefined> {
const productFound = await ProductRepository.find(productId);
return productFound;
}
}

export default FoundProduct;
export default FindProduct;
5 changes: 5 additions & 0 deletions products/src/shared/dtos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IResponse<T> {
message: String;
status: boolean;
data: T;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import axios, { AxiosResponse } from 'axios';

enum HTTPMethod {
export enum HTTPMethod {
GET = 'get',
POST = 'post',
PUT = 'put',
DELETE = 'delete',
PATCH = 'patch',
}

async function axiosProvider(
export async function axiosProvider(
httpMethod: HTTPMethod,
url: string,
queryParams?: Object,
@@ -46,4 +46,3 @@ async function axiosProvider(
}
}

export default axiosProvider;