Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: refatorar entidade Invoice #124

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Hotel.Domain/Controllers/InvoiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task<IActionResult> GetAsync(
[FromQuery] int? skip,
[FromQuery] int? take,
[FromQuery] string? number,
[FromQuery] EPaymentMethod? paymentMethod,
[FromQuery] string? paymentMethod,
[FromQuery] decimal? totalAmount,
[FromQuery] string? totalAmountOperator,
[FromQuery] EStatus? status,
Expand All @@ -42,9 +42,7 @@ [FromQuery] string? issueDateOperator
)
{
var queryParameters = new InvoiceQueryParameters(
skip, take, number, paymentMethod, totalAmount, totalAmountOperator, status,
customerId, reservationId, serviceId, taxInformation, taxInformationOperator,
issueDate, issueDateOperator
skip, take, paymentMethod, totalAmount, totalAmountOperator, customerId, reservationId, serviceId
);

return Ok(await _handler.HandleGetAsync(queryParameters));
Expand Down
17 changes: 6 additions & 11 deletions Hotel.Domain/DTOs/InvoiceDTOs/GetInvoice.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
using Hotel.Domain.Enums;

using Hotel.Domain.DTOs.ServiceDTOs;

namespace Hotel.Domain.DTOs.InvoiceDTOs;

public class GetInvoice : IDataTransferObject
{
public GetInvoice(Guid id, string number, EPaymentMethod paymentMethod, Guid reservationId, DateTime issueDate, decimal totalAmount, EStatus status, decimal taxInformation, Guid customerId)
public GetInvoice(Guid id, string paymentMethod, Guid reservationId, decimal totalAmount, Guid customerId, IEnumerable<GetService> services)
{
Id = id;
PaymentMethod = paymentMethod;
TaxInformation = taxInformation;
ReservationId = reservationId;
Number = number;
IssueDate = issueDate;
TotalAmount = totalAmount;
Status = status;
CustomerId = customerId;
Services = services;
}

public Guid? Id { get; private set; }
public EPaymentMethod PaymentMethod { get; private set; }
public decimal TaxInformation { get; private set; }
public string PaymentMethod { get; private set; }
public Guid ReservationId { get; private set; }
public string Number { get; private set; }
public DateTime IssueDate { get; private set; }
public decimal TotalAmount { get; private set; }
public EStatus Status { get; private set; }
public Guid CustomerId { get; private set; }
public IEnumerable<GetService> Services { get; private set; }

}
16 changes: 2 additions & 14 deletions Hotel.Domain/DTOs/InvoiceDTOs/InvoiceQueryParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,23 @@ namespace Hotel.Domain.DTOs.InvoiceDTOs;

public class InvoiceQueryParameters : IDataTransferObject
{
public InvoiceQueryParameters(int? skip, int? take, string? number, EPaymentMethod? paymentMethod, decimal? totalAmount, string? totalAmountOperator, EStatus? status, Guid? customerId, Guid? reservationId, Guid? serviceId, decimal? taxInformation, string? taxInformationOperator, DateTime? issueDate, string? issueDateOperator)
public InvoiceQueryParameters(int? skip, int? take, string? paymentMethod, decimal? totalAmount, string? totalAmountOperator, Guid? customerId, Guid? reservationId, Guid? serviceId)
{
Skip = skip;
Take = take;
Number = number;
IssueDate = issueDate;
TotalAmount = totalAmount;
Status = status;
PaymentMethod = paymentMethod;
CustomerId = customerId;
ReservationId = reservationId;
ServiceId = serviceId;
TaxInformation = taxInformation;
IssueDateOperator = issueDateOperator;
TotalAmountOperator = totalAmountOperator;
TaxInformationOperator = taxInformationOperator;
}
public int? Skip { get; private set; }
public int? Take { get; private set; }
public string? Number { get; private set; }
public DateTime? IssueDate { get; private set; }
public string? IssueDateOperator { get; private set; }
public decimal? TotalAmount { get; private set; }
public string? TotalAmountOperator { get; private set; }
public EStatus? Status { get; private set; }
public EPaymentMethod? PaymentMethod { get; private set; }
public string? PaymentMethod { get; private set; }
public Guid? CustomerId { get; set; }
public Guid? ReservationId { get; private set; }
public Guid? ServiceId { get; private set; }
public decimal? TaxInformation { get; private set; }
public string? TaxInformationOperator { get; private set; }
}
24 changes: 2 additions & 22 deletions Hotel.Domain/Data/Mappings/InvoiceMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,13 @@ public void Configure(EntityTypeBuilder<Invoice> builder)

builder.ToTable("Invoices");

builder.Property(x => x.Number)
.IsRequired()
.HasMaxLength(50);

builder.HasIndex(x => x.Number)
.IsUnique();

builder.Property(x => x.IssueDate)
.IsRequired();

builder.Property(x => x.TotalAmount)
.IsRequired()
.HasColumnType("DECIMAL(18,2)");
;

builder.Property(x => x.Status)
.IsRequired()
.HasConversion<int>();

builder.Property(x => x.PaymentMethod)
.IsRequired()
.HasConversion<int>();

builder.Property(x => x.TaxInformation)
.IsRequired()
.HasColumnType("DECIMAL(18,2)");

.IsRequired();

builder.Property(x => x.ReservationId)
.IsRequired()
.HasColumnType("UNIQUEIDENTIFIER");
Expand Down
7 changes: 1 addition & 6 deletions Hotel.Domain/Entities/Interfaces/IInvoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
using Hotel.Domain.Entities.CustomerEntity;
using Hotel.Domain.Entities.ReservationEntity;
using Hotel.Domain.Entities.ServiceEntity;
using Hotel.Domain.Enums;

namespace Hotel.Domain.Entities.Interfaces;

public interface IInvoice : IEntity
{
string Number { get; }
DateTime IssueDate { get; }
decimal TotalAmount { get; }
EStatus Status { get; }
EPaymentMethod PaymentMethod { get; }
decimal TaxInformation { get; }
string PaymentMethod { get; }
Customer? Customer { get; }
Guid ReservationId { get; }
Reservation? Reservation { get; }
Expand Down
16 changes: 3 additions & 13 deletions Hotel.Domain/Entities/InvoiceEntity/Invoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,28 @@
using Hotel.Domain.Entities.Interfaces;
using Hotel.Domain.Entities.ReservationEntity;
using Hotel.Domain.Entities.ServiceEntity;
using Hotel.Domain.Enums;

namespace Hotel.Domain.Entities.InvoiceEntity;

public partial class Invoice : Entity, IInvoice
{
internal Invoice() { }

public Invoice(EPaymentMethod paymentMethod, Reservation reservation, decimal taxInformation = 0)
public Invoice(Reservation reservation)
{
TotalAmount = reservation.TotalAmount();
Number = Guid.NewGuid().ToString(); //Serviço para gerar número
IssueDate = DateTime.Now;
Status = EStatus.Pending;
PaymentMethod = paymentMethod;
TaxInformation = taxInformation;
PaymentMethod = "card";
CustomerId = reservation.CustomerId;
Customer = reservation.Customer;
Reservation = reservation;
ReservationId = reservation.Id;
Services = reservation.Services;

Validate();

}

public string Number { get; private set; } = string.Empty;
public DateTime IssueDate { get; private set; }
public decimal TotalAmount { get; private set; }
public EStatus Status { get; private set; }
public EPaymentMethod PaymentMethod { get; private set; }
public decimal TaxInformation { get; private set; }
public string PaymentMethod { get; private set; } = null!;
public Guid CustomerId { get; private set; }
public Customer? Customer { get; private set; }
public Guid ReservationId { get; private set; }
Expand Down
13 changes: 0 additions & 13 deletions Hotel.Domain/Entities/InvoiceEntity/UpdateProperties.cs

This file was deleted.

33 changes: 0 additions & 33 deletions Hotel.Domain/Entities/InvoiceEntity/Validate.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Hotel.Domain/Entities/ReservationEntity/Finish.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Hotel.Domain.Entities.InvoiceEntity;
using Hotel.Domain.Enums;
using Hotel.Domain.Exceptions;

namespace Hotel.Domain.Entities.ReservationEntity;

Expand Down Expand Up @@ -28,7 +27,7 @@ public Invoice Finish()
UpdateCheckOut(DateTime.Now)
.ToCheckOut(); // Muda o status para CheckedOut

Invoice = new Invoice(EPaymentMethod.CreditCard, this, 0); //Gera uma instância de uma fatura
Invoice = new Invoice(this); //Gera uma instância de uma fatura

Room?.ChangeStatus(ERoomStatus.OutOfService); //troca o status do quarto para 'OutOfService'(Fora de serviço)

Expand Down
47 changes: 21 additions & 26 deletions Hotel.Domain/Repositories/InvoiceRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Hotel.Domain.Data;
using Hotel.Domain.DTOs.InvoiceDTOs;
using Hotel.Domain.DTOs.ServiceDTOs;
using Hotel.Domain.Entities.InvoiceEntity;
using Hotel.Domain.Extensions;
using Hotel.Domain.Repositories.Base;
Expand All @@ -12,14 +13,22 @@ public class InvoiceRepository : GenericRepository<Invoice>, IInvoiceRepository
{
public InvoiceRepository(HotelDbContext context) : base(context) { }


public async Task<GetInvoice?> GetByIdAsync(Guid id)
{
return await _context
.Invoices
.AsNoTracking()
.Where(x => x.Id == id)
.Select(x => new GetInvoice(x.Id, x.Number, x.PaymentMethod, x.ReservationId, x.IssueDate, x.TotalAmount, x.Status, x.TaxInformation, x.CustomerId))
.Include(x => x.Services)
.Select(x =>
new GetInvoice(
x.Id,
x.PaymentMethod,
x.ReservationId,
x.TotalAmount,
x.CustomerId,
x.Services.Select(s =>
new GetService(s.Id, s.Name, s.Price, s.Priority, s.IsActive, s.TimeInMinutes, s.CreatedAt))))
.FirstOrDefaultAsync();

}
Expand All @@ -28,19 +37,7 @@ public async Task<IEnumerable<GetInvoice>> GetAsync(InvoiceQueryParameters query
{
var query = _context.Invoices.AsQueryable();

if (queryParameters.Number != null)
query = query.Where(x => x.Number.Contains(queryParameters.Number));

if (queryParameters.IssueDate.HasValue)
query = query.FilterByOperator(queryParameters.IssueDateOperator, x => x.IssueDate, queryParameters.IssueDate);

if (queryParameters.TotalAmount.HasValue)
query = query.FilterByOperator(queryParameters.TotalAmountOperator, x => x.TotalAmount, queryParameters.TotalAmount);

if (queryParameters.Status.HasValue)
query = query.Where(x => x.Status == queryParameters.Status);

if (queryParameters.PaymentMethod.HasValue)
if (queryParameters.PaymentMethod != null)
query = query.Where(x => x.PaymentMethod == queryParameters.PaymentMethod);

if (queryParameters.CustomerId.HasValue)
Expand All @@ -52,22 +49,20 @@ public async Task<IEnumerable<GetInvoice>> GetAsync(InvoiceQueryParameters query
if (queryParameters.ServiceId.HasValue)
query = query.Where(x => x.Services.Any(x => x.Id == queryParameters.ServiceId));

if (queryParameters.TaxInformation.HasValue)
query = query.FilterByOperator(queryParameters.TaxInformationOperator, x => x.TaxInformation, queryParameters.TaxInformation);
if (queryParameters.TotalAmount.HasValue)
query = query.FilterByOperator(queryParameters.TotalAmountOperator, x => x.TotalAmount, queryParameters.TotalAmount);

query = query.Skip(queryParameters.Skip ?? 0).Take(queryParameters.Take ?? 1);
query = query.AsNoTracking();

return await query.Select(x => new GetInvoice(
x.Id,
x.Number,
x.PaymentMethod,
x.ReservationId,
x.IssueDate,
x.TotalAmount,
x.Status,
x.TaxInformation,
x.CustomerId
x.Id,
x.PaymentMethod,
x.ReservationId,
x.TotalAmount,
x.CustomerId,
x.Services.Select(s =>
new GetService(s.Id, s.Name, s.Price, s.Priority, s.IsActive, s.TimeInMinutes, s.CreatedAt))
)).ToListAsync();
}
}
39 changes: 0 additions & 39 deletions Hotel.Tests/UnitTests/Entities/InvoiceEntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,6 @@ namespace Hotel.Tests.UnitTests.Entities;
[TestClass]
public class InvoiceEntityTest
{

[TestMethod]
public void ValidInvoice_MustBeValid()
{
var room = new Room("Quarto",1, 50, 3, "Quarto padrão", TestParameters.Category);
var reservation = new Reservation(room, DateTime.Now.Date, DateTime.Now.AddDays(1), TestParameters.Customer, 2);

reservation.ToCheckIn();
reservation.Finish();
Assert.IsTrue(reservation?.Invoice?.IsValid);
}

[TestMethod]
[ExpectedException(typeof(ValidationException))]
public void InvoiceWithoutCheckOutStatusReservation_ExpectedException()
{
var room = new Room("Quarto",1, 50, 3, "Quarto padrão", TestParameters.Category);
var reservation = new Reservation(room, DateTime.Now.Date, DateTime.Now.AddDays(1), TestParameters.Customer, 2);
reservation.ToCheckIn();

new Invoice(EPaymentMethod.Pix, reservation);
Assert.Fail();
}

[TestMethod]
public void TheCustomersInReservation_MustBeSameOnInvoice()
{
Expand All @@ -45,19 +21,4 @@ public void TheCustomersInReservation_MustBeSameOnInvoice()
Assert.AreEqual(reservation.Customer, invoice.Customer);
}

[TestMethod]
public void ChangeToFinishInvoiceStatus_MustBeFinishStatus()
{
var room = new Room("Quarto",1, 50, 3, "Quarto padrão", TestParameters.Category);
var reservation = new Reservation(room, DateTime.Now.Date, DateTime.Now.AddDays(1), TestParameters.Customer, 2);
reservation.ToCheckIn();

var invoice = reservation.Finish();
invoice.FinishInvoice();
Assert.AreEqual(EStatus.Finish, invoice.Status);
}




}
Loading
Loading