-
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.
Merge pull request #20 from snax4a/feature/inquiries-api
Feature/inquiries api
- Loading branch information
Showing
22 changed files
with
286 additions
and
34 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/Core/Application/Exchange/Inquiries/CreateInquiryRequest.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,75 @@ | ||
using MassTransit; | ||
|
||
namespace FSH.WebApi.Application.Exchange.Inquiries; | ||
|
||
public class UserInquiriesSpec : Specification<Inquiry>, ISingleResultSpecification | ||
{ | ||
public UserInquiriesSpec(Guid userId) => Query.Where(i => i.CreatedBy == userId); | ||
} | ||
|
||
public class CreateInquiryRequest : IRequest<Guid> | ||
{ | ||
public string Name { get; set; } = default!; | ||
public string Title { get; set; } = default!; | ||
public IList<Guid> RecipientIds { get; set; } = default!; | ||
public IList<InquiryProductDto> Products { get; set; } = default!; | ||
} | ||
|
||
public class InquiryProductValidator : CustomValidator<InquiryProductDto> | ||
{ | ||
public InquiryProductValidator() | ||
{ | ||
RuleFor(p => p.Name).NotEmpty().MinimumLength(3).MaximumLength(100); | ||
RuleFor(p => p.Quantity).NotEmpty().GreaterThan(0); | ||
RuleFor(p => p.PreferredDeliveryDate).NotEmpty().GreaterThanOrEqualTo(DateTime.UtcNow); | ||
} | ||
} | ||
|
||
public class CreateInquiryRequestValidator : CustomValidator<CreateInquiryRequest> | ||
{ | ||
public CreateInquiryRequestValidator(IStringLocalizer<CreateInquiryRequestValidator> localizer) | ||
{ | ||
RuleFor(i => i.Name).NotEmpty().MinimumLength(3).MaximumLength(60); | ||
RuleFor(i => i.Title).NotEmpty().MinimumLength(3).MaximumLength(100); | ||
|
||
RuleFor(i => i.RecipientIds) | ||
.Must(ids => ids.Count > 0) | ||
.WithMessage(localizer["inquiry.norecipients"]); | ||
|
||
RuleFor(i => i.Products) | ||
.Must(products => products.Count > 0) | ||
.WithMessage(localizer["inquiry.noproducts"]); | ||
|
||
RuleForEach(i => i.Products).SetValidator(new InquiryProductValidator()); | ||
} | ||
} | ||
|
||
public class CreateInquiryRequestHandler : IRequestHandler<CreateInquiryRequest, Guid> | ||
{ | ||
private readonly ICurrentUser _currentUser; | ||
|
||
// Add Domain Events automatically by using IRepositoryWithEvents | ||
private readonly IRepositoryWithEvents<Inquiry> _repository; | ||
|
||
public CreateInquiryRequestHandler(ICurrentUser currentUser, IRepositoryWithEvents<Inquiry> repository) => | ||
(_currentUser, _repository) = (currentUser, repository); | ||
|
||
public async Task<Guid> Handle(CreateInquiryRequest request, CancellationToken cancellationToken) | ||
{ | ||
Guid inquiryId = NewId.Next().ToGuid(); | ||
List<InquiryProduct> products = new(); | ||
|
||
foreach (InquiryProductDto product in request.Products) | ||
{ | ||
products.Add(new InquiryProduct(inquiryId, product.Name, product.Quantity, product.PreferredDeliveryDate)); | ||
} | ||
|
||
var spec = new UserInquiriesSpec(_currentUser.GetUserId()); | ||
int referenceNumber = await _repository.CountAsync(spec, cancellationToken); | ||
var inquiry = new Inquiry(inquiryId, referenceNumber + 1, request.Name, request.Title, products, request.RecipientIds); | ||
|
||
await _repository.AddAsync(inquiry, cancellationToken); | ||
|
||
return inquiry.Id; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Core/Application/Exchange/Inquiries/GetInquiryRequest.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,36 @@ | ||
namespace FSH.WebApi.Application.Exchange.Inquiries; | ||
|
||
public class GetInquiryRequest : IRequest<InquiryDetailsDto> | ||
{ | ||
public Guid Id { get; set; } | ||
|
||
public GetInquiryRequest(Guid id) => Id = id; | ||
} | ||
|
||
public class InquiryDetailsSpec : Specification<Inquiry, InquiryDetailsDto>, ISingleResultSpecification | ||
{ | ||
public InquiryDetailsSpec(Guid id, Guid userId) => | ||
Query | ||
.Where(i => i.Id == id && i.CreatedBy == userId) | ||
.Include(i => i.Products) | ||
.Include(i => i.InquiryRecipients); | ||
} | ||
|
||
public class GetInquiryRequestHandler : IRequestHandler<GetInquiryRequest, InquiryDetailsDto> | ||
{ | ||
private readonly ICurrentUser _currentUser; | ||
private readonly IRepository<Inquiry> _repository; | ||
private readonly IStringLocalizer<GetInquiryRequestHandler> _localizer; | ||
|
||
public GetInquiryRequestHandler(ICurrentUser currentUser, IRepository<Inquiry> repository, IStringLocalizer<GetInquiryRequestHandler> localizer) => (_currentUser, _repository, _localizer) = (currentUser, repository, localizer); | ||
|
||
public async Task<InquiryDetailsDto> Handle(GetInquiryRequest request, CancellationToken cancellationToken) | ||
{ | ||
ISpecification<Inquiry, InquiryDetailsDto> spec = new InquiryDetailsSpec(request.Id, _currentUser.GetUserId()); | ||
var inquiry = await _repository.GetBySpecAsync(spec, cancellationToken); | ||
|
||
if (inquiry is not null) return inquiry; | ||
|
||
throw new NotFoundException(string.Format(_localizer["inquiry.notfound"], request.Id)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Core/Application/Exchange/Inquiries/InquiryDetailsDto.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,16 @@ | ||
using FSH.WebApi.Application.Exchange.Traders; | ||
|
||
namespace FSH.WebApi.Application.Exchange.Inquiries; | ||
|
||
public class InquiryDetailsDto : IDto | ||
{ | ||
public Guid Id { get; set; } | ||
public int ReferenceNumber { get; set; } | ||
public string Name { get; set; } = default!; | ||
public string Title { get; set; } = default!; | ||
public DateTime CreatedOn { get; set; } | ||
public int RecipientCount { get; set; } | ||
public int OfferCount { get; set; } | ||
public IList<InquiryProductDetailsDto> Products { get; set; } = default!; | ||
public IList<TraderDto> Recipients { get; set; } = default!; | ||
} |
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,11 @@ | ||
namespace FSH.WebApi.Application.Exchange.Inquiries; | ||
|
||
public class InquiryDto : IDto | ||
{ | ||
public Guid Id { get; set; } | ||
public int ReferenceNumber { get; set; } | ||
public string Name { get; set; } = default!; | ||
public DateTime CreatedOn { get; set; } | ||
public int RecipientCount { get; set; } | ||
public int OfferCount { get; set; } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Core/Application/Exchange/Inquiries/InquiryProductDetailsDto.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,10 @@ | ||
namespace FSH.WebApi.Application.Exchange.Inquiries; | ||
|
||
public class InquiryProductDetailsDto : IDto | ||
{ | ||
public Guid Id { get; set; } | ||
public Guid InquiryId { get; set; } | ||
public string Name { get; set; } = default!; | ||
public int Quantity { get; set; } | ||
public DateTime PreferredDeliveryDate { get; set; } | ||
} |
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,8 @@ | ||
namespace FSH.WebApi.Application.Exchange.Inquiries; | ||
|
||
public class InquiryProductDto : IDto | ||
{ | ||
public string Name { get; set; } = default!; | ||
public int Quantity { get; set; } | ||
public DateTime PreferredDeliveryDate { get; set; } | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Core/Application/Exchange/Inquiries/SearchInquiriesRequest.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,32 @@ | ||
namespace FSH.WebApi.Application.Exchange.Inquiries; | ||
|
||
public class SearchInquiriesRequest : PaginationFilter, IRequest<PaginationResponse<InquiryDto>> | ||
{ | ||
} | ||
|
||
public class SearchInquiriesSpec : EntitiesByPaginationFilterSpec<Inquiry, InquiryDto> | ||
{ | ||
public SearchInquiriesSpec(SearchInquiriesRequest request, Guid userId) | ||
: base(request) => Query | ||
.Where(i => i.CreatedBy == userId) | ||
.OrderByDescending(i => i.ReferenceNumber, !request.HasOrderBy()); | ||
} | ||
|
||
public class SearchInquiriesRequestHandler : IRequestHandler<SearchInquiriesRequest, PaginationResponse<InquiryDto>> | ||
{ | ||
private readonly ICurrentUser _currentUser; | ||
private readonly IReadRepository<Inquiry> _repository; | ||
|
||
public SearchInquiriesRequestHandler(ICurrentUser currentUser, IReadRepository<Inquiry> repository) => | ||
(_currentUser, _repository) = (currentUser, repository); | ||
|
||
public async Task<PaginationResponse<InquiryDto>> Handle(SearchInquiriesRequest request, CancellationToken cancellationToken) | ||
{ | ||
var spec = new SearchInquiriesSpec(request, _currentUser.GetUserId()); | ||
|
||
var list = await _repository.ListAsync(spec, cancellationToken); | ||
int count = await _repository.CountAsync(spec, cancellationToken); | ||
|
||
return new PaginationResponse<InquiryDto>(list, count, request.PageNumber, request.PageSize); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using FSH.WebApi.Application.Exchange.Groups; | ||
|
||
namespace FSH.WebApi.Application.Exchange.Traders; | ||
|
||
public class TraderDetailsDto : TraderDto | ||
{ | ||
public ICollection<GroupDto> Groups { get; set; } = default!; | ||
} |
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
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
Oops, something went wrong.