Доделала бизнес логику

This commit is contained in:
Alenka 2024-04-27 17:38:40 +04:00
parent 910e0d374e
commit 6654a1b627
5 changed files with 359 additions and 3 deletions

View File

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using HospitalDataModels.Models;
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.BusinessLogicContracts;
namespace HospitalBusinessLogic.BusinessLogics
{
public class DescriptionProcedureLogic : IDescriptionProcedureLogic
{
private readonly ILogger _logger;
private readonly IDescriptionProcedureStorage _guidanceStorage;
public DescriptionProcedureLogic(ILogger<DescriptionProcedureLogic> logger, IDescriptionProcedureStorage guidanceStorage)
{
_logger = logger;
_guidanceStorage = guidanceStorage;
}
public DescriptionProcedureViewModel? ReadElement(DescriptionProcedureSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _guidanceStorage.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<DescriptionProcedureViewModel>? ReadList(DescriptionProcedureSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _guidanceStorage.GetFullList() :
_guidanceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Create(DescriptionProcedureBindingModel model)
{
CheckModel(model);
if (_guidanceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DescriptionProcedureBindingModel model)
{
CheckModel(model);
if (_guidanceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DescriptionProcedureBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_guidanceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(DescriptionProcedureBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Description))
{
throw new ArgumentNullException("Нет текста рекомендации",
nameof(model.Description));
}
_logger.LogInformation("Guidance. Text:{Text}.", model.Description);
}
}
}

View File

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using HospitalDataModels.Models;
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.BusinessLogicContracts;
namespace HospitalBusinessLogic.BusinessLogics
{
public class MedicineLogic : IMedicineLogic
{
private readonly ILogger _logger;
private readonly IMedicineStorage _medicineStorage;
public MedicineLogic(ILogger<MedicineLogic> logger, IMedicineStorage iceCreamStorage)
{
_logger = logger;
_medicineStorage = iceCreamStorage;
}
public List<MedicineViewModel>? ReadList(MedicineSearchModel? model)
{
_logger.LogInformation("ReadList. MedicineName:{MedicineName}. Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _medicineStorage.GetFullList() :
_medicineStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public MedicineViewModel? ReadElement(MedicineSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. MedicineName:{MedicineName}.Id:{ Id}", model.Name, model.Id);
var element = _medicineStorage.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(MedicineBindingModel model)
{
CheckModel(model);
if (_medicineStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(MedicineBindingModel model)
{
CheckModel(model);
if (_medicineStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(MedicineBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_medicineStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(MedicineBindingModel 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.Price <= 0)
{
throw new ArgumentNullException("Цена медикамента должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Medicine. Medicine:{MedicineName}. Price:{ Price }. Id: { Id}", model.Name, model.Price, model.Id);
var element = _medicineStorage.GetElement(new MedicineSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Медикамент с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using HospitalDataModels.Models;
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.BusinessLogicContracts;
namespace HospitalBusinessLogic.BusinessLogics
{
public class ProcedureLogic : IProcedureLogic
{
private readonly ILogger _logger;
private readonly IProcedureStorage _serviceStorage;
public ProcedureLogic(ILogger<ProcedureLogic> logger, IProcedureStorage serviceStorage)
{
_logger = logger;
_serviceStorage = serviceStorage;
}
public List<ProcedureViewModel>? ReadList(ProcedureSearchModel? model)
{
_logger.LogInformation("ReadList. ServiceName:{ServiceName}. Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _serviceStorage.GetFullList() :
_serviceStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ProcedureViewModel? ReadElement(ProcedureSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ServiceName:{ServiceName}.Id:{ Id}", model.Name, model.Id);
var element = _serviceStorage.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(ProcedureBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ProcedureBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ProcedureBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_serviceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ProcedureBindingModel 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.Price <= 0)
{
// throw new ArgumentNullException("Цена мороженного услуги быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Service. ServiceName:{ServiceName}. Price:{ Price }. Id: { Id}", model.Name, model.Price, model.Id);
var element = _serviceStorage.GetElement(new ProcedureSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Услуга с таким названием уже есть");
}
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,7 +7,12 @@ using System.Threading.Tasks;
namespace HospitalContracts.BindingModels
{
public class DescriptionProcedureBindingModel
public class DescriptionProcedureBindingModel : IDescriptionProcedureModel
{
public string Description { get; set; } = string.Empty;
public int PharmacistId { get; set; }
public int Id { get; set; }
}
}

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace HospitalContracts.StoragesContracts
{
public interface IDescriptionProcedureStorege
public interface IDescriptionProcedureStorage
{
List<DescriptionProcedureViewModel> GetFullList();
List<DescriptionProcedureViewModel> GetFilteredList(DescriptionProcedureSearchModel model);