diff --git a/Vetclinic/VetclinicBusinessLogic/BusinessLogics/DoctorVisitLogic.cs b/Vetclinic/VetclinicBusinessLogic/BusinessLogics/DoctorVisitLogic.cs new file mode 100644 index 0000000..6a70dcb --- /dev/null +++ b/Vetclinic/VetclinicBusinessLogic/BusinessLogics/DoctorVisitLogic.cs @@ -0,0 +1,130 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetclinicContracts.BindingModels; +using VetclinicContracts.BusinessLogicsContracts; +using VetclinicContracts.SearchModels; +using VetclinicContracts.StoragesContracts; +using VetclinicContracts.ViewModels; +using VetclinicDataModels.Enums; + +namespace VetclinicBusinessLogic.BusinessLogics +{ + public class DoctorVisitLogic: IDoctorVisitLogic + { + private readonly ILogger _logger; + private readonly IDoctorVisitStorage _doctorVisitStorage; + public DoctorVisitLogic(ILogger logger, IDoctorVisitStorage doctorVisitStorage) + { + _logger = logger; + _doctorVisitStorage = doctorVisitStorage; + } + public List? ReadList(DoctorVisitSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{Id}", model?.Id); + var list = model == null ? _doctorVisitStorage.GetFullList() : _doctorVisitStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public DoctorVisitViewModel? ReadElement(DoctorVisitSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); + + var element = _doctorVisitStorage.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 CreateDoctorVisit(DoctorVisitBindingModel model) + { + CheckModel(model); + if (model.Status != DoctorVisitStatus.Неизвестен) + { + _logger.LogWarning("Wrong DoctorVisit status. Insert operation failed"); + return false; + } + model.Status = DoctorVisitStatus.Забронировано; + if (_doctorVisitStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool ChangeStatus(DoctorVisitBindingModel model, DoctorVisitStatus status) + { + CheckModel(model, false); + var element = _doctorVisitStorage.GetElement(new DoctorVisitSearchModel { Id = model.Id }); + if (element == null) + { + _logger.LogWarning("Read operation failed"); + return false; + } + if (element.Status != status - 1) + { + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); + } + DoctorVisitStatus oldStatus = model.Status; + model.Status = status; + /* РАЗ УЖ НЕТ ДАТЫ ОКОНЧАНИЯ ТО ЭТО НЕ НАДО? + if (model.Status == DoctorVisitStatus.Закончен) + { + model.DateImplement = DateTime.Now; + } + */ + if (_doctorVisitStorage.Update(model) == null) + { + model.Status = oldStatus; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool BeginDoctorVisit(DoctorVisitBindingModel model) + { + return ChangeStatus(model, DoctorVisitStatus.Выполняется); + } + public bool FinishDoctorVisit(DoctorVisitBindingModel model) + { + return ChangeStatus(model, DoctorVisitStatus.Закончен); + } + private void CheckModel(DoctorVisitBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.DateVisit != null) + { + throw new ArithmeticException("Дата завершения не может быть раньше даты начала"); + } + _logger.LogInformation("DoctorVisit. Id:{Id}. DateVisit:{DateVisit}. ServiceId:{ServiceId}. AnimalId:{AnimalId}", + model.Id, model.DateVisit, model.ServiceId, model.AnimalId); + } + } +} diff --git a/Vetclinic/VetclinicBusinessLogic/BusinessLogics/DrugPurchaseLogic.cs b/Vetclinic/VetclinicBusinessLogic/BusinessLogics/DrugPurchaseLogic.cs index 8a9a7aa..f764264 100644 --- a/Vetclinic/VetclinicBusinessLogic/BusinessLogics/DrugPurchaseLogic.cs +++ b/Vetclinic/VetclinicBusinessLogic/BusinessLogics/DrugPurchaseLogic.cs @@ -133,7 +133,7 @@ namespace VetclinicBusinessLogic.BusinessLogics { throw new ArithmeticException("Дата завершения не может быть раньше даты начала"); } - _logger.LogInformation("DrugPurchase. Id:{Id}. Sum:{Sum}. AnimalId:{AnimalId}", model.Id, model.Sum, model.AnimalId); + _logger.LogInformation("DrugPurchase. Id:{Id}. Cost:{Cost}. DrugId:{DrugId}. AnimalId:{DrugId}", model.Id, model.Cost, model.DrugId, model.AnimalId); } } }