Skip to content

Commit

Permalink
[SOA-029][Enhancement] Log the messages for entire the Controllers. (#54
Browse files Browse the repository at this point in the history
)
  • Loading branch information
LyQuocCuong authored Jul 23, 2023
1 parent 29c2b90 commit 8f49a79
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,66 @@ namespace eShopOnlineApiRestful.Abstracts
[Route("api")]
public abstract class AbstractApiController : ControllerBase
{
private readonly ILogService _logService;
protected readonly IServiceManager _services;
protected abstract string ChildClassName { get; }

protected AbstractApiController(ControllerParams controllerParams)
{
_services = controllerParams.Service;
_logService = controllerParams.LogService;
_services = controllerParams.ServiceManager;
}

[NonAction]
protected void LogInfoRequest()
{
_logService.LogInfo("=============================================================");
_logService.LogInfo($"[REQUEST][METHOD - {Request.Method}] PATH - {Request.Path}");
}

[NonAction]
protected void LogInfoResponse()
{
if (Response.StatusCode is
400 or 401 or 402 or 403 or 404 or 405 or 406)
{
_logService.LogWarning($"[RESPONSE][STATUS CODE - {Response.StatusCode}]");
}
else
{
_logService.LogInfo($"[RESPONSE][STATUS CODE - {Response.StatusCode}]");
}
}

[NonAction]
private string GenerateMessages(string methodName, string message)
{
return LogMessages.FormatMessageForController(ChildClassName, methodName, message);
}

[NonAction]
protected void LogDebug(string methodName, string message)
{
_logService.LogDebug(GenerateMessages(methodName, message));
}

[NonAction]
protected void LogError(string methodName, string message)
{
_logService.LogError(GenerateMessages(methodName, message));
}

[NonAction]
protected void LogInfo(string methodName, string message)
{
_logService.LogInfo(GenerateMessages(methodName, message));
}

[NonAction]
protected void LogWarning(string methodName, string message)
{
_logService.LogWarning(GenerateMessages(methodName, message));
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public sealed class CompanyController : AbstractApiController
{
protected override string ChildClassName => nameof(CompanyController);

public CompanyController(ControllerParams controllerParams) : base(controllerParams)
{
}
Expand All @@ -10,31 +12,48 @@ public CompanyController(ControllerParams controllerParams) : base(controllerPar
[Route("companies", Name = "GetAllCompanies")]
public IActionResult GetAllCompanies()
{
LogInfoRequest();

LogInfo(nameof(GetAllCompanies), LogMessages.MessageForExecutingMethod);
IEnumerable<CompanyDto> companyDtos = _services.Company.GetAll();

LogInfoResponse();
return Ok(companyDtos);
}

[HttpGet]
[Route("companies/{id:guid}", Name = "GetCompanyById")]
public IActionResult GetCompanyById([FromRoute]Guid id)
{
LogInfoRequest();

LogInfo(nameof(GetCompanyById), LogMessages.MessageForExecutingMethod);
CompanyDto? companyDto = _services.Company.GetById(id);
if (companyDto == null)
{
LogInfoResponse();
return NotFound();
}

LogInfoResponse();
return Ok(companyDto);
}

[HttpPut]
[Route("companies/{id:guid}", Name = "UpdateCompanyFully")]
public IActionResult UpdateCompanyFully([FromRoute]Guid id, [FromBody]CompanyForUpdateDto updateDto)
{
LogInfoRequest();

LogInfo(nameof(UpdateCompanyFully), LogMessages.MessageForExecutingMethod);
if (_services.Company.IsValidId(id) == false)
{
LogInfoResponse();
return NotFound();
}
bool result = _services.Company.UpdateFully(id, updateDto);

LogInfoResponse();
return NoContent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public sealed class CustomerController : AbstractApiController
{
protected override string ChildClassName => nameof(CustomerController);

public CustomerController(ControllerParams controllerParams) : base(controllerParams)
{
}
Expand All @@ -10,63 +12,97 @@ public CustomerController(ControllerParams controllerParams) : base(controllerPa
[Route("customers", Name = "GetAllCustomers")]
public IActionResult GetAllCustomers()
{
LogInfoRequest();

LogInfo(nameof(GetAllCustomers), LogMessages.MessageForExecutingMethod);
IEnumerable<CustomerDto> employeeDto = _services.Customer.GetAll();

LogInfoResponse();
return Ok(employeeDto);
}

[HttpGet]
[Route("customers/{id:guid}", Name = "GetCustomerById")]
public IActionResult GetCustomerById([FromRoute]Guid id)
{
LogInfoRequest();

LogInfo(nameof(GetCustomerById), LogMessages.MessageForExecutingMethod);
CustomerDto? employeeDto = _services.Customer.GetById(id);
if (employeeDto == null)
{
LogInfoResponse();
return NotFound();
}

LogInfoResponse();
return Ok(employeeDto);
}

[HttpPost]
[Route("customers", Name = "CreateCustomer")]
public IActionResult CreateCustomer([FromBody]CustomerForCreationDto creationDto)
{
LogInfoRequest();

LogInfo(nameof(CreateCustomer), LogMessages.MessageForExecutingMethod);
CustomerDto customerDto = _services.Customer.Create(creationDto);

LogInfoResponse();
return CreatedAtRoute("GetCustomerById", new { id = customerDto.Id }, customerDto);
}

[HttpPut]
[Route("customers/{id:guid}", Name = "UpdateCustomerFully")]
public IActionResult UpdateCustomerFully([FromRoute]Guid id, [FromBody]CustomerForUpdateDto updateDto)
{
LogInfoRequest();

LogInfo(nameof(UpdateCustomerFully), LogMessages.MessageForExecutingMethod);
if (_services.Customer.IsValidId(id) == false)
{
LogInfoResponse();
return NotFound();
}
bool result = _services.Customer.UpdateFully(id, updateDto);

LogInfoResponse();
return NoContent();
}

[HttpDelete]
[Route("customers/{id:guid}", Name = "DeleteCustomerSoftly")]
public IActionResult DeleteCustomerSoftly([FromRoute]Guid id)
{
LogInfoRequest();

LogInfo(nameof(DeleteCustomerSoftly), LogMessages.MessageForExecutingMethod);
bool result = _services.Customer.DeleteSoftly(id);
if (result == false)
{
LogInfoResponse();
return BadRequest();
}

LogInfoResponse();
return NoContent();
}

[HttpDelete]
[Route("admin/customers/{id:guid}", Name = "DeleteCustomerHard")]
public IActionResult DeleteCustomerHard([FromRoute]Guid id)
{
LogInfoRequest();

LogInfo(nameof(DeleteCustomerHard), LogMessages.MessageForExecutingMethod);
bool result = _services.Customer.DeleteHard(id);
if (result == false)
{
LogInfoResponse();
return BadRequest();
}

LogInfoResponse();
return NoContent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
public sealed class EmployeeController : AbstractApiController
{
protected override string ChildClassName => nameof(EmployeeController);

public EmployeeController(ControllerParams controllerParams) : base(controllerParams)
{
}
Expand All @@ -10,63 +12,94 @@ public EmployeeController(ControllerParams controllerParams) : base(controllerPa
[Route("employees", Name = "GetAllEmployees")]
public IActionResult GetAllEmployees()
{
LogInfoRequest();

LogInfo(nameof(GetAllEmployees), LogMessages.MessageForExecutingMethod);
IEnumerable<EmployeeDto> employeeDtos = _services.Employee.GetAll();

LogInfoResponse();
return Ok(employeeDtos);
}

[HttpGet]
[Route("employees/{id:guid}", Name = "GetEmployeeById")]
public IActionResult GetEmployeeById([FromRoute]Guid id)
{
LogInfoRequest();

LogInfo(nameof(GetEmployeeById), LogMessages.MessageForExecutingMethod);
EmployeeDto? employeeDto = _services.Employee.GetById(id);
if (employeeDto == null)
{
LogInfoResponse();
return NotFound();
}
LogInfoResponse();
return Ok(employeeDto);
}

[HttpPost]
[Route("employees", Name = "CreateEmployee")]
public IActionResult CreateEmployee([FromBody]EmployeeForCreationDto creationDto)
{
LogInfoRequest();

LogInfo(nameof(CreateEmployee), LogMessages.MessageForExecutingMethod);
EmployeeDto employeeDto = _services.Employee.Create(creationDto);

LogInfoResponse();
return CreatedAtRoute("GetEmployeeById", new { id = employeeDto.Id }, employeeDto);
}

[HttpPut]
[Route("employees/{id:guid}", Name = "UpdateEmployeeFully")]
public IActionResult UpdateEmployeeFully([FromRoute]Guid id, [FromBody]EmployeeForUpdateDto updateDto)
{
LogInfoRequest();

LogInfo(nameof(UpdateEmployeeFully), LogMessages.MessageForExecutingMethod);
if (_services.Employee.IsValidId(id) == false)
{
LogInfoResponse();
return NotFound();
}
bool result = _services.Employee.UpdateFully(id, updateDto);

LogInfoResponse();
return NoContent();
}

[HttpDelete]
[Route("employees/{id:guid}", Name = "DeleteEmployeeSoftly")]
public IActionResult DeleteEmployeeSoftly([FromRoute]Guid id)
{
LogInfoRequest();

LogInfo(nameof(DeleteEmployeeSoftly), LogMessages.MessageForExecutingMethod);
bool result = _services.Employee.DeleteSoftly(id);
if (result == false)
{
LogInfoResponse();
return BadRequest();
}
LogInfoResponse();
return NoContent();
}

[HttpDelete]
[Route("admin/employees/{id:guid}", Name = "DeleteEmployeeHard")]
public IActionResult DeleteEmployeeHard([FromRoute] Guid id)
{
LogInfoRequest();

LogInfo(nameof(DeleteEmployeeHard), LogMessages.MessageForExecutingMethod);
bool result = _services.Employee.DeleteHard(id);
if (result == false)
{
LogInfoResponse();
return BadRequest();
}
LogInfoResponse();
return NoContent();
}

Expand Down
Loading

0 comments on commit 8f49a79

Please sign in to comment.