PIbd-22_Fedorenko_Puchkina_.../LawFim/LawFirmBusinessLogic/BusinessLogics/HearingLogic.cs

147 lines
3.8 KiB
C#
Raw Normal View History

2024-04-27 19:02:25 +04:00
using LawFimDataModels.Models;
using LawFirmContracts.BindingModels;
using LawFirmContracts.BusinessLogicContracts;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StoragesContracts;
using LawFirmContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace LawFirmBusinessLogic.BusinessLogics
{
public class HearingLogic : IHearingLogic
{
private readonly ILogger _logger;
private readonly IHearingStorage _hearingStorage;
public HearingLogic(ILogger<HearingLogic> logger, IHearingStorage hearingStorage)
{
_logger = logger;
_hearingStorage = hearingStorage;
}
public bool Create(HearingBindingModel model)
{
CheckModel(model);
if (_hearingStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(HearingBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_hearingStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public HearingViewModel? ReadElement(HearingSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. HearingDate:{HearingDate}. Id:{Id}", model.HearingDate, model.Id);
var element = _hearingStorage.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<HearingViewModel>? ReadList(HearingSearchModel? model)
{
_logger.LogInformation("ReadList. HearingDate:{HearingDate}.Id:{Id}", model?.HearingDate, model?.Id);
var list = model == null ? _hearingStorage.GetFullList() : _hearingStorage.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(HearingBindingModel model)
{
CheckModel(model);
if (_hearingStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool AddLawyerToHearing(HearingSearchModel model, ILawyerModel lawyer)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _hearingStorage.GetElement(model);
if (element == null)
{
return false;
}
2024-05-01 17:50:10 +04:00
if (element.HearingLawyers.ContainsKey(lawyer.Id))
{
return false;
}
2024-04-27 19:02:25 +04:00
element.HearingLawyers[lawyer.Id] = lawyer;
_hearingStorage.Update(new()
{
Id = element.Id,
HearingDate = element.HearingDate,
Judge = element.Judge,
HearingLawyers = element.HearingLawyers,
GuarantorId = element.GuarantorId,
});
return true;
}
private void CheckModel(HearingBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
2024-05-01 17:50:10 +04:00
if (string.IsNullOrEmpty(model.Judge))
{
throw new ArgumentNullException("Не указан суд",
nameof(model.Judge));
}
2024-04-27 19:02:25 +04:00
if (string.IsNullOrEmpty((model.HearingDate).ToString()))
{
throw new ArgumentNullException("Не поставлено время", nameof(model.HearingDate));
}
_logger.LogInformation("Hearing. HearingDate:{HearingDate}. Id: {Id} ", model.HearingDate, model.Id);
var element = _hearingStorage.GetElement(new HearingSearchModel
{
HearingDate = model.HearingDate
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("На данное время уже назначено слушание");
}
}
}
}