aleksandr chegodaev 3055f22db6 lab1
2024-04-13 01:58:54 +04:00

119 lines
3.9 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 LawFirmContracts.BindingModels;
using LawFirmContracts.BusinessLogicsContracts;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmBusinessLogic.BusinessLogic
{
public class LawLogic : ILawLogic
{
private readonly ILogger _logger;
private readonly ILawStorage _LawStorage;
public LawLogic(ILogger<LawLogic> logger, ILawStorage
LawStorage)
{
_logger = logger;
_LawStorage = LawStorage;
}
public List<LawViewModel>? ReadList(LawSearchModel? model)
{
_logger.LogInformation("ReadList. LawName:{LawName}.Id:{Id}", model?.LawName, model?.Id);
var list = model == null ? _LawStorage.GetFullList() : _LawStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public LawViewModel? ReadElement(LawSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. LawName:{LawName}.Id:{Id}", model.LawName, model.Id);
var element = _LawStorage.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(LawBindingModel model)
{
CheckModel(model);
if (_LawStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(LawBindingModel model)
{
CheckModel(model);
if (_LawStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(LawBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_LawStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(LawBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.LawName))
{
throw new ArgumentNullException("Нет названия изделия", nameof(model.LawName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Law. LawName:{LawName}.Price:{Cost}. Id: {Id}", model.LawName, model.Price, model.Id);
var element = _LawStorage.GetElement(new LawSearchModel
{
LawName = model.LawName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}