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

[SOA-028][Enhancement] Log the messages for entire the Business (Service). #52

Merged
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
11 changes: 5 additions & 6 deletions Cores/Contracts/Utilities/Logger/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public static class LogMessages
public static readonly string MessageForExecutingWithDefaultCheckList = "Executing with the DEFAULT checklist.";
public static readonly string MessageForSettingAllConditionsInCheckListToFalse = "All of the Conditions in CheckList will be set to FALSE.";

public static readonly string MessageForHardDeleteSuccess = "";


public static string FormatMessageForValidatingItemPassed(string item)
{
return $"{item} - PASSED";
Expand All @@ -33,22 +32,22 @@ public static string FormatMessageForController(string className, string methodN

public static string FormatMessageForBusiness(string className, string methodName, string message)
{
return $"___|___[BUSINESS] {className}/{methodName}() - {message}";
return $" |__[BUSINESS] {className}/{methodName}() - {message}";
}

public static string FormatMessageForMappingService(string sourceType, string destinationType)
{
return $"___|___[MAPPING] Mapping from [{sourceType}] to [{destinationType}]";
return $" |__[MAPPING] Mapping from [{sourceType}] to [{destinationType}]";
}

public static string FormatMessageForRepository(string className, string methodName, string message)
{
return $"_______|___[REPOSITORY] {className}/{methodName}() - {message}";
return $" |__[REPOSITORY] {className}/{methodName}() - {message}";
}

public static string FormatMessageForEFCore(string message)
{
return $"___________|___[EFCORE] {message}";
return $" |__[EFCORE] {message}";
}

}
Expand Down
28 changes: 28 additions & 0 deletions Implementations/eShopOnlineBusiness/Abstracts/AbstractService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,42 @@ namespace eShopOnlineBusiness.Abstracts
{
public abstract class AbstractService : IAbstractService
{
private readonly ILogService _logService;
protected readonly IRepositoryManager _repository;
protected readonly IMapperService _mapperService;
protected abstract string ChildClassName { get; }

protected AbstractService(ServiceParams serviceParams)
{
_logService = serviceParams.LogService;
_repository = serviceParams.RepositoryManager;
_mapperService = serviceParams.MapperService;
}

private string GenerateMessages(string methodName, string message)
{
return LogMessages.FormatMessageForBusiness(ChildClassName, methodName, message);
}

public void LogDebug(string methodName, string message)
{
_logService.LogDebug(GenerateMessages(methodName, message));
}

public void LogError(string methodName, string message)
{
_logService.LogError(GenerateMessages(methodName, message));
}

public void LogInfo(string methodName, string message)
{
_logService.LogInfo(GenerateMessages(methodName, message));
}

public void LogWarning(string methodName, string message)
{
_logService.LogWarning(GenerateMessages(methodName, message));
}

}
}
23 changes: 18 additions & 5 deletions Implementations/eShopOnlineBusiness/Entities/CompanyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,54 @@
{
internal sealed class CompanyService : AbstractService, ICompanyService
{
protected override string ChildClassName => nameof(CompanyService);

public CompanyService(ServiceParams serviceParams) : base(serviceParams)
{
}

public IEnumerable<CompanyDto> GetAll()
{
LogInfo(nameof(GetAll), LogMessages.MessageForExecutingMethod);
IEnumerable<Company> companies = _repository.Company.GetAll(isTrackChanges: false);
return _mapperService.Execute<IEnumerable<Company>, IEnumerable<CompanyDto>>(companies);
}

public CompanyDto? GetById(Guid id)
{
LogInfo(nameof(GetById), LogMessages.MessageForExecutingMethod);
Company? company = _repository.Company.GetById(isTrackChanges: false, id);
if (company == null)
{
LogInfo(nameof(GetById), LogMessages.FormatMessageForObjectWithIdNotExistingInDatabase(nameof(Company), id.ToString()));
return null;
}
return _mapperService.Execute<Company, CompanyDto>(company);
}

public bool IsValidId(Guid id)
{
LogInfo(nameof(IsValidId), LogMessages.MessageForExecutingMethod);
return _repository.Company.IsValidId(id);
}

public bool UpdateFully(Guid id, CompanyForUpdateDto updateDto)
{
bool result = true;
LogInfo(nameof(UpdateFully), LogMessages.MessageForStartingMethodExecution);
Company? company = _repository.Company.GetById(isTrackChanges: true, id);
if (company == null)
if (company != null)
{
_mapperService.Execute<CompanyForUpdateDto, Company>(updateDto, company);
_repository.SaveChanges();
}
else
{
throw new Exception();
LogInfo(nameof(UpdateFully), LogMessages.FormatMessageForObjectWithIdNotExistingInDatabase(nameof(Company), id.ToString()));
result = false;
}
_mapperService.Execute<CompanyForUpdateDto, Company>(updateDto, company);
_repository.SaveChanges();
return true;
LogInfo(nameof(UpdateFully), LogMessages.MessageForFinishingMethodExecution);
return result;
}

}
Expand Down
82 changes: 59 additions & 23 deletions Implementations/eShopOnlineBusiness/Entities/CustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,41 @@
{
internal sealed class CustomerService : AbstractService, ICustomerService
{
protected override string ChildClassName => nameof(CustomerService);

public CustomerService(ServiceParams serviceParams) : base(serviceParams)
{
}

public IEnumerable<CustomerDto> GetAll()
{
LogInfo(nameof(GetAll), LogMessages.MessageForExecutingMethod);
IEnumerable<Customer> customers = _repository.Customer.GetAll(isTrackChanges: false);
return _mapperService.Execute<IEnumerable<Customer>, IEnumerable<CustomerDto>>(customers);
}

public bool IsValidId(Guid id)
{
LogInfo(nameof(IsValidId), LogMessages.MessageForExecutingMethod);
return _repository.Customer.IsValidId(id);
}

public CustomerDto? GetById(Guid id)
{
LogInfo(nameof(GetById), LogMessages.MessageForExecutingMethod);
Customer? customer = _repository.Customer.GetById(isTrackChanges: false, id);
if (customer == null)
{
LogInfo(nameof(GetById), LogMessages.FormatMessageForObjectWithIdNotExistingInDatabase(nameof(Customer), id.ToString()));
return null;
}
return _mapperService.Execute<Customer, CustomerDto>(customer);
}

public CustomerDto Create(CustomerForCreationDto creationDto)
{
LogInfo(nameof(Create), LogMessages.MessageForExecutingMethod);

Customer newCustomer = _mapperService.Execute<CustomerForCreationDto, Customer>(creationDto);
_repository.Customer.Create(newCustomer);
_repository.SaveChanges();
Expand All @@ -38,48 +46,76 @@ public CustomerDto Create(CustomerForCreationDto creationDto)

public bool UpdateFully(Guid id, CustomerForUpdateDto updateDto)
{
bool result = true;
LogInfo(nameof(UpdateFully), LogMessages.MessageForStartingMethodExecution);
Customer? customer = _repository.Customer.GetById(isTrackChanges: true, id);
if (customer == null)
if (customer != null)
{
throw new Exception();
_mapperService.Execute<CustomerForUpdateDto, Customer>(updateDto, customer);
_repository.SaveChanges();
}
_mapperService.Execute<CustomerForUpdateDto, Customer>(updateDto, customer);
_repository.SaveChanges();
return true;
else
{
LogInfo(nameof(UpdateFully), LogMessages.FormatMessageForObjectWithIdNotExistingInDatabase(nameof(Customer), id.ToString()));
result = false;
}
LogInfo(nameof(UpdateFully), LogMessages.MessageForFinishingMethodExecution);
return result;
}

public bool DeleteSoftly(Guid id)
{
var result = _repository.Customer.CheckRequiredConditionsForDeletion(id);
if (result.Any(condition => condition.Value == false))
bool result = true;
LogInfo(nameof(DeleteSoftly), LogMessages.MessageForStartingMethodExecution);
var resultCheckList = _repository.Customer.CheckRequiredConditionsForDeletion(id);
if (resultCheckList.Any(condition => condition.Value == false))
{
return false;
result = false;
}
Customer? customer = _repository.Customer.GetById(isTrackChanges: true, id);
if (customer != null)
else
{
_repository.Customer.DeleteSoftly(customer);
_repository.SaveChanges();
return true;
Customer? customer = _repository.Customer.GetById(isTrackChanges: true, id);
if (customer != null)
{
_repository.Customer.DeleteSoftly(customer);
_repository.SaveChanges();
}
else
{
LogInfo(nameof(DeleteSoftly), LogMessages.FormatMessageForObjectWithIdNotExistingInDatabase(nameof(Customer), id.ToString()));
result = false;
}
}
return false;
LogInfo(nameof(DeleteSoftly), LogMessages.MessageForFinishingMethodExecution);
return result;
}

public bool DeleteHard(Guid id)
{
var result = _repository.Customer.CheckRequiredConditionsForDeletion(id);
if (result.Any(condition => condition.Value == false))
bool result = true;
LogInfo(nameof(DeleteHard), LogMessages.MessageForStartingMethodExecution);
var resultCheckList = _repository.Customer.CheckRequiredConditionsForDeletion(id);
if (resultCheckList.Any(condition => condition.Value == false))
{
return false;
result = false;
}
Customer? customer = _repository.Customer.GetById(isTrackChanges: true, id);
if (customer != null)
else
{
_repository.Customer.DeleteHard(customer);
_repository.SaveChanges();
return true;
Customer? customer = _repository.Customer.GetById(isTrackChanges: true, id);
if (customer != null)
{
_repository.Customer.DeleteHard(customer);
_repository.SaveChanges();
result = true;
}
else
{
LogInfo(nameof(DeleteHard), LogMessages.FormatMessageForObjectWithIdNotExistingInDatabase(nameof(Customer), id.ToString()));
result = false;
}
}
return false;
LogInfo(nameof(DeleteHard), LogMessages.MessageForFinishingMethodExecution);
return result;
}

}
Expand Down
Loading