Skip to content

Commit

Permalink
fixbug: checkout basket
Browse files Browse the repository at this point in the history
  • Loading branch information
datsunbae committed Apr 20, 2024
1 parent 7643bee commit 50a2a05
Show file tree
Hide file tree
Showing 24 changed files with 1,350 additions and 75 deletions.
33 changes: 16 additions & 17 deletions src/API/CleanArchitecture.Api/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace CleanArchitecture.Api.Controllers.V1
namespace CleanArchitecture.Api.Controllers.V1.Baskets
{
[ApiVersion(1)]
public class BasketController : BaseApiController
Expand Down Expand Up @@ -86,6 +86,12 @@ public async Task<IActionResult> ClearBasket()
public async Task<IActionResult> Checkout([FromBody] CheckoutCommand request)
{
var result = await Sender.Send(request);

if (result.IsFailure)
{
throw new BadRequestException(new List<Error> { result.Error });
}

return Ok(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace CleanArchitecture.Api.Controllers.V1;
namespace CleanArchitecture.Api.Controllers.V1.Categories;

[ApiVersion(1)]
public class CategoryController : BaseApiController
Expand Down Expand Up @@ -46,7 +46,7 @@ public async Task<IActionResult> GetCategoryById(Guid id)
{
Result<CategoryResponse> result = await Sender.Send(new GetCategoryByIdQuery(id));

if(result.IsFailure)
if (result.IsFailure)
{
throw new BadRequestException(new List<Error> { result.Error });
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public async Task<IActionResult> CreateCategory([FromBody] CreateCategoryCommand
[MustHavePermission(Action.Update, Resource.Categories)]
public async Task<IActionResult> UpdateCategory(Guid id, [FromBody] UpdateCategoryCommand request)
{
if(request.Id != id)
if (request.Id != id)
return BadRequest();

var result = await Sender.Send(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace CleanArchitecture.Api.Controllers.V1
namespace CleanArchitecture.Api.Controllers.V1.Orders
{
[ApiVersion(1)]
public class OrderController : BaseApiController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace CleanArchitecture.Api.Controllers.V1;
namespace CleanArchitecture.Api.Controllers.V1.Products;

[ApiVersion(1)]
public class ProductController : BaseApiController
Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task<IActionResult> UpdateProduct([FromBody] UpdateProductCommand r
if (result.IsFailure)
throw new BadRequestException(new List<Error> { result.Error });

return CreatedAtAction(nameof(GetProductById), new { Id = result.Value}, result.Value);
return CreatedAtAction(nameof(GetProductById), new { Id = result.Value }, result.Value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
<PackageReference Include="Serilog" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="Features\V1\Products\EventHandlers\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CleanArchitecture.Domain\CleanArchitecture.Domain.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<Result<Guid>> Handle(CheckoutCommand request, CancellationToke
Guid userId = _currentUser.GetUserId();

UserInformation userInfomation = new UserInformation(
request.UserInformation.UserName,
request.UserInformation.Name,
new Phone(request.UserInformation.Phone),
new Address(request.UserInformation.Address.Street, request.UserInformation.Address.City));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public CheckoutValidator()
.NotEmpty()
.NotNull().WithMessage("Phone number is required.")
.MinimumLength(10).WithMessage("Phone number must not be less than 10 characters.")
.MaximumLength(20).WithMessage("Phone number must not exceed 50 characters.")
.Matches(new Regex(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}")).WithMessage("Phone number not valid");
.MaximumLength(20).WithMessage("Phone number must not exceed 50 characters.");

RuleFor(c => c.UserInformation.Address)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanArchitecture.Application.Features.V1.Baskets.Models.Requests;

public sealed record UserInformationRequest(
string UserName,
string Name,
string Phone,
AddressRequest Address);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CleanArchitecture.Application.Common.Email;
using CleanArchitecture.Application.Common.Messaging;
using CleanArchitecture.Application.Features.Identities.Users;
using CleanArchitecture.Domain.AggregatesModels.Orders.Events;

namespace CleanArchitecture.Application.Features.V1.Orders.EventHandlers;

public sealed class OrderCreatedDomainEventHandler : IDomainEventHandler<OrderCreatedDomainEvent>
{
private readonly IMailService _mailService;
private readonly IUserService _userService;
public OrderCreatedDomainEventHandler(IMailService mailService, IUserService userService)
{
_mailService = mailService ?? throw new ArgumentNullException(nameof(mailService));
_userService = userService ?? throw new ArgumentNullException(nameof(userService));
}

public async Task Handle(OrderCreatedDomainEvent notification, CancellationToken cancellationToken)
{
var user = await _userService.GetAsync(notification.UserId, cancellationToken);
if (user is null)
return;

var mail = new MailRequest(new List<string> {
user.Email! },
"Thank you for your purchase",
"Thank you for ordering. We received your order and will begin processing it soon!");

await _mailService.SendAsync(mail, cancellationToken);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using CleanArchitecture.Domain.Common;

namespace CleanArchitecture.Domain.AggregatesModels.Orders.Events;

public sealed record OrderCreatedDomainEvent(Guid UserId) : IDomainEvent;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CleanArchitecture.Domain.AggregatesModels.Baskets;
using CleanArchitecture.Domain.AggregatesModels.Orders.Enums;
using CleanArchitecture.Domain.AggregatesModels.Orders.Events;
using CleanArchitecture.Domain.AggregatesModels.Shared;
using CleanArchitecture.Domain.Common;

Expand All @@ -8,6 +9,7 @@ namespace CleanArchitecture.Domain.AggregatesModels.Orders;
public sealed class Order : BaseEntityRoot
{
private readonly List<OrderItem> _orderItems = new();

private Order(Guid userId)

Check warning on line 13 in src/Core/CleanArchitecture.Domain/AggregatesModels/Orders/Order.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'UserInformation' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
UserId = userId;
Expand All @@ -33,13 +35,13 @@ public static Order Create(Basket basket, UserInformation userInfomation)
order.AddOrderItem(item.ProductId, item.Quantity, item.Product.Price);

Check warning on line 35 in src/Core/CleanArchitecture.Domain/AggregatesModels/Orders/Order.cs

View workflow job for this annotation

GitHub Actions / publish

Dereference of a possibly null reference.
}

order.RaiseDomainEvent(new OrderCreatedDomainEvent(order.UserId));

return order;
}

private void AddOrderItem(Guid productId, int quantity, decimal price)
{
OrderItem orderItem = _orderItems.FirstOrDefault(o => o.ProductId == productId);
if (orderItem is not null)
_orderItems.Add(OrderItem.Create(Id, productId, quantity, price));
_orderItems.Add(OrderItem.Create(Id, productId, quantity, price));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CleanArchitecture.Domain.AggregatesModels.Categories;
using CleanArchitecture.Domain.AggregatesModels.Products.Events;
using CleanArchitecture.Domain.Common;

namespace CleanArchitecture.Domain.AggregatesModels.Products;
Expand Down Expand Up @@ -71,8 +70,6 @@ public Product Update(
if (categoryId.HasValue && CategoryId.Equals(categoryId.Value) is not true && categoryId.Value != Guid.Empty)
CategoryId = categoryId.Value;

RaiseDomainEvent(new ProductUpdatedDomainEvent(Id));

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

public record UserInformation
{
public string UserName { get; private set; }
public string Name { get; private set; }
public Phone Phone { get; private set; }
public Address Address { get; private set; }

protected UserInformation() { }

Check warning on line 9 in src/Core/CleanArchitecture.Domain/AggregatesModels/Shared/UserInformation.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in src/Core/CleanArchitecture.Domain/AggregatesModels/Shared/UserInformation.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Phone' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in src/Core/CleanArchitecture.Domain/AggregatesModels/Shared/UserInformation.cs

View workflow job for this annotation

GitHub Actions / publish

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public UserInformation(string userName, Phone phone, Address address)
public UserInformation(string name, Phone phone, Address address)
{
UserName = userName;
Name = name;
Phone = phone;
Address = address;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Configure(EntityTypeBuilder<Order> builder)
.IsRequired();
});

userInformationBuider.Property(u => u.UserName)
userInformationBuider.Property(u => u.Name)
.IsRequired();
});
}
Expand Down
Loading

0 comments on commit 50a2a05

Please sign in to comment.