Skip to content

Commit

Permalink
feature: adicionar logs com Serilog
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoKroetz committed Jul 12, 2024
1 parent 5015e91 commit a5cac99
Show file tree
Hide file tree
Showing 62 changed files with 555 additions and 157 deletions.
10 changes: 9 additions & 1 deletion Hotel.Domain/Handlers/AdminHandlers/HandleCreateAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public partial class AdminHandler : GenericUserHandler<IAdminRepository, Admin>,
private readonly IAdminRepository _repository;
private readonly IPermissionRepository _permissionRepository;
private readonly IEmailService _emailService;
private readonly ILogger<AdminHandler> _logger;

public AdminHandler(IAdminRepository repository, IPermissionRepository permissionRepository, IEmailService emailService) : base(repository, emailService)
public AdminHandler(IAdminRepository repository, IPermissionRepository permissionRepository, IEmailService emailService, ILogger<AdminHandler> logger) : base(repository, emailService)
{
_repository = repository;
_permissionRepository = permissionRepository;
_emailService = emailService;
_logger = logger;
}

public async Task<Response> HandleCreateAsync(CreateUser model, string? code)
Expand Down Expand Up @@ -60,10 +62,16 @@ public async Task<Response> HandleCreateAsync(CreateUser model, string? code)
{

if (innerException.Contains("Email"))
{
_logger.LogError("Erro ao cadastradar administrador pois o email já está cadastrado");
throw new ArgumentException("Esse email já está cadastrado.");
}

if (innerException.Contains("Phone"))
{
_logger.LogError("Erro ao cadastradar administrador pois o telefone já está cadastrado");
throw new ArgumentException("Esse telefone já está cadastrado.");
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions Hotel.Domain/Handlers/AdminHandlers/HandleUpdateAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ public async Task<Response> HandleUpdateAsync(UpdateUser model, Guid adminId)
{

if (innerException.Contains("Email"))
{
_logger.LogError("Erro ao cadastradar administrador pois o email já está cadastrado");
throw new ArgumentException("Esse email já está cadastrado.");
}

if (innerException.Contains("Phone"))
{
_logger.LogError("Erro ao cadastradar administrador pois o telefone já está cadastrado");
throw new ArgumentException("Esse telefone já está cadastrado.");
}
}
}

Expand Down
14 changes: 11 additions & 3 deletions Hotel.Domain/Handlers/CustomerHandlers/HandleCreateCustomer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public partial class CustomerHandler : GenericUserHandler<ICustomerRepository, H
private readonly ICustomerRepository _repository;
private readonly IEmailService _emailService;
private readonly IStripeService _stripeService;
private readonly ILogger<CustomerHandler> _logger;

public CustomerHandler(ICustomerRepository repository, IEmailService emailService, IStripeService stripeService) : base(repository, emailService)
public CustomerHandler(ICustomerRepository repository, IEmailService emailService, IStripeService stripeService, ILogger<CustomerHandler> logger) : base(repository, emailService)
{
_repository = repository;
_emailService = emailService;
_stripeService = stripeService;
_logger = logger;
}

public async Task<Response> HandleCreateAsync(CreateUser model, string? code)
Expand Down Expand Up @@ -57,14 +59,19 @@ public async Task<Response> HandleCreateAsync(CreateUser model, string? code)

if (innerException != null)
{

if (innerException.Contains("Email"))
{
_logger.LogError("Erro ao cadastradar cliente pois o email já está cadastrado");
throw new ArgumentException("Esse email já está cadastrado.");
}

if (innerException.Contains("Phone"))
{
_logger.LogError("Erro ao cadastradar cliente pois o telefone já está cadastrado");
throw new ArgumentException("Esse telefone já está cadastrado.");
}
}
throw new DbUpdateException("Ocorreu um erro ao criar o cliente no banco de dados");
throw;
}

try
Expand All @@ -76,6 +83,7 @@ public async Task<Response> HandleCreateAsync(CreateUser model, string? code)
}
catch (StripeException)
{
_logger.LogError("Erro ao criar cliente no stripe");
throw new StripeException("Ocorreu um erro ao criar o cliente no Stripe");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public async Task<Response> HandleDeleteAsync(Guid id)
}
catch (DbUpdateException)
{
_logger.LogError("Erro ao deletar cliente do banco de dados");
throw new DbUpdateException("Ocorreu um erro ao deletar o usuário do banco de dados");
}

Expand All @@ -32,6 +33,7 @@ public async Task<Response> HandleDeleteAsync(Guid id)
}
catch (StripeException)
{
_logger.LogError("Erro ao deletar cliente do stripe");
throw new StripeException("Ocorreu um erro ao deletar o cliente no Stripe");
}

Expand Down
12 changes: 9 additions & 3 deletions Hotel.Domain/Handlers/CustomerHandlers/HandleUpdateCustomer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ public async Task<Response> HandleUpdateAsync(UpdateUser model, Guid id)

if (innerException != null)
{

if (innerException.Contains("Email"))
return new Response("Esse email já está cadastrado.");
{
_logger.LogError("Erro ao cadastradar cliente pois o email já está cadastrado");
throw new ArgumentException("Esse email já está cadastrado.");
}

if (innerException.Contains("Phone"))
return new Response("Esse telefone já está cadastrado.");
{
_logger.LogError("Erro ao cadastradar cliente pois o telefone já está cadastrado");
throw new ArgumentException("Esse telefone já está cadastrado.");
}
}
throw;
}
Expand All @@ -51,6 +56,7 @@ public async Task<Response> HandleUpdateAsync(UpdateUser model, Guid id)
}
catch (StripeException)
{
_logger.LogError("Erro ao atualizar cliente no stripe");
throw new StripeException("Ocorreu um erro ao atualizar o usuário no Stripe");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public partial class CustomerHandler
}
catch (StripeException)
{
_logger.LogError("Erro ao atualizar cliente no stripe");
throw new StripeException("Ocorreu um erro ao atualizar o cliente no Stripe");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class CustomerHandler
}
catch (StripeException)
{
_logger.LogError("Erro ao atualizar cliente no stripe");
throw new StripeException("Ocorreu um erro ao atualizar o cliente no Stripe");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class CustomerHandler
}
catch (StripeException)
{
_logger.LogError("Erro ao atualizar cliente no stripe");
throw new StripeException("Ocorreu um erro ao atualizar o cliente no Stripe");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public partial class CustomerHandler
}
catch (StripeException)
{
_logger.LogError("Erro ao atualizar cliente no stripe");
throw new StripeException("Ocorreu um erro ao atualizar o cliente no Stripe");
}

Expand Down
11 changes: 9 additions & 2 deletions Hotel.Domain/Handlers/EmployeeHandlers/HandleCreateEmployee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public partial class EmployeeHandler : GenericUserHandler<IEmployeeRepository, E
private readonly IResponsibilityRepository _responsibilityRepository;
private readonly IPermissionRepository _permissionRepository;
private readonly IEmailService _emailService;
private readonly ILogger<EmployeeHandler> _logger;

public EmployeeHandler(IEmployeeRepository repository, IResponsibilityRepository responsibilityRepository, IPermissionRepository permissionRepository, IEmailService emailService) : base(repository, emailService)
public EmployeeHandler(IEmployeeRepository repository, IResponsibilityRepository responsibilityRepository, IPermissionRepository permissionRepository, IEmailService emailService, ILogger<EmployeeHandler> logger) : base(repository, emailService)
{
_repository = repository;
_responsibilityRepository = responsibilityRepository;
_permissionRepository = permissionRepository;
_emailService = emailService;
_logger = logger;
}

public async Task<Response> HandleCreateAsync(CreateEmployee model, string? code)
Expand Down Expand Up @@ -58,14 +60,19 @@ public async Task<Response> HandleCreateAsync(CreateEmployee model, string? code
if (innerException != null)
{
if (innerException.Contains("Email"))
{
_logger.LogError("Erro ao cadastradar administrador pois o email já está cadastrado");
throw new ArgumentException("Esse email já está cadastrado.");
}

if (innerException.Contains("Phone"))
{
_logger.LogError("Erro ao cadastradar administrador pois o telefone já está cadastrado");
throw new ArgumentException("Esse telefone já está cadastrado.");
}
}
}


return new Response("Funcionário cadastrado com sucesso!", new { employee.Id });
}
}
13 changes: 9 additions & 4 deletions Hotel.Domain/Handlers/EmployeeHandlers/HandleUpdateEmployee.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class EmployeeHandler
public async Task<Response> HandleUpdateAsync(UpdateEmployee model, Guid id)
{
var employee = await _repository.GetEntityByIdAsync(id)
?? throw new NotFoundException("Funcionário não encontrado.");
?? throw new NotFoundException("Funcionário não encontrado.");

employee.ChangeName(new Name(model.FirstName, model.LastName));
employee.ChangePhone(new Phone(model.Phone));
Expand All @@ -31,12 +31,17 @@ public async Task<Response> HandleUpdateAsync(UpdateEmployee model, Guid id)

if (innerException != null)
{

if (innerException.Contains("Email"))
return new Response("Esse email já está cadastrado.");
{
_logger.LogError("Erro ao cadastradar administrador pois o email já está cadastrado");
throw new ArgumentException("Esse email já está cadastrado.");
}

if (innerException.Contains("Phone"))
return new Response("Esse telefone já está cadastrado.");
{
_logger.LogError("Erro ao cadastradar administrador pois o telefone já está cadastrado");
throw new ArgumentException("Esse telefone já está cadastrado.");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ public async Task<Response> HandleAddDeslikeAsync(Guid feedbackId, Guid customer

await _dislikeRepository.CreateDeslike(dislike);

await _feedbackRepository.SaveChangesAsync();
try
{
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao criar um dislike: {ex.Message}");
}

return new Response("Deslike adicionado com sucesso!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ public async Task<Response> HandleRemoveDeslikeAsync(Guid feedbackId, Guid custo

_dislikeRepository.RemoveDeslike(dislike);

await _feedbackRepository.SaveChangesAsync();
try
{
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao deletar um dislike: {ex.Message}");
}

return new Response("Deslike removido com sucesso!");
}
Expand Down
17 changes: 14 additions & 3 deletions Hotel.Domain/Handlers/FeedbackHandlers/HandleCreateFeedback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ public partial class FeedbackHandler : IHandler
private readonly IRoomRepository _roomRepository;
private readonly ILikeRepository _likeRepository;
private readonly IDeslikeRepository _dislikeRepository;
private readonly ILogger<FeedbackHandler> _logger;

public FeedbackHandler(IFeedbackRepository feedbackRepository, ICustomerRepository customerRepository, IReservationRepository reservationRepository, IRoomRepository roomRepository, ILikeRepository likeRepository, IDeslikeRepository dislikeRepository)

public FeedbackHandler(IFeedbackRepository feedbackRepository, ICustomerRepository customerRepository, IReservationRepository reservationRepository, IRoomRepository roomRepository, ILikeRepository likeRepository, IDeslikeRepository dislikeRepository, ILogger<FeedbackHandler> logger)
{
_feedbackRepository = feedbackRepository;
_customerRepository = customerRepository;
_reservationRepository = reservationRepository;
_roomRepository = roomRepository;
_likeRepository = likeRepository;
_dislikeRepository = dislikeRepository;
_logger = logger;
}

public async Task<Response> HandleCreateAsync(CreateFeedback model, Guid userId)
Expand All @@ -43,8 +46,16 @@ public async Task<Response> HandleCreateAsync(CreateFeedback model, Guid userId)
model.Comment, model.Rate, customer.Id, model.ReservationId, room.Id, reservation //vai validar se o cliente que está criando o feedback está na reserva
);

await _feedbackRepository.CreateAsync(feedback);
await _feedbackRepository.SaveChangesAsync();
try
{
await _feedbackRepository.CreateAsync(feedback);
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao criar o feedback no banco de dados: {ex.Message}");
}


return new Response("Feedback criado com sucesso!", new { feedback.Id });
}
Expand Down
12 changes: 10 additions & 2 deletions Hotel.Domain/Handlers/FeedbackHandlers/HandleDeleteFeedback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ public async Task<Response> HandleDeleteAsync(Guid id, Guid customerId)
if (feedback.CustomerId != customerId)
throw new ArgumentException("Apenas o autor do feedback pode excluí-lo.");

_feedbackRepository.Delete(feedback);
await _feedbackRepository.SaveChangesAsync();
try
{
_feedbackRepository.Delete(feedback);
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao deletar o feedback no banco de dados: {ex.Message}");
}

return new Response("Feedback deletado com sucesso!.", new { id });
}
}
11 changes: 9 additions & 2 deletions Hotel.Domain/Handlers/FeedbackHandlers/HandleUpdateFeedback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ public async Task<Response> HandleUpdateAsync(UpdateFeedback model, Guid id, Gui
feedback.ChangeComment(model.Comment);
feedback.ChangeRate(model.Rate);

_feedbackRepository.Update(feedback);
await _feedbackRepository.SaveChangesAsync();
try
{
_feedbackRepository.Update(feedback);
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao atualizar o feedback no banco de dados: {ex.Message}");
}

return new Response("Feedback atualizado com sucesso!", new { feedback.Id });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ public async Task<Response> HandleUpdateCommentAsync(Guid id, string comment, Gu

feedback.ChangeComment(comment);

await _feedbackRepository.SaveChangesAsync();
try
{
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao atualizar o feedback no banco de dados: {ex.Message}");
}

return new Response("Feedback atualizado com sucesso!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ public async Task<Response> HandleUpdateRateAsync(Guid id, int rate, Guid custom

feedback.ChangeRate(rate);

await _feedbackRepository.SaveChangesAsync();
try
{
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao atualizar o feedback no banco de dados: {ex.Message}");
}

return new Response("Feedback atualizado com sucesso!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ public async Task<Response> HandleAddLikeAsync(Guid feedbackId, Guid customerId)

await _likeRepository.CreateLike(like);

await _feedbackRepository.SaveChangesAsync();
try
{
await _feedbackRepository.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError($"Ocorreu um erro ao criar um like no banco de dados: {ex.Message}");
}

return new Response("Like adicionado com sucesso!");
}
Expand Down
Loading

0 comments on commit a5cac99

Please sign in to comment.