2023-05-17 10:48:52 +04:00

125 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.BusinessLogicContracts;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
namespace TaskTrackerBusinessLogic
{
public class CompanyLogic : ICompanyLogic
{
private readonly ILogger _logger;
private readonly ICompanyStorage _companyStorage;
public CompanyLogic(ILogger<CompanyLogic> logger, ICompanyStorage
companyStorage)
{
_logger = logger;
_companyStorage = companyStorage;
}
public List<CompanyViewModel>? ReadList(CompanySearchModel? model)
{
_logger.LogInformation("ReadList. CompanyName:{CompanyName}.Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _companyStorage.GetFullList() :
_companyStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public CompanyViewModel? ReadElement(CompanySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. CompanyName:{CompanyName}. Id:{ Id}", model.Name, model.Id);
var element = _companyStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(CompanyBindingModel model)
{
CheckModel(model);
if (_companyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(CompanyBindingModel model)
{
CheckModel(model);
if (_companyStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(CompanyBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_companyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(CompanyBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени",
nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина ",
nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля",
nameof(model.Name));
}
var element = _companyStorage.GetElement(new CompanySearchModel
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким названием уже есть");
}
}
}
}