using LawCompanyDataModels.Models; using LawCompanyContracts.BindingModels; using LawCompanyContracts.BusinessLogicContracts; using LawCompanyContracts.SearchModels; using LawCompanyContracts.StoragesContracts; using LawCompanyContracts.ViewModels; using Microsoft.Extensions.Logging; namespace LawCompanyBusinessLogic.BusinessLogics { public class VisitLogic : IVisitLogic { private readonly ILogger _logger; private readonly IVisitStorage _visitStorage; public VisitLogic(ILogger logger, IVisitStorage VisitStorage) { _logger = logger; _visitStorage = VisitStorage; } public bool Create(VisitBindingModel model) { CheckModel(model); 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. VisitDate:{VisitDate}. Id:{Id}", model.VisitDate, 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? ReadList(VisitSearchModel? model) { _logger.LogInformation("ReadList. VisitDate:{VisitDate}.Id:{Id}", model?.VisitDate, 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); if (_visitStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool AddClientToVisit(VisitSearchModel model, IClientModel client) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _visitStorage.GetElement(model); if (element == null) { return false; } element.VisitClients[client.Id] = client; _visitStorage.Update(new() { Id = element.Id, VisitDate = element.VisitDate, HearingId = element.HearingId, VisitClients = element.VisitClients, ExecutorId = element.ExecutorId, }); return true; } private void CheckModel(VisitBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty((model.VisitDate).ToString())) { throw new ArgumentNullException("Не поставлено время", nameof(model.VisitDate)); } _logger.LogInformation("Visit. VisitDate:{VisitDate}. Id: {Id} ", model.VisitDate, model.Id); var element = _visitStorage.GetElement(new VisitSearchModel { VisitDate = model.VisitDate }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("На данное время уже назначен визит"); } } } }