121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
|
using Microsoft.Extensions.Logging;
|
|||
|
using TravelAgencyContracts.BindingModels;
|
|||
|
using TravelAgencyContracts.BusinessLogicsContracts;
|
|||
|
using TravelAgencyContracts.SearchModels;
|
|||
|
using TravelAgencyContracts.StoragesContracts;
|
|||
|
using TravelAgencyContracts.ViewModels;
|
|||
|
|
|||
|
namespace TravelAgencyBusinessLogic.BusinessLogics
|
|||
|
{
|
|||
|
public class GuideLogic : IGuideLogic
|
|||
|
{
|
|||
|
private readonly ILogger _logger;
|
|||
|
|
|||
|
private readonly IGuideStorage _guideStorage;
|
|||
|
|
|||
|
public GuideLogic(ILogger<GuideLogic> logger, IGuideStorage guideStorage)
|
|||
|
{
|
|||
|
_logger = logger;
|
|||
|
_guideStorage = guideStorage;
|
|||
|
}
|
|||
|
|
|||
|
public List<GuideViewModel>? ReadList(GuideSearchModel? model)
|
|||
|
{
|
|||
|
_logger.LogInformation("ReadList. GuideFIO: {GuideFIO}. Id: {Id} ", model?.GuideFIO, model?.Id);
|
|||
|
var list = (model == null) ? _guideStorage.GetFullList() : _guideStorage.GetFilteredList(model);
|
|||
|
if (list == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("ReadList return null list");
|
|||
|
return null;
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
public GuideViewModel? ReadElement(GuideSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
_logger.LogInformation("ReadElement. Guide id: {Id}, Guide email: {Email}, Guide phone number: {PhoneNumber}", model?.Id, model?.Email, model?.PhoneNumber);
|
|||
|
var element = _guideStorage.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 Create(GuideBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_guideStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Insert operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Update(GuideBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_guideStorage.Update(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Update operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public bool Delete(GuideBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
|||
|
if (_guideStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
_logger.LogWarning("Delete operation failed");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(GuideBindingModel model, bool withParams = true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.GuideFIO))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет ФИО гида", nameof(model.GuideFIO));
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.Email))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет почты гида", nameof(model.Email));
|
|||
|
}
|
|||
|
if (string.IsNullOrEmpty(model.PhoneNumber))
|
|||
|
{
|
|||
|
throw new ArgumentNullException("Нет номера телефона гида", nameof(model.Email));
|
|||
|
}
|
|||
|
_logger.LogInformation("Guide. Id: {id}, FIO: {fio}, email: {email}", model.Id, model.GuideFIO, model.Email);
|
|||
|
var element = _guideStorage.GetElement(new GuideSearchModel
|
|||
|
{
|
|||
|
Email = model.Email,
|
|||
|
PhoneNumber = model.PhoneNumber,
|
|||
|
});
|
|||
|
if (element != null && element.Id != model.Id)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("Гид с такой почтой или номером телефона уже есть");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|