using LawCompanyDataModels.Enums; 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 CaseLogic : ICaseLogic { private readonly ILogger _logger; private readonly ICaseStorage _caseStorage; public CaseLogic(ILogger logger, ICaseStorage CaseStorage) { _logger = logger; _caseStorage = CaseStorage; } public bool CaseAnalysis(CaseBindingModel model) { return UpdateStatus(model, CaseStatus.АнализДелаИПодготовкаДокументов); } public bool CaseHearing(CaseBindingModel model) { return UpdateStatus(model, CaseStatus.СлушанияДела); } public bool CloseCase(CaseBindingModel model) { return UpdateStatus(model, CaseStatus.ЗакрытиеДела); } public bool CreateCase(CaseBindingModel model) { CheckModel(model); if (model.Status != CaseStatus.Неизвестен) { _logger.LogWarning("Case status is incorrect"); return false; } model.Status = CaseStatus.Принято; model.DateCreate = DateTime.Now; if (_caseStorage.Insert(model) == null) { model.Status = CaseStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool DeleteCase(CaseBindingModel model) { if (_caseStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public CaseViewModel? ReadElement(CaseSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. Name:{Name}. Id:{Id}", model.Name, model.Id); var element = _caseStorage.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 Update(CaseBindingModel model) { CheckModel(model); if (_caseStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public List? ReadList(CaseSearchModel? model) { _logger.LogInformation("ReadList. CaseId:{Id}", model?.Id); var list = model == null ? _caseStorage.GetFullList() : _caseStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public bool AddClientToCase(CaseSearchModel model, IClientModel client) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation(" CaseName:{CaseName}.Id:{Id}", model.Name, model.Id); var element = _caseStorage.GetElement(model); if (element == null) { return false; } if (element.CaseClients.ContainsKey(client.Id)) { return false; } element.CaseClients[client.Id] = client; _caseStorage.Update(new() { Id = element.Id, Name = element.Name, DateCreate = element.DateCreate, Status = element.Status, CaseClients = element.CaseClients }); return true; } private void CheckModel(CaseBindingModel 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)); } _logger.LogInformation("Case. CaseID:{Id}, Name:{Name}.", model.Id, model.Name); } private bool UpdateStatus(CaseBindingModel model, CaseStatus newStatus) { CheckModel(model); if (model.Status + 1 != newStatus) { _logger.LogWarning("Status incorrected"); return false; } model.Status = newStatus; if (model.Status == CaseStatus.ЗакрытиеДела) { model.DateImplement = DateTime.Now; } if (_caseStorage.Update(model) == null) { model.Status--; _logger.LogWarning("Update operation failed"); return false; } return true; } } }