-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
481 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 31 additions & 21 deletions
52
src/Wax.Core/Handlers/CommandHandlers/Customers/CreateCustomerCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,50 @@ | ||
using AutoMapper; | ||
using FluentValidation; | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Wax.Core.Data; | ||
using Wax.Core.Domain.Customers; | ||
using Wax.Core.Domain.Customers.Exceptions; | ||
using Wax.Core.Middlewares.FluentMessageValidator; | ||
using Wax.Core.Repositories; | ||
using Wax.Messages.Commands.Customers; | ||
|
||
namespace Wax.Core.Handlers.CommandHandlers.Customers | ||
namespace Wax.Core.Handlers.CommandHandlers.Customers; | ||
|
||
public class CreateCustomerCommandHandler : ICommandHandler<CreateCustomerCommand, CreateCustomerResponse> | ||
{ | ||
public class CreateCustomerCommandHandler : ICommandHandler<CreateCustomerCommand, CreateCustomerResponse> | ||
private readonly IMapper _mapper; | ||
private readonly ICustomerRepository _customerRepository; | ||
|
||
public CreateCustomerCommandHandler(IMapper mapper, ICustomerRepository customerRepository) | ||
{ | ||
private readonly IMapper _mapper; | ||
private readonly ICustomerRepository _customerRepository; | ||
_mapper = mapper; | ||
_customerRepository = customerRepository; | ||
} | ||
|
||
public CreateCustomerCommandHandler(IMapper mapper,ICustomerRepository customerRepository) | ||
{ | ||
_mapper = mapper; | ||
_customerRepository = customerRepository; | ||
} | ||
public async Task<CreateCustomerResponse> Handle(IReceiveContext<CreateCustomerCommand> context, | ||
CancellationToken cancellationToken) | ||
{ | ||
var isUnique = await _customerRepository.IsUniqueAsync(context.Message.Name); | ||
|
||
public async Task<CreateCustomerResponse> Handle(IReceiveContext<CreateCustomerCommand> context, | ||
CancellationToken cancellationToken) | ||
if (!isUnique) | ||
{ | ||
var existing = await _customerRepository.FindByNameAsync(context.Message.Name); | ||
throw new CustomerNameAlreadyExistsException(); | ||
} | ||
|
||
if (existing != null) | ||
{ | ||
throw new CustomerNameAlreadyExistsException(); | ||
} | ||
var customer = _mapper.Map<Customer>(context.Message); | ||
|
||
var customer = _mapper.Map<Customer>(context.Message); | ||
await _customerRepository.InsertAsync(customer, cancellationToken); | ||
|
||
await _customerRepository.InsertAsync(customer); | ||
return new CreateCustomerResponse { CustomerId = customer.Id }; | ||
} | ||
} | ||
|
||
return new CreateCustomerResponse { CustomerId = customer.Id }; | ||
} | ||
public class CreateCustomerCommandValidator : FluentMessageValidator<CreateCustomerCommand> | ||
{ | ||
public CreateCustomerCommandValidator() | ||
{ | ||
RuleFor(v => v.Name).NotEmpty().MaximumLength(64); | ||
RuleFor(v => v.Address).MaximumLength(512); | ||
RuleFor(v => v.Contact).MaximumLength(128); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
src/Wax.Core/Handlers/RequestHandlers/Customers/GetCustomerRequestHandler.cs
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/Wax.Core/Handlers/RequestHandlers/Customers/GetCustomersRequestHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using FluentValidation; | ||
using Mediator.Net.Context; | ||
using Mediator.Net.Contracts; | ||
using Wax.Core.Middlewares.FluentMessageValidator; | ||
using Wax.Core.Repositories; | ||
using Wax.Messages.Dtos.Customers; | ||
using Wax.Messages.Requests; | ||
using Wax.Messages.Requests.Customers; | ||
|
||
namespace Wax.Core.Handlers.RequestHandlers.Customers; | ||
|
||
public class GetCustomersRequestHandler : IRequestHandler<GetCustomersRequest, PaginatedResponse<CustomerShortInfo>> | ||
{ | ||
private readonly ICustomerRepository _customerRepository; | ||
|
||
public GetCustomersRequestHandler(ICustomerRepository customerRepository) | ||
{ | ||
_customerRepository = customerRepository; | ||
} | ||
|
||
public async Task<PaginatedResponse<CustomerShortInfo>> Handle(IReceiveContext<GetCustomersRequest> context, | ||
CancellationToken cancellationToken) | ||
{ | ||
var data = await _customerRepository.GetPaginatedListByProjectionAsync( | ||
c => new CustomerShortInfo | ||
{ | ||
Id = c.Id, | ||
Address = c.Address, | ||
Name = c.Name | ||
}, | ||
orderBy: o => o.Name, | ||
pageIndex: context.Message.PageIndex, | ||
pageSize: context.Message.PageSize, | ||
cancellationToken: cancellationToken); | ||
|
||
return new PaginatedResponse<CustomerShortInfo>(data); | ||
} | ||
} | ||
|
||
public class GetCustomersRequestValidator : FluentMessageValidator<GetCustomersRequest> | ||
{ | ||
public GetCustomersRequestValidator() | ||
{ | ||
RuleFor(v => v.PageIndex).GreaterThan(0); | ||
RuleFor(v => v.PageSize).GreaterThan(0); | ||
} | ||
} |
Oops, something went wrong.