Создание логики администратора

This commit is contained in:
Extrimal 2024-04-22 19:44:52 +04:00
parent 0398a5c289
commit 875f1c4911
5 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,164 @@
using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogic
{
public class AdministratorLogic : IAdministratorLogic
{
private readonly int _loginMaxLength = 25;
private readonly int _passwordMaxLength = 50;
private readonly ILogger _logger;
private readonly IAdministratorStorage _administratorStorage;
public AdministratorLogic(ILogger<AdministratorLogic> logger, IAdministratorStorage administratorStorage)
{
_logger = logger;
_administratorStorage = administratorStorage;
}
public List<AdministratorViewModel>? ReadList(AdministratorSearchModel? model)
{
_logger.LogInformation("ReadList. AdministratorFIO: {AdministratorFIO}. AdministratorLogin: {AdministratorLogin}. Id: {Id}", model?.AdministratorFIO, model?.AdministratorLogin, model?.Id);
var list = model == null ? _administratorStorage.GetFullList() : _administratorStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public AdministratorViewModel? ReadElement(AdministratorSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. AdministratorFIO: {AdministratorFIO}. AdministratorLogin: {AdministratorLogin}. Id: {Id}.", model.AdministratorFIO, model.AdministratorLogin, model.Id);
var element = _administratorStorage.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(AdministratorBindingModel model)
{
CheckModel(model);
if (_administratorStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(AdministratorBindingModel model)
{
CheckModel(model);
if (_administratorStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(AdministratorBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_administratorStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(AdministratorBindingModel model, bool Parametrs = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!Parametrs)
{
return;
}
if (string.IsNullOrEmpty(model.AdministratorFIO))
{
throw new ArgumentNullException("Нет ФИО администратора", nameof(model.AdministratorFIO));
}
if (string.IsNullOrEmpty(model.AdministratorLogin))
{
throw new ArgumentNullException("Нет логина администратора", nameof(model.AdministratorLogin));
}
if (model.AdministratorLogin.Length > _loginMaxLength)
{
throw new ArgumentNullException("Логин превышает допустимое количество символов", nameof(model.AdministratorLogin));
}
if (string.IsNullOrEmpty(model.AdministratorPhone))
{
throw new ArgumentNullException("Нет номера телефона администратора", nameof(model.AdministratorPhone));
}
if (string.IsNullOrEmpty(model.AdministratorEmail))
{
throw new ArgumentNullException("Нет эл. почты администратора", nameof(model.AdministratorEmail));
}
if (string.IsNullOrEmpty(model.AdministratorPassword))
{
throw new ArgumentNullException("Нет пароля администратора", nameof(model.AdministratorPassword));
}
if (model.AdministratorPassword.Length > _passwordMaxLength)
{
throw new ArgumentNullException("Пароль превышает допустимое количество символов", nameof(model.AdministratorPassword));
}
_logger.LogInformation("Administrator. AdministratorFIO: {AdministratorFIO}. AdministratorLogin: {AdministratorLogin}. Id: {Id}", model.AdministratorFIO, model.AdministratorLogin, model.Id);
var elementEmail = _administratorStorage.GetElement(new AdministratorSearchModel
{
AdministratorEmail = model.AdministratorEmail
});
if (elementEmail != null && elementEmail.Id != model.Id)
{
throw new InvalidOperationException("администратор с такой почтой уже есть");
}
var elementPhone = _administratorStorage.GetElement(new AdministratorSearchModel
{
AdministratorPhone = model.AdministratorPhone
});
if (elementPhone != null && elementPhone.Id != model.Id)
{
throw new InvalidOperationException("администратор с таким номером уже есть");
}
var elementLogin = _administratorStorage.GetElement(new AdministratorSearchModel
{
AdministratorLogin = model.AdministratorLogin
});
if (elementLogin != null && elementLogin.Id != model.Id)
{
throw new InvalidOperationException("администратор с таким логином уже есть");
}
}
}
}

View File

@ -0,0 +1,14 @@
using HotelContracts.BusinessLogicsContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogic
{
public class ConferenceBookingLogic : IConferenceBookingLogic
{
}
}

View File

@ -0,0 +1,13 @@
using HotelContracts.BusinessLogicsContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogic
{
public class DinnerLogic : IDinnerLogic
{
}
}

View File

@ -0,0 +1,14 @@
using HotelContracts.BusinessLogicsContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogic
{
public class RoomLogic : IRoomLogic
{
}
}

View File

@ -14,5 +14,6 @@ namespace HotelContracts.SearchModels
public string? AdministratorPassword { get; set; }
public string? AdministratorEmail { get; set; }
public string? AdministratorPhone { get; set; }
}
}