Skip to content

Commit 5ae8776

Browse files
committed
added getter queries for the Invoice API
1 parent a81cc9e commit 5ae8776

File tree

13 files changed

+146
-43
lines changed

13 files changed

+146
-43
lines changed

src/Core/Logistics.Application.Tenant/Commands/Invoice/CreateInvoice/CreateInvoiceCommand.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ namespace Logistics.Application.Tenant.Commands;
55

66
public class CreateInvoiceCommand : IRequest<ResponseResult>
77
{
8-
public string? CompanyName { get; set; }
9-
public string? CompanyAddress { get; set; }
108
public string CustomerId { get; set; } = default!;
119
public string LoadId { get; set; } = default!;
1210
public PaymentMethod PaymentMethod { get; set; }

src/Core/Logistics.Application.Tenant/Commands/Invoice/UpdateInvoice/UpdateInvoiceCommand.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ namespace Logistics.Application.Tenant.Commands;
66
public class UpdateInvoiceCommand : IRequest<ResponseResult>
77
{
88
public string Id { get; set; } = default!;
9-
public string? CompanyName { get; set; }
10-
public string? CompanyAddress { get; set; }
11-
public string CustomerId { get; set; } = default!;
12-
public string LoadId { get; set; } = default!;
13-
public PaymentMethod PaymentMethod { get; set; }
14-
public decimal PaymentAmount { get; set; }
15-
16-
public PaymentMethod? Method { get; set; }
17-
public decimal? Amount { get; set; }
18-
public PaymentStatus? Status { get; set; }
19-
public PaymentFor? PaymentFor { get; set; }
20-
public string? Comment { get; set; }
9+
public PaymentMethod? PaymentMethod { get; set; }
10+
public decimal? PaymentAmount { get; set; }
2111
}

src/Core/Logistics.Application.Tenant/Commands/Invoice/UpdateInvoice/UpdateInvoiceHandler.cs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Logistics.Application.Tenant.Commands;
22

3-
internal sealed class UpdateInvoiceHandler : RequestHandler<UpdatePaymentCommand, ResponseResult>
3+
internal sealed class UpdateInvoiceHandler : RequestHandler<UpdateInvoiceCommand, ResponseResult>
44
{
55
private readonly ITenantRepository _tenantRepository;
66

@@ -10,35 +10,23 @@ public UpdateInvoiceHandler(ITenantRepository tenantRepository)
1010
}
1111

1212
protected override async Task<ResponseResult> HandleValidated(
13-
UpdatePaymentCommand req, CancellationToken cancellationToken)
13+
UpdateInvoiceCommand req, CancellationToken cancellationToken)
1414
{
15-
var payment = await _tenantRepository.GetAsync<Payment>(req.Id);
15+
var invoice = await _tenantRepository.GetAsync<Invoice>(req.Id);
1616

17-
if (payment is null)
18-
return ResponseResult.CreateError($"Could not find a payment with ID '{req.Id}'");
19-
20-
if (req.PaymentFor.HasValue && payment.PaymentFor != req.PaymentFor)
21-
{
22-
payment.PaymentFor = req.PaymentFor.Value;
23-
}
24-
if (req.Method.HasValue && payment.Method != req.Method)
25-
{
26-
payment.Method = req.Method.Value;
27-
}
28-
if (req.Status.HasValue && payment.Status != req.Status)
29-
{
30-
payment.SetStatus(req.Status.Value);
31-
}
32-
if (req.Amount.HasValue && payment.Amount != req.Amount)
17+
if (invoice is null)
18+
return ResponseResult.CreateError($"Could not find an invoice with ID '{req.Id}'");
19+
20+
if (req.PaymentMethod.HasValue && invoice.Payment.Method != req.PaymentMethod)
3321
{
34-
payment.Amount = req.Amount.Value;
22+
invoice.Payment.Method = req.PaymentMethod.Value;
3523
}
36-
if (!string.IsNullOrEmpty(req.Comment) && payment.Comment != req.Comment)
24+
if (req.PaymentAmount.HasValue && invoice.Payment.Amount != req.PaymentAmount)
3725
{
38-
payment.Comment = req.Comment;
26+
invoice.Payment.Amount = req.PaymentAmount.Value;
3927
}
4028

41-
_tenantRepository.Update(payment);
29+
_tenantRepository.Update(invoice);
4230
await _tenantRepository.UnitOfWork.CommitAsync();
4331
return ResponseResult.CreateSuccess();
4432
}

src/Core/Logistics.Application.Tenant/Mappers/InvoiceMapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static InvoiceDto ToDto(this Invoice entity)
88
{
99
return new InvoiceDto
1010
{
11-
CustomerId = entity.CustomerId,
11+
Customer = entity.Customer.ToDto(),
1212
LoadId = entity.LoadId,
1313
Payment = entity.Payment.ToDto()
1414
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Logistics.Application.Tenant.Mappers;
2+
using Logistics.Shared.Models;
3+
4+
namespace Logistics.Application.Tenant.Queries;
5+
6+
internal sealed class GetInvoiceByIdHandler : RequestHandler<GetInvoiceByIdQuery, ResponseResult<InvoiceDto>>
7+
{
8+
private readonly ITenantRepository _tenantRepository;
9+
10+
public GetInvoiceByIdHandler(ITenantRepository tenantRepository)
11+
{
12+
_tenantRepository = tenantRepository;
13+
}
14+
15+
protected override async Task<ResponseResult<InvoiceDto>> HandleValidated(
16+
GetInvoiceByIdQuery req, CancellationToken cancellationToken)
17+
{
18+
var invoiceEntity = await _tenantRepository.GetAsync<Invoice>(req.Id);
19+
20+
if (invoiceEntity is null)
21+
return ResponseResult<InvoiceDto>.CreateError($"Could not find an invoice with ID {req.Id}");
22+
23+
var invoiceDto = invoiceEntity.ToDto();
24+
return ResponseResult<InvoiceDto>.CreateSuccess(invoiceDto);
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Logistics.Shared.Models;
2+
using MediatR;
3+
4+
namespace Logistics.Application.Tenant.Queries;
5+
6+
public class GetInvoiceByIdQuery : IRequest<ResponseResult<InvoiceDto>>
7+
{
8+
public string? Id { get; set; }
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using FluentValidation;
2+
3+
namespace Logistics.Application.Tenant.Queries;
4+
5+
internal sealed class GetInvoiceByIdValidator : AbstractValidator<GetInvoiceByIdQuery>
6+
{
7+
public GetInvoiceByIdValidator()
8+
{
9+
RuleFor(i => i.Id).NotEmpty();
10+
}
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Logistics.Application.Tenant.Mappers;
2+
using Logistics.Shared.Models;
3+
4+
namespace Logistics.Application.Tenant.Queries;
5+
6+
internal sealed class GetInvoicesHandler : RequestHandler<GetInvoicesQuery, PagedResponseResult<InvoiceDto>>
7+
{
8+
private readonly ITenantRepository _tenantRepository;
9+
10+
public GetInvoicesHandler(ITenantRepository tenantRepository)
11+
{
12+
_tenantRepository = tenantRepository;
13+
}
14+
15+
protected override Task<PagedResponseResult<InvoiceDto>> HandleValidated(
16+
GetInvoicesQuery req,
17+
CancellationToken cancellationToken)
18+
{
19+
var totalItems = _tenantRepository.Query<Invoice>().Count();
20+
var specification = new FilterInvoicesByInterval(req.OrderBy, req.StartDate, req.EndDate, req.Descending);
21+
22+
var invoicesDto = _tenantRepository.ApplySpecification(specification)
23+
.Skip((req.Page - 1) * req.PageSize)
24+
.Take(req.PageSize)
25+
.Select(i => i.ToDto())
26+
.ToArray();
27+
28+
var totalPages = (int)Math.Ceiling(totalItems / (double)req.PageSize);
29+
return Task.FromResult(PagedResponseResult<InvoiceDto>.Create(invoicesDto, totalItems, totalPages));
30+
}
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Logistics.Shared.Models;
2+
using MediatR;
3+
4+
namespace Logistics.Application.Tenant.Queries;
5+
6+
public class GetInvoicesQuery : PagedIntervalQuery, IRequest<PagedResponseResult<InvoiceDto>>
7+
{
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using FluentValidation;
2+
3+
namespace Logistics.Application.Tenant.Queries;
4+
5+
internal sealed class GetInvoicesValidator : AbstractValidator<GetInvoicesQuery>
6+
{
7+
public GetInvoicesValidator()
8+
{
9+
RuleFor(i => i.StartDate).LessThan(i => i.EndDate);
10+
RuleFor(i => i.Page)
11+
.GreaterThanOrEqualTo(0);
12+
13+
RuleFor(i => i.PageSize)
14+
.GreaterThanOrEqualTo(1);
15+
}
16+
}

0 commit comments

Comments
 (0)