PIbd23_Ivanova_Yakobchuk_Co.../LawCompany/LawCompanyBusinessLogic/BusinessLogics/HearingLogic.cs
2024-05-01 02:33:49 +04:00

139 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using LawCompanyContracts.BindingModels;
using LawCompanyContracts.BusinessLogicContracts;
using LawCompanyContracts.SearchModels;
using LawCompanyContracts.StoragesContracts;
using LawCompanyContracts.ViewModels;
using LawCompanyDataModels.Models;
using Microsoft.Extensions.Logging;
namespace LawCompanyBusinessLogic.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;
}
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;
}
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("На данное время уже назначено слушание");
}
}
}
}