Бизнес-логика

This commit is contained in:
Владимир Данилов 2024-05-01 01:24:55 +04:00
parent be0a3cd20a
commit 848fbf8970
9 changed files with 818 additions and 1 deletions

View File

@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VeterinaryClinicContracts",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VeterinaryClinicDatabaseImplement", "VeterinaryClinicDatabaseImplement\VeterinaryClinicDatabaseImplement.csproj", "{BC172585-14F2-4484-8855-F00019F5EA5F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VeterinaryClinicBusinessLogics", "VeterinaryClinicBusinessLogics\VeterinaryClinicBusinessLogics.csproj", "{F307552A-1BB6-430A-92C1-12B26CEEE1AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -33,6 +35,10 @@ Global
{BC172585-14F2-4484-8855-F00019F5EA5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC172585-14F2-4484-8855-F00019F5EA5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC172585-14F2-4484-8855-F00019F5EA5F}.Release|Any CPU.Build.0 = Release|Any CPU
{F307552A-1BB6-430A-92C1-12B26CEEE1AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F307552A-1BB6-430A-92C1-12B26CEEE1AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F307552A-1BB6-430A-92C1-12B26CEEE1AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F307552A-1BB6-430A-92C1-12B26CEEE1AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StoragesContracts;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class AnimalLogic : IAnimalLogic
{
private readonly ILogger _logger;
private readonly IAnimalStorage _animalStorage;
public AnimalLogic(ILogger<AnimalLogic> logger, IAnimalStorage animalStorage)
{
_logger = logger;
_animalStorage = animalStorage;
}
public bool Create(AnimalBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Animal.Id: {Id}", model.Id);
if (_animalStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(AnimalBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Animal.Id: {Id}", model.Id);
if (_animalStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public AnimalViewModel? ReadElement(AnimalSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Animal.Id: {Id}", model?.Id);
var element = _animalStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Animal.Id: {Id}", element?.Id);
return element;
}
public List<AnimalViewModel>? ReadList(AnimalSearchModel? model)
{
_logger.LogInformation("ReadList. Animal.Id: {Id}. UserId: {DoctorId}", model?.Id, model?.UserId);
var list = model == null ? _animalStorage.GetFullList() : _animalStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(AnimalBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Animal.Id: {Id}", model.Id);
if (_animalStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(AnimalBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Type))
{
throw new ArgumentNullException("Не указан вид животного", nameof(model.Type));
}
if (string.IsNullOrEmpty(model.Breed))
{
throw new ArgumentNullException("Не указан порода животного", nameof(model.Breed));
}
if (model.Age < 0)
{
throw new ArgumentNullException("Не указан возраст животного", nameof(model.Age));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("CheckModel. Animal.Id: {Id}", model.Id);
}
}
}

View File

@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StoragesContracts;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class MedicationLogic : IMedicationLogic
{
private readonly ILogger _logger;
private readonly IMedicationStorage _medicationStorage;
public MedicationLogic(ILogger<MedicationLogic> logger, IMedicationStorage medicationStorage)
{
_logger = logger;
_medicationStorage = medicationStorage;
}
public bool Create(MedicationBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Medication.Id: {Id}", model.Id);
if (_medicationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(MedicationBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Medication.Id: {Id}", model.Id);
if (_medicationStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public MedicationViewModel? ReadElement(MedicationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Medication.Id: {Id}.", model?.Id);
var element = _medicationStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Medication.Id: {Id}", element?.Id);
return element;
}
public List<MedicationViewModel>? ReadList(MedicationSearchModel? model)
{
_logger.LogInformation("ReadList. Medication.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var list = model == null ? _medicationStorage.GetFullList() : _medicationStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(MedicationBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Medication.Id: {Id}", model.Id);
if (_medicationStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(MedicationBindingModel 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.Description))
{
throw new ArgumentNullException("Не указано описание медикамента", nameof(model.Description));
}
_logger.LogInformation("CheckModel. Medication.Id: {Id}", model.Id);
var element = _medicationStorage.GetElement(new MedicationSearchModel
{
Name = model.Name
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Медикамент с данным названием уже есть");
}
}
}
}

View File

@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StoragesContracts;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class ServiceLogic : IServiceLogic
{
private readonly ILogger _logger;
private readonly IServiceStorage _serviceStorage;
public ServiceLogic(ILogger<ServiceLogic> logger, IServiceStorage serviceStorage)
{
_logger = logger;
_serviceStorage = serviceStorage;
}
public bool Create(ServiceBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Service.Id: {Id}", model.Id);
if (_serviceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ServiceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Service.Id: {Id}", model.Id);
if (_serviceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ServiceViewModel? ReadElement(ServiceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Service.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var element = _serviceStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Service.Id: {Id}", element?.Id);
return element;
}
public List<ServiceViewModel>? ReadList(ServiceSearchModel? model)
{
_logger.LogInformation("ReadList. Service.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(ServiceBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Service.Id: {Id}", model.Id);
if (_serviceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ServiceBindingModel 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 (model.Cost <= 0)
{
throw new ArgumentNullException("Не указан цена", nameof(model.Cost));
}
if (model.MedicationId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор медикоментов", nameof(model.MedicationId));
}
_logger.LogInformation("CheckModel. Service.Id: {Id}", model.Id);
var element = _serviceStorage.GetElement(new ServiceSearchModel
{
Name = model.Name
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Услуга с данным названием уже есть");
}
}
}
}

View File

@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StoragesContracts;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class UserLogic : IUserLogic
{
private readonly ILogger _logger;
private readonly IUserStorage _userStorage;
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
{
_logger = logger;
_userStorage = userStorage;
}
public bool Create(UserBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. User.Id: {Id}", model.Id);
if (_userStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(UserBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. User.Id: {Id}", model.Id);
if (_userStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public UserViewModel? ReadElement(UserSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. User.Id: {Id}. Email: {Email}. Password: {Password}", model?.Id, model?.Email, model?.Password);
var element = _userStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find User.Id: {Id}", element?.Id);
return element;
}
public List<UserViewModel>? ReadList(UserSearchModel? model)
{
_logger.LogInformation("ReadList. User.Id: {Id}. Email: {Email}. FullName: {FullName}", model?.Id, model?.Email, model?.FullName);
var list = model == null ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(UserBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. User.Id: {Id}", model.Id);
if (_userStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(UserBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FullName))
{
throw new ArgumentNullException("Не указано ФИО", nameof(model.FullName));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Не указан номер телефона", nameof(model.Phone));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Не указан пароль", nameof(model.Password));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Не указана электронная почта", nameof(model.Email));
}
_logger.LogInformation("CheckModel. User.Id: {Id}", model.Id);
var element = _userStorage.GetElement(new UserSearchModel
{
Email = model.Email
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Пользователь с данной электронной почтой уже существует");
}
}
}
}

View File

@ -0,0 +1,143 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
using VeterinaryClinicContracts.StoragesContracts;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class VaccinationLogic : IVaccinationLogic
{
private readonly ILogger _logger;
private readonly IVaccinationStorage _vaccinationStorage;
public VaccinationLogic(ILogger<VaccinationLogic> logger, IVaccinationStorage vaccinationStorage)
{
_logger = logger;
_vaccinationStorage = vaccinationStorage;
}
public bool Create(VaccinationBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Vaccination.Id: {Id}", model.Id);
if (_vaccinationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(VaccinationBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Vaccination.Id: {Id}", model.Id);
if (_vaccinationStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public VaccinationViewModel? ReadElement(VaccinationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Vaccination.Id: {Id}.", model?.Id);
var element = _vaccinationStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Vaccination.Id: {Id}", element?.Id);
return element;
}
public List<VaccinationViewModel>? ReadList(VaccinationSearchModel? model)
{
_logger.LogInformation("ReadList. Vaccination.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var list = model == null ? _vaccinationStorage.GetFullList() : _vaccinationStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(VaccinationBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Vaccination.Id: {Id}", model.Id);
if (_vaccinationStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(VaccinationBindingModel 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 (model.DateInjection == DateTime.MinValue)
{
throw new ArgumentNullException("Не указана дата укола", nameof(model.DateInjection));
}
if (string.IsNullOrEmpty(model.ValidityPeriod))
{
throw new ArgumentNullException("Не указан срок действия", nameof(model.ValidityPeriod));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор пользователя", nameof(model.UserId));
}
if (model.AnimalId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор животного", nameof(model.AnimalId));
}
_logger.LogInformation("CheckModel. Vaccination.Id: {Id}", model.Id);
var element = _vaccinationStorage.GetElement(new VaccinationSearchModel
{
Name = model.Name
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Прививка с таким названием уже существует");
}
}
}
}

View File

@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryClinicContracts.BindingModels;
using VeterinaryClinicContracts.BusinessLogicsContracts;
using VeterinaryClinicContracts.SearchModels;
using VeterinaryClinicContracts.StoragesContracts;
using VeterinaryClinicContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace VeterinaryClinicBusinessLogics.BusinessLogics
{
public class VisitLogic : IVisitLogic
{
private readonly ILogger _logger;
private readonly IVisitStorage _visitStorage;
public VisitLogic(ILogger<VisitLogic> logger, IVisitStorage visitStorage)
{
_logger = logger;
_visitStorage = visitStorage;
}
public bool Create(VisitBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Visit.Id: {Id}", model.Id);
if (_visitStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(VisitBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_visitStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public VisitViewModel? ReadElement(VisitSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DateFrom:{DateFrom}. DateTo:{DateTo}. VisitId:{Id}", model.DateFrom, model.DateTo, model.Id);
var element = _visitStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. VisitId:{Id}", element.Id);
return element;
}
public List<VisitViewModel>? ReadList(VisitSearchModel? model)
{
_logger.LogInformation("ReadList. VisitId:{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 Update(VisitBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Visit.Id: {Id}", model.Id);
if (_visitStorage.Update(model) == null)
{
_logger.LogWarning("Update 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.Date == DateTime.MinValue)
{
throw new ArgumentNullException("Не указана дата визита", nameof(model.Date));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Не указан идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("CheckModel. Visit.Id: {Id}", model.Id);
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VeterinaryClinicContracts\VeterinaryClinicContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -19,6 +19,6 @@ namespace VeterinaryClinicContracts.BusinessLogicsContracts
bool Update(VisitBindingModel model);
bool Delete(VaccinationBindingModel model);
bool Delete(VisitBindingModel model);
}
}