using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; using HospitalContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HospitalBusinessLogics.BusinessLogics { /// /// Бизнес-логика для сущности "Пациент" /// public class PatientLogic : IPatientLogic { /// /// Логгер /// private readonly ILogger _logger; /// /// Хранилище /// private readonly IPatientStorage _patientStorage; /// /// Конструктор /// /// /// public PatientLogic(ILogger logger, IPatientStorage patientStorage) { _logger = logger; _patientStorage = patientStorage; } /// /// Получить список /// /// /// public List? ReadList(PatientSearchModel? model) { _logger.LogInformation("ReadList. Patient.Id: {Id}. DoctorId: {DoctorId}. FullName: {FullName}", model?.Id, model?.DoctorId, model?.FullName); var list = model == null ? _patientStorage.GetFullList() : _patientStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList. Returned null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } /// /// Получить отдельную запись /// /// /// /// public PatientViewModel? ReadElement(PatientSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Patient.Id: {Id}. Phone: {Phone}", model?.Id, model?.Phone); var element = _patientStorage.GetElement(model!); if (element == null) { _logger.LogWarning("ReadElement. Element not found"); return null; } _logger.LogInformation("ReadElement. Find Patient.Id: {Id}", element?.Id); return element; } /// /// Создать запись /// /// /// public bool Create(PatientBindingModel model) { CheckModel(model); _logger.LogInformation("Create. Patient.Id: {Id}", model.Id); if (_patientStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } /// /// Изменить запись /// /// /// public bool Update(PatientBindingModel model) { CheckModel(model); _logger.LogInformation("Update. Patient.Id: {Id}", model.Id); if (_patientStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } /// /// Удалить запись /// /// /// public bool Delete(PatientBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Patient.Id: {Id}", model.Id); if (_patientStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } /// /// Проверка модели /// /// /// /// /// private void CheckModel(PatientBindingModel 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 (model.BirthDate == DateTime.MinValue) { throw new ArgumentNullException("Не указана дата рождения пациента", nameof(model.BirthDate)); } if (string.IsNullOrEmpty(model.Phone)) { throw new ArgumentNullException("Не указан номер телефона", nameof(model.Phone)); } if (model.DoctorId <= 0) { throw new ArgumentNullException("Не указан идентификатор лечащего врача", nameof(model.DoctorId)); } _logger.LogInformation("CheckModel. Patient.Id: {Id}", model.Id); var element = _patientStorage.GetElement(new PatientSearchModel { Phone = model.Phone }); if (element != null && !element.Id.Equals(model.Id)) { throw new InvalidOperationException("Пациент с таким номером телефона уже существует"); } } } }