PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/PositionLogic.cs

118 lines
4.1 KiB
C#
Raw Normal View History

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyBusinessLogic.BusinessLogics
{
public class PositionLogic
{
private readonly ILogger _logger;
private readonly IPositionStorage _positionStorage;
public PositionLogic(ILogger<PositionLogic> logger, IPositionStorage positionStorage)
{
_logger = logger;
_positionStorage = positionStorage;
}
public List<PositionViewModel>? ReadList(PositionSearchModel? model)
{
_logger.LogInformation("ReadList. PositionName:{PositionName}. Id:{Id}", model?.PositionName, model?.Id);
var list = model == null ? _positionStorage.GetFullList() : _positionStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public PositionViewModel? ReadElement(PositionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. PositionName:{PositionName}. Id:{Id}", model.PositionName, model.Id);
var element = _positionStorage.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(PositionBindingModel model)
{
CheckModel(model);
if (_positionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PositionBindingModel model)
{
CheckModel(model);
if (_positionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PositionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_positionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PositionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PositionName))
{
throw new ArgumentNullException("Нет названия материала", nameof(model.PositionName));
}
if (model.Salary < 0)
{
throw new ArgumentNullException("Зарплата должна быть не меньше 0", nameof(model.Salary));
}
_logger.LogInformation("Position. PositionName:{PositionName}. Salary:{Salary}. Id:{Id}", model.PositionName, model.Salary, model.Id);
var element = _positionStorage.GetElement(new PositionSearchModel
{
PositionName = model.PositionName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Должность с таким названием уже есть");
}
}
}
}