Skip to content

Commit

Permalink
feat(order): add order enpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MayerliSantander committed Sep 26, 2024
1 parent f3d504d commit 0e8f312
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class BaseEntityController<TEntity, TCreateDto, TUpdateDto>(IRep
protected IRepository<TEntity> Repository { get; } = repository;

[HttpPost]
public async Task<IActionResult> Create(TCreateDto request)
public virtual async Task<IActionResult> Create(TCreateDto request)
{
Result validateResult = request.Validate();

Expand All @@ -37,15 +37,15 @@ public async Task<IActionResult> Create(TCreateDto request)
}

[HttpGet("{id}")]
public async Task<IActionResult> GetById([FromRoute] [Required] Guid id)
public virtual async Task<IActionResult> GetById([FromRoute] [Required] Guid id)
{
Result<TEntity> result = await Repository.GetByIdAsync(id);

return result.Match(entity => Ok(entity), this.ErrorsResponse);
}

[HttpPut("{id}")]
public async Task<IActionResult> Update([FromRoute] [Required] Guid id, TUpdateDto request)
public virtual async Task<IActionResult> Update([FromRoute] [Required] Guid id, TUpdateDto request)
{
Result validateResult = request.Validate();

Expand All @@ -69,7 +69,7 @@ public async Task<IActionResult> Update([FromRoute] [Required] Guid id, TUpdateD
}

[HttpPatch("{id}/disable")]
public async Task<IActionResult> Disable([FromRoute] [Required] Guid id)
public virtual async Task<IActionResult> Disable([FromRoute] [Required] Guid id)
{
Result<TEntity> searchEntity = await Repository.GetByIdAsync(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,23 @@ public class OrderController(
IEmailService emailService
) : BaseEntityController<Order, CreateOrderDto, UpdateOrderDto>(orderRepository)
{
[HttpPost]
public override async Task<IActionResult> Create(CreateOrderDto request)
{
IActionResult requestResult = await base.Create(request);

if (requestResult is OkObjectResult okResult && okResult.Value is Order order)
{
_ = await SendEmailByStatus(order.Id);

return Ok(order);
}

return requestResult;
}

[HttpGet("{id}/status")]
public async Task<IActionResult> SendEmailByStatus([FromRoute] [Required] Guid id)
public async Task<IActionResult> SendEmailByStatus([FromRoute][Required] Guid id)
{
Result<Order> orderResult = await Repository.GetByIdAsync(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ IConfiguration configuration
{ typeof(Order), new OrderTable(_.GetRequiredService<IDbConnectionFactory<IDbConnection>>()) },
{ typeof(Trip), new TripTable(_.GetRequiredService<IDbConnectionFactory<IDbConnection>>()) },
{ typeof(Strike), new StrikeTable(_.GetRequiredService<IDbConnectionFactory<IDbConnection>>()) },
{ typeof(OrderProduct), new OrderProductTable(_.GetRequiredService<IDbConnectionFactory<IDbConnection>>())}
}
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,85 @@ namespace DistributionCenter.Application.Repositories.Concretes;

using Bases;
using Contexts.Interfaces;
using Commons.Results;
using Domain.Entities.Concretes;

public class OrderRepository(IContext context) : BaseRepository<Order>(context) { }
public class OrderRepository(IContext context) : BaseRepository<Order>(context)
{
public override async Task<Result<Order>> GetByIdAsync(Guid id)
{
Result<Order> orderResult = await base.GetByIdAsync(id);

if (!orderResult.IsSuccess)
{
return orderResult.Errors;
}

Result<IEnumerable<OrderProduct>> orderProductsResult = await Context.SetTable<OrderProduct>().GetAll().ExecuteAsync();

if (!orderProductsResult.IsSuccess)
{
return orderProductsResult.Errors;
}

Order order = orderResult.Value;

IEnumerable<OrderProduct> productsOrder = orderProductsResult.Value.Where(p => p.OrderId == order.Id);

foreach (OrderProduct orderProduct in productsOrder)
{
Result<Product> product = await Context.SetTable<Product>().GetById(orderProduct.ProductId).ExecuteAsync();

if (!product.IsSuccess)
{
return product.Errors;
}

orderProduct.Product = product.Value;
}

order.Products = productsOrder;

return order;
}


public override async Task<Result<Order>> CreateAsync(Order entity)
{
IEnumerable<OrderProduct> products = entity.Products;

foreach (OrderProduct product in products)
{
Console.WriteLine(product.Id);
Console.WriteLine(product.OrderId);
Console.WriteLine(product.ProductId);
Console.WriteLine(product.Quantity);

Result resultInsert = await Context.SetTable<OrderProduct>().Create(product).ExecuteAsync();

if (!resultInsert.IsSuccess)
{
return resultInsert.Errors;
}
}

return await base.CreateAsync(entity);
}

public override async Task<Result<Order>> UpdateAsync(Order entity)
{
IEnumerable<OrderProduct> products = entity.Products;

foreach (OrderProduct product in products)
{
Result resultInsert = await Context.SetTable<OrderProduct>().Update(product).ExecuteAsync();

if (!resultInsert.IsSuccess)
{
return resultInsert.Errors;
}
}

return await base.UpdateAsync(entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

namespace DistributionCenter.Application.Tables.Components.Information.Concretes;

using Bases;

public class OrderProductTableInformation : BaseEntityTableInformation
{
public OrderProductTableInformation()
: base() { }

protected override string ObtainGetByIdFields()
{
return "quantity AS Quantity, product_id AS ProductId, client_order_id AS OrderId";
}

protected override string ObtainTableName()
{
return "client_order_product";
}

protected override string ObtainCreateFields()
{
return "quantity, product_id, client_order_id";
}

protected override string ObtainCreateValues()
{
return "@Quantity, @ProductId, @OrderId";
}

protected override string ObtainUpdateFields()
{
return "quantity = @Quantity, product_id = @ProductId, client_order_id = @OrderId";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace DistributionCenter.Application.Tables.Core.Concretes;

using System.Data;
using Bases;
using Components.Information.Concretes;
using Components.Information.Interfaces;
using Connections.Dapper.Interfaces;
using Domain.Entities.Concretes;

public class OrderProductTable(IDbConnectionFactory<IDbConnection> dbConnectionFactory) : BaseDapperTable<OrderProduct>(dbConnectionFactory)
{
public override ITableInformation GetInformation()
{
return new OrderProductTableInformation();
}
}
6 changes: 3 additions & 3 deletions src/DistributionCenter.Domain/Entities/Concretes/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace DistributionCenter.Domain.Entities.Concretes;

public class Order : BaseEntity
{
public required Guid RouteId { get; set; }
public Guid RouteId { get; set; } = Guid.Empty;
public required Guid ClientId { get; set; }
public required Guid DeliveryPointId { get; set; }
public ICollection<OrderProduct> Products { get; init; } = [];
public required Status Status { get; set; } = Status.Pending;
public IEnumerable<OrderProduct> Products { get; set; } = [];
public Status Status { get; set; } = Status.Pending;

public override bool Equals(object? obj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace DistributionCenter.Domain.Entities.Concretes;
public class OrderProduct : BaseEntity
{
public required Guid ProductId { get; set; }
public required Guid OrderId { get; set; }
public Guid OrderId { get; set; } = Guid.Empty;
public required int Quantity { get; set; }
public required Product Product { get; set; }
public Product Product { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace DistributionCenter.Infraestructure.DTOs.Concretes.Orders;

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/CreateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / build

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/CreateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / build

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/CreateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / Build Project (ubuntu-latest, 8.0.x)

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/CreateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / Build Project (ubuntu-latest, 8.0.x)

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

using Commons.Results;
using Domain.Entities.Concretes;
using Interfaces;

public class CreateOrderProductDto : ICreateDto<OrderProduct>
{
public required Guid ProductId { get; set; }
public required int Quantity { get; set; }

public OrderProduct ToEntity()
{
return new OrderProduct
{
ProductId = ProductId,
Quantity = Quantity
};
}

public Result Validate()
{
return Result.Ok();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace DistributionCenter.Infraestructure.DTOs.Concretes.Orders;

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/UpdateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / build

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/UpdateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / build

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/UpdateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / Build Project (ubuntu-latest, 8.0.x)

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

Check failure on line 1 in src/DistributionCenter.Infraestructure/DTOs/Concretes/OrderProducts/UpdateOrderProductDto.cs

View workflow job for this annotation

GitHub Actions / Build Project (ubuntu-latest, 8.0.x)

Namespace "DistributionCenter.Infraestructure.DTOs.Concretes.Orders" does not match folder structure, expected "DistributionCenter.Infraestructure.DTOs.Concretes.OrderProducts" (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0130)

using Commons.Results;
using Domain.Entities.Concretes;
using Interfaces;

public class UpdateOrderProductDto : IUpdateDto<OrderProduct>
{
public Guid ProductId { get; set; }
public int? Quantity { get; set; }

public OrderProduct FromEntity(OrderProduct entity)
{
entity.ProductId = ProductId;
entity.Quantity = Quantity ?? entity.Quantity;

return entity;
}

public Result Validate()
{
return Result.Ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,41 @@ namespace DistributionCenter.Infraestructure.DTOs.Concretes.Orders;

using Commons.Results;
using Domain.Entities.Concretes;
using Domain.Entities.Enums;
using Interfaces;
using Validators.Core.Concretes.Orders;

public class CreateOrderDto : ICreateDto<Order>
{
public required Guid RouteId { get; init; }
public required Guid ClientId { get; init; }
public required Guid DeliveryPointId { get; init; }
public required string Status { get; init; }
public required IEnumerable<CreateOrderProductDto> Products { get; init; }

public Order ToEntity()
{
_ = Enum.TryParse(Status, true, out Status parseStatus);
return new Order

Order order = new()
{
RouteId = RouteId,
ClientId = ClientId,
Status = parseStatus,
DeliveryPointId = DeliveryPointId,
DeliveryPointId = DeliveryPointId
};

List<OrderProduct> products = [];

foreach (CreateOrderProductDto product in Products)
{
OrderProduct orderProduct = new()
{
OrderId = order.Id,
ProductId = product.ProductId,
Quantity = product.Quantity
};

products.Add(orderProduct);
}

order.Products = products;

return order;
}

public Result Validate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@ namespace DistributionCenter.Infraestructure.DTOs.Concretes.Orders;
using Domain.Entities.Concretes;
using Domain.Entities.Enums;
using Interfaces;
using Validators.Core.Concretes.Orders;

public class UpdateOrderDto : IUpdateDto<Order>
{
public Guid? RouteId { get; set; }
public Guid? ClientId { get; set; }
public Guid? DeliveryPointId { get; set; }
public string? Status { get; init; }
public Status? Status { get; init; }
public IEnumerable<UpdateOrderProductDto> Products { get; init; } = [];


public Order FromEntity(Order entity)
{
ArgumentNullException.ThrowIfNull(entity, nameof(entity));
entity.Status = Status ?? entity.Status;

foreach (UpdateOrderProductDto product in Products)
{
if (!entity.Products.Any(p => p.ProductId == product.ProductId))
{
continue;
}

entity.RouteId = RouteId ?? entity.RouteId;
entity.ClientId = ClientId ?? entity.ClientId;
entity.DeliveryPointId = DeliveryPointId ?? entity.DeliveryPointId;
OrderProduct orderProduct = entity.Products.FirstOrDefault(p => p.ProductId == product.ProductId)!;

_ = Enum.TryParse(Status, true, out Status parseStatus);
entity.Status = Status != null ? parseStatus : entity.Status;
orderProduct.Quantity = product.Quantity ?? orderProduct.Quantity;
}

return entity;
}

public Result Validate()
{
return new UpdateOrderValidator().Validate(this);
return Result.Ok();
}
}
Loading

0 comments on commit 0e8f312

Please sign in to comment.