-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added getter queries for the Invoice API
- Loading branch information
Showing
13 changed files
with
146 additions
and
43 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
26 changes: 26 additions & 0 deletions
26
...Core/Logistics.Application.Tenant/Queries/Invoice/GetInvoiceById/GetInvoiceByIdHandler.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,26 @@ | ||
using Logistics.Application.Tenant.Mappers; | ||
using Logistics.Shared.Models; | ||
|
||
namespace Logistics.Application.Tenant.Queries; | ||
|
||
internal sealed class GetInvoiceByIdHandler : RequestHandler<GetInvoiceByIdQuery, ResponseResult<InvoiceDto>> | ||
{ | ||
private readonly ITenantRepository _tenantRepository; | ||
|
||
public GetInvoiceByIdHandler(ITenantRepository tenantRepository) | ||
{ | ||
_tenantRepository = tenantRepository; | ||
} | ||
|
||
protected override async Task<ResponseResult<InvoiceDto>> HandleValidated( | ||
GetInvoiceByIdQuery req, CancellationToken cancellationToken) | ||
{ | ||
var invoiceEntity = await _tenantRepository.GetAsync<Invoice>(req.Id); | ||
|
||
if (invoiceEntity is null) | ||
return ResponseResult<InvoiceDto>.CreateError($"Could not find an invoice with ID {req.Id}"); | ||
|
||
var invoiceDto = invoiceEntity.ToDto(); | ||
return ResponseResult<InvoiceDto>.CreateSuccess(invoiceDto); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Core/Logistics.Application.Tenant/Queries/Invoice/GetInvoiceById/GetInvoiceByIdQuery.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,9 @@ | ||
using Logistics.Shared.Models; | ||
using MediatR; | ||
|
||
namespace Logistics.Application.Tenant.Queries; | ||
|
||
public class GetInvoiceByIdQuery : IRequest<ResponseResult<InvoiceDto>> | ||
{ | ||
public string? Id { get; set; } | ||
} |
11 changes: 11 additions & 0 deletions
11
...re/Logistics.Application.Tenant/Queries/Invoice/GetInvoiceById/GetInvoiceByIdValidator.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,11 @@ | ||
using FluentValidation; | ||
|
||
namespace Logistics.Application.Tenant.Queries; | ||
|
||
internal sealed class GetInvoiceByIdValidator : AbstractValidator<GetInvoiceByIdQuery> | ||
{ | ||
public GetInvoiceByIdValidator() | ||
{ | ||
RuleFor(i => i.Id).NotEmpty(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Core/Logistics.Application.Tenant/Queries/Invoice/GetInvoices/GetInvoicesHandler.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,31 @@ | ||
using Logistics.Application.Tenant.Mappers; | ||
using Logistics.Shared.Models; | ||
|
||
namespace Logistics.Application.Tenant.Queries; | ||
|
||
internal sealed class GetInvoicesHandler : RequestHandler<GetInvoicesQuery, PagedResponseResult<InvoiceDto>> | ||
{ | ||
private readonly ITenantRepository _tenantRepository; | ||
|
||
public GetInvoicesHandler(ITenantRepository tenantRepository) | ||
{ | ||
_tenantRepository = tenantRepository; | ||
} | ||
|
||
protected override Task<PagedResponseResult<InvoiceDto>> HandleValidated( | ||
GetInvoicesQuery req, | ||
CancellationToken cancellationToken) | ||
{ | ||
var totalItems = _tenantRepository.Query<Invoice>().Count(); | ||
var specification = new FilterInvoicesByInterval(req.OrderBy, req.StartDate, req.EndDate, req.Descending); | ||
|
||
var invoicesDto = _tenantRepository.ApplySpecification(specification) | ||
.Skip((req.Page - 1) * req.PageSize) | ||
.Take(req.PageSize) | ||
.Select(i => i.ToDto()) | ||
.ToArray(); | ||
|
||
var totalPages = (int)Math.Ceiling(totalItems / (double)req.PageSize); | ||
return Task.FromResult(PagedResponseResult<InvoiceDto>.Create(invoicesDto, totalItems, totalPages)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Core/Logistics.Application.Tenant/Queries/Invoice/GetInvoices/GetInvoicesQuery.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,8 @@ | ||
using Logistics.Shared.Models; | ||
using MediatR; | ||
|
||
namespace Logistics.Application.Tenant.Queries; | ||
|
||
public class GetInvoicesQuery : PagedIntervalQuery, IRequest<PagedResponseResult<InvoiceDto>> | ||
{ | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Core/Logistics.Application.Tenant/Queries/Invoice/GetInvoices/GetInvoicesValidator.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 FluentValidation; | ||
|
||
namespace Logistics.Application.Tenant.Queries; | ||
|
||
internal sealed class GetInvoicesValidator : AbstractValidator<GetInvoicesQuery> | ||
{ | ||
public GetInvoicesValidator() | ||
{ | ||
RuleFor(i => i.StartDate).LessThan(i => i.EndDate); | ||
RuleFor(i => i.Page) | ||
.GreaterThanOrEqualTo(0); | ||
|
||
RuleFor(i => i.PageSize) | ||
.GreaterThanOrEqualTo(1); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Core/Logistics.Domain/Specifications/FilterInvoicesByInterval.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,26 @@ | ||
using System.Linq.Expressions; | ||
using Logistics.Domain.Entities; | ||
|
||
namespace Logistics.Domain.Specifications; | ||
|
||
public class FilterInvoicesByInterval : BaseSpecification<Invoice> | ||
{ | ||
public FilterInvoicesByInterval(string? orderProperty, DateTime? startPeriod, DateTime endPeriod, bool descending) | ||
{ | ||
Descending = descending; | ||
OrderBy = InitOrderBy(orderProperty); | ||
|
||
Criteria = i => | ||
i.Created >= startPeriod && i.Created <= endPeriod; | ||
} | ||
|
||
private static Expression<Func<Invoice, object>> InitOrderBy(string? propertyName) | ||
{ | ||
propertyName = propertyName?.ToLower(); | ||
return propertyName switch | ||
{ | ||
"paymentamount" => i => i.Payment.Amount, | ||
_ => i => i.Created | ||
}; | ||
} | ||
} |
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