120 lines
3.3 KiB
C#
120 lines
3.3 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using VeterinaryContracts.BindingModels;
|
|
using VeterinaryContracts.BusinessLogicContracts;
|
|
using VeterinaryContracts.SearchModels;
|
|
using VeterinaryContracts.StorageContracts;
|
|
using VeterinaryContracts.ViewModels;
|
|
|
|
namespace VeterinaryBusinessLogic.BusinessLogic
|
|
{
|
|
public class PetLogic : IPetLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IPetStorage _petStorage;
|
|
public PetLogic(ILogger<PetLogic> logger, IPetStorage
|
|
petStorage)
|
|
{
|
|
_logger = logger;
|
|
_petStorage = petStorage;
|
|
}
|
|
public List<PetViewModel>? ReadList(PetSearchModel? model)
|
|
{
|
|
_logger.LogInformation("ReadList. PetName:{PetName}. Id:{ Id}", model?.PetName, model?.Id);
|
|
var list = model == null ? _petStorage.GetFullList() : _petStorage.GetFilteredList(model);
|
|
if (list == null)
|
|
{
|
|
_logger.LogWarning("ReadList return null list");
|
|
return null;
|
|
}
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
return list;
|
|
}
|
|
public PetViewModel? ReadElement(PetSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
_logger.LogInformation("ReadElement. PetName:{PetName}.Id:{ Id}", model.PetName, model.Id);
|
|
var element = _petStorage.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(PetBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_petStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public bool Update(PetBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_petStorage.Update(model) == null)
|
|
{
|
|
_logger.LogWarning("Update operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public bool Delete(PetBindingModel model)
|
|
{
|
|
CheckModel(model, false);
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
if (_petStorage.Delete(model) == null)
|
|
{
|
|
_logger.LogWarning("Delete operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
private void CheckModel(PetBindingModel model, bool withParams =
|
|
true)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
if (!withParams)
|
|
{
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(model.PetName))
|
|
{
|
|
throw new ArgumentNullException("Нет Клички животного",
|
|
nameof(model.PetName));
|
|
}
|
|
if (string.IsNullOrEmpty(model.PetType))
|
|
{
|
|
throw new ArgumentNullException("Нет Вида животного",
|
|
nameof(model.PetType));
|
|
}
|
|
if (string.IsNullOrEmpty(model.PetBreed))
|
|
{
|
|
throw new ArgumentNullException("Нет Породы животного",
|
|
nameof(model.PetBreed));
|
|
}
|
|
if (string.IsNullOrEmpty(model.PetGender))
|
|
{
|
|
throw new ArgumentNullException("Нет Пола животного",
|
|
nameof(model.PetGender));
|
|
}
|
|
_logger.LogInformation("Pet. PetName:{PetName}." +
|
|
"PetType:{ PetType}. PetBreed:{ PetBreed}. PetGender:{ PetGender}. Id: { Id} ", model.PetName, model.PetType, model.PetBreed, model.PetGender, model.Id);
|
|
var element = _petStorage.GetElement(new PetSearchModel
|
|
{
|
|
PetName = model.PetName
|
|
|
|
});
|
|
}
|
|
}
|
|
}
|