BusinessLogics

This commit is contained in:
russell 2024-04-29 19:16:39 +04:00
parent 1c584ba71a
commit a864d1108d
16 changed files with 1021 additions and 51 deletions

View File

@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyDataModels", "T
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyContracts", "TravelAgencyContracts\TravelAgencyContracts.csproj", "{5A5C7696-4047-4BB8-BEC3-9DDB4000394E}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyContracts", "TravelAgencyContracts\TravelAgencyContracts.csproj", "{5A5C7696-4047-4BB8-BEC3-9DDB4000394E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelAgencyDatabaseImplement", "TravelAgencyDatabaseImplement\TravelAgencyDatabaseImplement.csproj", "{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyDatabaseImplement", "TravelAgencyDatabaseImplement\TravelAgencyDatabaseImplement.csproj", "{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelAgencyBusinessLogic", "TravelAgencyBusinessLogic\TravelAgencyBusinessLogic.csproj", "{AA81731F-6DB3-4BA0-98AF-E319788D7388}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -27,6 +29,10 @@ Global
{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.Build.0 = Release|Any CPU {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.Build.0 = Release|Any CPU
{AA81731F-6DB3-4BA0-98AF-E319788D7388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA81731F-6DB3-4BA0-98AF-E319788D7388}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA81731F-6DB3-4BA0-98AF-E319788D7388}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA81731F-6DB3-4BA0-98AF-E319788D7388}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -0,0 +1,119 @@
using Microsoft.Extensions.Logging;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class ExcursionGroupLogic : IExcursionGroupLogic
{
private readonly ILogger _logger;
private readonly IExcursionGroupStorage _excursionGroupStorage;
public ExcursionGroupLogic(ILogger<ExcursionGroupLogic> logger, IExcursionGroupStorage excursionGroupStorage)
{
_logger = logger;
_excursionGroupStorage = excursionGroupStorage;
}
public List<ExcursionGroupViewModel>? ReadList(ExcursionGroupSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, ExcursionGroupName: {ExcursionGroupName}, UserId: {UserId}, GuideId: {GuideId}.", model?.Id, model?.ExcursionGroupName, model?.UserId, model?.GuideId);
var list = (model == null) ? _excursionGroupStorage.GetFullList() : _excursionGroupStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public ExcursionGroupViewModel? ReadElement(ExcursionGroupSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ExcursionGroup id: {Id}, ExcursionGroup name: {ExcursionGroupName}", model?.Id, model?.ExcursionGroupName);
var element = _excursionGroupStorage.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(ExcursionGroupBindingModel model)
{
CheckModel(model);
if (_excursionGroupStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ExcursionGroupBindingModel model)
{
CheckModel(model);
if (_excursionGroupStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ExcursionGroupBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_excursionGroupStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ExcursionGroupBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ExcursionGroupName))
{
throw new ArgumentNullException("Нет названия группы", nameof(model.ExcursionGroupName));
}
if (model.ParticipantsAmount <= 0)
{
throw new ArgumentNullException("Количество участников быть больше 0", nameof(model.ParticipantsAmount));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("ExcursionGroup. Id: {id}, ExcursionGroupName: {ExcursionGroupName}, ParticipantsAmount: {ParticipantsAmount}, UserId: {UserId}", model.Id, model.ExcursionGroupName, model.ParticipantsAmount, model.UserId);
var element = _excursionGroupStorage.GetElement(new ExcursionGroupSearchModel
{
ExcursionGroupName = model.ExcursionGroupName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Группа с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,119 @@
using Microsoft.Extensions.Logging;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class ExcursionLogic : IExcursionLogic
{
private readonly ILogger _logger;
private readonly IExcursionStorage _excursionStorage;
public ExcursionLogic(ILogger<ExcursionLogic> logger, IExcursionStorage excursionStorage)
{
_logger = logger;
_excursionStorage = excursionStorage;
}
public List<ExcursionViewModel>? ReadList(ExcursionSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, ExcursionName: {ExcursionName}, UserId: {UserId}.", model?.Id, model?.ExcursionName, model?.UserId);
var list = (model == null) ? _excursionStorage.GetFullList() : _excursionStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public ExcursionViewModel? ReadElement(ExcursionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Excursion id: {Id}, Excursion name: {ExcursionName}", model?.Id, model?.ExcursionName);
var element = _excursionStorage.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(ExcursionBindingModel model)
{
CheckModel(model);
if (_excursionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ExcursionBindingModel model)
{
CheckModel(model);
if (_excursionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ExcursionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_excursionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ExcursionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ExcursionName))
{
throw new ArgumentNullException("Нет названия экскурсии", nameof(model.ExcursionName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена экскурсии должна быть больше 0", nameof(model.Price));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("Excursion. Id: {id}, ExcursionName: {ExcursionName}, Price: {Price}, UserId: {UserId}", model.Id, model.ExcursionName, model.Price, model.UserId);
var element = _excursionStorage.GetElement(new ExcursionSearchModel
{
ExcursionName = model.ExcursionName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Экскурсия с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,120 @@
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("Гид с такой почтой или номером телефона уже есть");
}
}
}
}

View File

@ -0,0 +1,119 @@
using Microsoft.Extensions.Logging;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class PlaceLogic : IPlaceLogic
{
private readonly ILogger _logger;
private readonly IPlaceStorage _placeStorage;
public PlaceLogic(ILogger<PlaceLogic> logger, IPlaceStorage placeStorage)
{
_logger = logger;
_placeStorage = placeStorage;
}
public List<PlaceViewModel>? ReadList(PlaceSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, PlaceName: {PlaceName}, ExcursionId: {ExcursionId}.", model?.Id, model?.PlaceName, model?.ExcursionId);
var list = (model == null) ? _placeStorage.GetFullList() : _placeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public PlaceViewModel? ReadElement(PlaceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Place id: {Id}, Place name: {PlaceName}, Place address: {PlaceAddress}", model?.Id, model?.PlaceName, model?.PlaceAddress);
var element = _placeStorage.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(PlaceBindingModel model)
{
CheckModel(model);
if (_placeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PlaceBindingModel model)
{
CheckModel(model);
if (_placeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PlaceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_placeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PlaceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PlaceName))
{
throw new ArgumentNullException("Нет названия места", nameof(model.PlaceName));
}
if (string.IsNullOrEmpty(model.PlaceAddress))
{
throw new ArgumentNullException("Нет адреса места", nameof(model.PlaceAddress));
}
if (model.ExcursionId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор экскурсии", nameof(model.ExcursionId));
}
_logger.LogInformation("Place. Id: {id}, PlaceName: {PlaceName}, PlaceAddress: {PlaceAddress}, ExcursionId: {ExcursionId}", model.Id, model.PlaceName, model.PlaceAddress, model.ExcursionId);
var element = _placeStorage.GetElement(new PlaceSearchModel
{
PlaceName = model.PlaceName,
PlaceAddress = model.PlaceAddress
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Место с таким названием или адресом уже есть");
}
}
}

View File

@ -0,0 +1,123 @@
using Microsoft.Extensions.Logging;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class TourLogic : ITourLogic
{
private readonly ILogger _logger;
private readonly ITourStorage _tourStorage;
public TourLogic(ILogger<TourLogic> logger, ITourStorage tourStorage)
{
_logger = logger;
_tourStorage = tourStorage;
}
public List<TourViewModel>? ReadList(TourSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, TourName: {TourName}, UserId: {UserId}.", model?.Id, model?.TourName, model?.UserId);
var list = (model == null) ? _tourStorage.GetFullList() : _tourStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public TourViewModel? ReadElement(TourSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Tour id: {Id}, Tour name: {TourName}", model?.Id, model?.TourName);
var element = _tourStorage.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(TourBindingModel model)
{
CheckModel(model);
if (_tourStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(TourBindingModel model)
{
CheckModel(model);
if (_tourStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(TourBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_tourStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(TourBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.TourName))
{
throw new ArgumentNullException("Нет названия тура", nameof(model.TourName));
}
if (model.TourDate == DateTime.MinValue)
{
throw new ArgumentNullException("Нет даты тура", nameof(model.TourDate));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена тура должна быть больше 0", nameof(model.Price));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("Tour. Id: {id}, TourName: {TourName}, Price: {Price}, UserId: {UserId}", model.Id, model.TourName, model.Price, model.UserId);
var element = _tourStorage.GetElement(new TourSearchModel
{
TourName = model.TourName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Тур с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,119 @@
using Microsoft.Extensions.Logging;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class TripLogic : ITripLogic
{
private readonly ILogger _logger;
private readonly ITripStorage _tripStorage;
public TripLogic(ILogger<TripLogic> logger, ITripStorage tripStorage)
{
_logger = logger;
_tripStorage = tripStorage;
}
public List<TripViewModel>? ReadList(TripSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, TripName: {TripName}, GuideId: {GuideId}.", model?.Id, model?.TripName, model?.GuideId);
var list = (model == null) ? _tripStorage.GetFullList() : _tripStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public TripViewModel? ReadElement(TripSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Trip id: {Id}, Trip name: {TripName}", model?.Id, model?.TripName);
var element = _tripStorage.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(TripBindingModel model)
{
CheckModel(model);
if (_tripStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(TripBindingModel model)
{
CheckModel(model);
if (_tripStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(TripBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_tripStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(TripBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.TripName))
{
throw new ArgumentNullException("Нет названия поездки", nameof(model.TripName));
}
if (model.TripDate == DateTime.MinValue)
{
throw new ArgumentNullException("Нет даты поездки", nameof(model.TripDate));
}
if (model.GuideId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор гида", nameof(model.GuideId));
}
_logger.LogInformation("Trip. Id: {id}, TripName: {TripName}, GuideId: {GuideId}", model.Id, model.TripName, model.GuideId);
var element = _tripStorage.GetElement(new TripSearchModel
{
TripName = model.TripName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Поездка с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,125 @@
using Microsoft.Extensions.Logging;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class UserLogic : IUserLogic
{
private readonly ILogger _logger;
private readonly IUserStorage _userStorage;
public UserLogic(ILogger<UserLogic> logger, IUserStorage userStorage)
{
_logger = logger;
_userStorage = userStorage;
}
public List<UserViewModel>? ReadList(UserSearchModel? model)
{
_logger.LogInformation("ReadList. UserFIO: {UserFIO}. Id: {Id} ", model?.UserFIO, model?.Id);
var list = (model == null) ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public UserViewModel? ReadElement(UserSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. User id: {Id}, User email: {Email}, User phone number: {PhoneNumber}, User password: {Password}", model?.Id, model?.Email, model?.PhoneNumber, model?.Password);
var element = _userStorage.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(UserBindingModel model)
{
CheckModel(model);
if (_userStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(UserBindingModel model)
{
CheckModel(model);
if (_userStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(UserBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_userStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(UserBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.UserFIO))
{
throw new ArgumentNullException("Нет ФИО пользователя", nameof(model.UserFIO));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет логина(почты) пользователя", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("Нет номера телефона пользователя", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
}
_logger.LogInformation("User. Id: {id}, FIO: {fio}, email: {email}, phone number: {PhoneNumber}, password: {password}", model.Id, model.UserFIO, model.Email, model.PhoneNumber,
model.Password);
var element = _userStorage.GetElement(new UserSearchModel
{
Email = model.Email,
PhoneNumber = model.PhoneNumber,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Пользователь с таким логином(почтой) или номером телефона уже есть");
}
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TravelAgencyContracts\TravelAgencyContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -24,12 +24,10 @@ namespace TravelAgencyDatabaseImplement.Implements
public List<ExcursionGroupViewModel> GetFilteredList(ExcursionGroupSearchModel model) public List<ExcursionGroupViewModel> GetFilteredList(ExcursionGroupSearchModel model)
{ {
if (string.IsNullOrEmpty(model.ExcursionGroupName))
{
return new();
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.ExcursionGroups if (!string.IsNullOrEmpty(model.ExcursionGroupName))
{
return context.ExcursionGroups
.Include(x => x.User) .Include(x => x.User)
.Include(x => x.Guide) .Include(x => x.Guide)
.Include(x => x.Tours) .Include(x => x.Tours)
@ -38,6 +36,32 @@ namespace TravelAgencyDatabaseImplement.Implements
.ToList() .ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
}
if (model.UserId.HasValue)
{
return context.ExcursionGroups
.Include(x => x.User)
.Include(x => x.Guide)
.Include(x => x.Tours)
.ThenInclude(x => x.Tour)
.Where(x => x.UserId.Equals(model.UserId))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
if (model.GuideId.HasValue)
{
return context.ExcursionGroups
.Include(x => x.User)
.Include(x => x.Guide)
.Include(x => x.Tours)
.ThenInclude(x => x.Tour)
.Where(x => x.GuideId.Equals(model.GuideId))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return new();
} }
public ExcursionGroupViewModel? GetElement(ExcursionGroupSearchModel model) public ExcursionGroupViewModel? GetElement(ExcursionGroupSearchModel model)

View File

@ -23,12 +23,10 @@ namespace TravelAgencyDatabaseImplement.Implements
public List<ExcursionViewModel> GetFilteredList(ExcursionSearchModel model) public List<ExcursionViewModel> GetFilteredList(ExcursionSearchModel model)
{ {
if (string.IsNullOrEmpty(model.ExcursionName))
{
return new();
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Excursions if (!string.IsNullOrEmpty(model.ExcursionName))
{
return context.Excursions
.Include(x => x.User) .Include(x => x.User)
.Include(x => x.Tours) .Include(x => x.Tours)
.ThenInclude(x => x.Tour) .ThenInclude(x => x.Tour)
@ -36,6 +34,19 @@ namespace TravelAgencyDatabaseImplement.Implements
.ToList() .ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
}
if (model.UserId.HasValue)
{
return context.Excursions
.Include(x => x.User)
.Include(x => x.Tours)
.ThenInclude(x => x.Tour)
.Where(x => x.UserId.Equals(model.UserId))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return new();
} }
public ExcursionViewModel? GetElement(ExcursionSearchModel model) public ExcursionViewModel? GetElement(ExcursionSearchModel model)

View File

@ -18,27 +18,39 @@ namespace TravelAgencyDatabaseImplement.Implements
public List<GuideViewModel> GetFilteredList(GuideSearchModel model) public List<GuideViewModel> GetFilteredList(GuideSearchModel model)
{ {
if (string.IsNullOrEmpty(model.Email)) if (string.IsNullOrEmpty(model.GuideFIO))
{ {
return new(); return new();
} }
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Guides return context.Guides
.Where(x => x.Email.Contains(model.Email)) .Where(x => x.GuideFIO.Contains(model.GuideFIO))
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
public GuideViewModel? GetElement(GuideSearchModel model) public GuideViewModel? GetElement(GuideSearchModel model)
{ {
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Guides.FirstOrDefault(x => if (model.Id.HasValue)
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) {
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; return context.Guides
.FirstOrDefault(x => x.Id.Equals(model.Id))
?.GetViewModel;
}
if (!string.IsNullOrEmpty(model.Email))
{
return context.Guides
.FirstOrDefault(x => x.Email.Equals(model.Email))
?.GetViewModel;
}
if (!string.IsNullOrEmpty(model.PhoneNumber))
{
return context.Guides
.FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber))
?.GetViewModel;
}
return null;
} }
public GuideViewModel? Insert(GuideBindingModel model) public GuideViewModel? Insert(GuideBindingModel model)

View File

@ -20,29 +20,48 @@ namespace TravelAgencyDatabaseImplement.Implements
public List<PlaceViewModel> GetFilteredList(PlaceSearchModel model) public List<PlaceViewModel> GetFilteredList(PlaceSearchModel model)
{ {
if (string.IsNullOrEmpty(model.PlaceName))
{
return new();
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Places if (!string.IsNullOrEmpty(model.PlaceName))
{
return context.Places
.Include(x => x.Excursion) .Include(x => x.Excursion)
.Where(x => x.PlaceName.Contains(model.PlaceName)) .Where(x => x.PlaceName.Contains(model.PlaceName))
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
}
if (model.ExcursionId.HasValue)
{
return context.Places
.Include(x => x.Excursion)
.Where(x => x.ExcursionId.Equals(model.ExcursionId))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
} }
public PlaceViewModel? GetElement(PlaceSearchModel model) public PlaceViewModel? GetElement(PlaceSearchModel model)
{ {
if (string.IsNullOrEmpty(model.PlaceName) && !model.Id.HasValue)
{
return null;
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Places.Include(x => x.Excursion) if (model.Id.HasValue)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaceName) && x.PlaceName == model.PlaceName) || {
(model.Id.HasValue && x.Id == model.Id)) return context.Places
.FirstOrDefault(x => x.Id.Equals(model.Id))
?.GetViewModel; ?.GetViewModel;
}
if (!string.IsNullOrEmpty(model.PlaceName))
{
return context.Places
.FirstOrDefault(x => x.PlaceName.Equals(model.PlaceName))
?.GetViewModel;
}
if (!string.IsNullOrEmpty(model.PlaceAddress))
{
return context.Places
.FirstOrDefault(x => x.PlaceAddress.Equals(model.PlaceAddress))
?.GetViewModel;
}
return null;
} }
public PlaceViewModel? Insert(PlaceBindingModel model) public PlaceViewModel? Insert(PlaceBindingModel model)

View File

@ -20,16 +20,24 @@ namespace TravelAgencyDatabaseImplement.Implements
public List<TourViewModel> GetFilteredList(TourSearchModel model) public List<TourViewModel> GetFilteredList(TourSearchModel model)
{ {
if (string.IsNullOrEmpty(model.TourName))
{
return new();
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Tours if (!string.IsNullOrEmpty(model.TourName))
{
return context.Tours
.Include(x => x.User) .Include(x => x.User)
.Where(x => x.TourName.Contains(model.TourName)) .Where(x => x.TourName.Contains(model.TourName))
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
}
if (model.UserId.HasValue)
{
return context.Tours
.Include(x => x.User)
.Where(x => x.UserId.Equals(model.UserId))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
} }
public TourViewModel? GetElement(TourSearchModel model) public TourViewModel? GetElement(TourSearchModel model)

View File

@ -23,12 +23,10 @@ namespace TravelAgencyDatabaseImplement.Implements
public List<TripViewModel> GetFilteredList(TripSearchModel model) public List<TripViewModel> GetFilteredList(TripSearchModel model)
{ {
if (string.IsNullOrEmpty(model.TripName))
{
return new();
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Trips if (!string.IsNullOrEmpty(model.TripName))
{
return context.Trips
.Include(x => x.Guide) .Include(x => x.Guide)
.Include(x => x.Places) .Include(x => x.Places)
.ThenInclude(x => x.Place) .ThenInclude(x => x.Place)
@ -36,6 +34,19 @@ namespace TravelAgencyDatabaseImplement.Implements
.ToList() .ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
}
if (model.GuideId.HasValue)
{
return context.Trips
.Include(x => x.Guide)
.Include(x => x.Places)
.ThenInclude(x => x.Place)
.Where(x => x.GuideId.Equals(model.GuideId))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return new();
} }
public TripViewModel? GetElement(TripSearchModel model) public TripViewModel? GetElement(TripSearchModel model)

View File

@ -18,27 +18,45 @@ namespace TravelAgencyDatabaseImplement.Implements
public List<UserViewModel> GetFilteredList(UserSearchModel model) public List<UserViewModel> GetFilteredList(UserSearchModel model)
{ {
if (string.IsNullOrEmpty(model.Email)) if (string.IsNullOrEmpty(model.UserFIO))
{ {
return new(); return new();
} }
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Users return context.Users
.Where(x => x.Email.Contains(model.Email)) .Where(x => x.UserFIO.Contains(model.UserFIO))
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
public UserViewModel? GetElement(UserSearchModel model) public UserViewModel? GetElement(UserSearchModel model)
{ {
if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
{
return null;
}
using var context = new TravelAgencyDatabase(); using var context = new TravelAgencyDatabase();
return context.Users.FirstOrDefault(x => if (model.Id.HasValue)
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) {
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; return context.Users
.FirstOrDefault(x => x.Id.Equals(model.Id))
?.GetViewModel;
}
if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password))
{
return context.Users
.FirstOrDefault(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password))
?.GetViewModel;
}
if (!string.IsNullOrEmpty(model.Email))
{
return context.Users
.FirstOrDefault(x => x.Email.Equals(model.Email))
?.GetViewModel;
}
if (!string.IsNullOrEmpty(model.PhoneNumber))
{
return context.Users
.FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber))
?.GetViewModel;
}
return null;
} }
public UserViewModel? Insert(UserBindingModel model) public UserViewModel? Insert(UserBindingModel model)