antoc0der 2024-04-23 22:32:58 +04:00
commit 13086b7a8b
5 changed files with 391 additions and 1 deletions

View File

@ -0,0 +1,115 @@
using Microsoft.Extensions.Logging;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.ViewModels;
namespace VeterinaryBusinessLogic.BusinessLogic
{
public class OwnerLogic : IOwnerLogic
{
private readonly ILogger _logger;
private readonly IOwnerStorage _ownerStorage;
public OwnerLogic(ILogger<OwnerLogic> logger, IOwnerStorage ownerStorage)
{
_logger = logger;
_ownerStorage = ownerStorage;
}
public List<OwnerViewModel>? ReadList(OwnerSearchModel? model)
{
_logger.LogInformation("ReadList. OwnerFIO:{OwnerFIO}. Id:{ Id}", model?.OwnerFIO, model?.Id);
var list = model == null ? _ownerStorage.GetFullList() : _ownerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public OwnerViewModel? ReadElement(OwnerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. OwnerFIO:{OwnerFIO}.Id:{ Id}", model.OwnerFIO, model.Id);
var element = _ownerStorage.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(OwnerBindingModel model)
{
CheckModel(model);
if (_ownerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(OwnerBindingModel model)
{
CheckModel(model);
if (_ownerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(OwnerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_ownerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(OwnerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.OwnerFIO))
{
throw new ArgumentNullException("Нет ФИО клиента",
nameof(model.OwnerFIO));
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет Email клиента",
nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля клиента",
nameof(model.Password));
}
_logger.LogInformation("Owner. OwnerFIO:{OwnerFIO}." +
"Login:{ Login}. Password:{ Password}. Id: { Id} ", model.OwnerFIO, model.Login, model.Password, model.Id);
var element = _ownerStorage.GetElement(new OwnerSearchModel
{
Login = model.Login,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с таким логином уже есть");
}
}
}
}

View File

@ -0,0 +1,114 @@
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);
}
}
}

View File

@ -0,0 +1,82 @@
using Microsoft.Extensions.Logging;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.ViewModels;
namespace VeterinaryBusinessLogic.BusinessLogic
{
public class PurchaseLogic : IPurchaseLogic
{
private readonly ILogger _logger;
private readonly IPurchaseStorage _purchaseStorage;
public PurchaseLogic(ILogger<PurchaseLogic> logger, IPurchaseStorage purchaseStorage)
{
_logger = logger;
_purchaseStorage = purchaseStorage;
}
public PurchaseViewModel? ReadElement(PurchaseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _purchaseStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _purchaseStorage.GetFullList() : _purchaseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool CreatePurchase(PurchaseBindingModel model)
{
CheckModel(model);
if (_purchaseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
private void CheckModel(PurchaseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.DatePurchase <= DateTime.Now)
{
throw new ArgumentNullException("Дата покупки не должна быть в прошлом", nameof(model.DatePurchase));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count));
}
_logger.LogInformation("Purchase. DatePuchase: { DatePurchase}. Count:{ Count}. Id: { Id}", model.DatePurchase, model.Count, model.Id);
}
}
}

View File

@ -0,0 +1,79 @@
using Microsoft.Extensions.Logging;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.BusinessLogicContracts;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.ViewModels;
namespace VeterinaryBusinessLogic.BusinessLogic
{
public class VisitLogic : IVisitLogic
{
private readonly ILogger _logger;
private readonly IVisitStorage _visitStorage;
public VisitLogic(ILogger<VisitLogic> logger, IVisitStorage visitStorage)
{
_logger = logger;
_visitStorage = visitStorage;
}
public VisitViewModel? ReadElement(VisitSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _visitStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<VisitViewModel>? ReadList(VisitSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _visitStorage.GetFullList() :
_visitStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool CreateVisit(VisitBindingModel model)
{
CheckModel(model);
if (_visitStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
private void CheckModel(VisitBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.DateVisit >= DateTime.Now)
{
throw new ArgumentNullException("Дата посещения не должна быть в прошлом", nameof(model.DateVisit));
}
_logger.LogInformation("Visit. DateVisit:{ DateVisit}. Id: { Id}", model.DateVisit);
}
}
}

View File

@ -8,6 +8,6 @@ namespace VeterinaryContracts.BusinessLogicContracts
{
List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model);
PurchaseViewModel? ReadElement(PurchaseSearchModel model);
bool CreateOrder(PurchaseBindingModel model);
bool CreatePurchase(PurchaseBindingModel model);
}
}