Добавила бизнес-логику

This commit is contained in:
Allllen4a 2024-04-24 14:02:00 +04:00
parent 1d32872493
commit 3a086dc54b
4 changed files with 492 additions and 0 deletions

View File

@ -0,0 +1,156 @@
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.BusinessLogicContracts;
using BeautySalonContracts.SearchModels;
using BeautySalonContracts.StoragesContracts;
using BeautySalonContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace BeautySalonBusinessLogic.BusinessLogics
{
public class ClientLogic : IClientLogic
{
private readonly int _passwordMaxLength = 50;
private readonly int _passwordMinLength = 10;
private readonly ILogger _logger;
private readonly IClientStorage _clientStorage;
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
{
_logger = logger;
_clientStorage = clientStorage;
}
public bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ClientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_clientStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ClientViewModel? ReadElement(ClientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ClientLogin: {ClientLogin}. ClientFIO: {ClientFIO} ClientEmail: {ClientEmail} Id: {Id}",
model.ClientLogin, model.ClientFIO, model.ClientEmail, model.Id);
var element = _clientStorage.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<ClientViewModel>? ReadList(ClientSearchModel? model)
{
_logger.LogInformation("ReadList. ClientLogin: {ClientLogin}. ClientFIO: {ClientFIO} ClientEmail: {ClientEmail} Id: {Id}",
model?.ClientLogin, model?.ClientFIO, model?.ClientEmail, model?.Id);
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.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(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ClientLogin))
{
throw new ArgumentNullException("Нет логина клиента", nameof(model.ClientLogin));
}
if (string.IsNullOrEmpty(model.ClientFIO))
{
throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO));
}
if (string.IsNullOrEmpty(model.ClientEmail))
{
throw new ArgumentNullException("Нет почты клиента", nameof(model.ClientEmail));
}
if (string.IsNullOrEmpty(model.ClientPassword))
{
throw new ArgumentNullException("Нет пароля", nameof(model.ClientPassword));
}
if (model.ClientPassword.Length < _passwordMinLength)
{
throw new ArgumentNullException("Пароль слишком короткий", nameof(model.ClientPassword));
}
if (model.ClientPassword.Length > _passwordMaxLength)
{
throw new ArgumentNullException("Пароль слишком длинный", nameof(model.ClientPassword));
}
_logger.LogInformation("Client. ClientLogin: {ClientLogin}. ClientFIO: {ClientFIO} ClientEmail: {ClientEmail} Id: {Id}",
model.ClientLogin, model.ClientFIO, model.ClientEmail, model.Id);
var element = _clientStorage.GetElement(new ClientSearchModel
{
ClientEmail = model.ClientEmail
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с такой почтой уже есть");
}
}
}
}

View File

@ -0,0 +1,107 @@
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.BusinessLogicContracts;
using BeautySalonContracts.SearchModels;
using BeautySalonContracts.StoragesContracts;
using BeautySalonContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeautySalonBusinessLogic.BusinessLogics
{
public class EvaluationLogic : IEvaluationLogic
{
private readonly ILogger _logger;
private readonly IEvaluationStorage _evaluationStorage;
public EvaluationLogic(ILogger<EvaluationLogic> logger, IEvaluationStorage evaluationStorage)
{
_logger = logger;
_evaluationStorage = evaluationStorage;
}
public int GetNumberOfPages(int userId, int pageSize = 10)
{
return _evaluationStorage.GetNumberOfPages(userId, pageSize);
}
public List<EvaluationViewModel>? ReadList(EvaluationSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}",
model?.Id);
var list = model == null ? _evaluationStorage.GetFullList() :
_evaluationStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public EvaluationViewModel? ReadElement(EvaluationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _evaluationStorage.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(EvaluationBindingModel model)
{
CheckModel(model);
if (_evaluationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(EvaluationBindingModel model)
{
CheckModel(model);
if (_evaluationStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(EvaluationBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_evaluationStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(EvaluationBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.PointsProcedure <= 0)
{
throw new ArgumentNullException("Оценки за процедуру должны быть больше 0", nameof(model.PointsProcedure));
}
_logger.LogInformation("Rating. PointsProcedure: {PointsProcedure}}. Id: {Id}",
model.PointsProcedure, model.PointsCosmetics, model.Id);
}
}
}

View File

@ -0,0 +1,106 @@
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.BusinessLogicContracts;
using BeautySalonContracts.SearchModels;
using BeautySalonContracts.StoragesContracts;
using BeautySalonContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeautySalonBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<StaffMemberLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public int GetNumberOfPages(int userId, int pageSize = 10)
{
return _orderStorage.GetNumberOfPages(userId, pageSize);
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("Order. OrderID:{Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. OrderDate: {OrderDate}. Id: {Id}",
model.OrderDate, model.Id);
var element = _orderStorage.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(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(OrderBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_orderStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.OrderAmount <= 0)
{
throw new ArgumentNullException("Cумма заказа должна быть больше 0",
nameof(model.OrderAmount));
}
}
}
}

View File

@ -0,0 +1,123 @@
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.BusinessLogicContracts;
using BeautySalonContracts.SearchModels;
using BeautySalonContracts.StoragesContracts;
using BeautySalonContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeautySalonBusinessLogic.BusinessLogics
{
public class ProcedureLogic : IProcedureLogic
{
private readonly ILogger _logger;
private readonly IProcedureStorage _procedureStorage;
public ProcedureLogic(ILogger<ProcedureLogic> logger, IProcedureStorage procedureStorage)
{
_logger = logger;
_procedureStorage = procedureStorage;
}
public int GetNumberOfPages(int userId, int pageSize = 10)
{
return _procedureStorage.GetNumberOfPages(userId, pageSize);
}
public List<ProcedureViewModel>? ReadList(ProcedureSearchModel? model)
{
_logger.LogInformation("ReadList. ProcedureName: {ProcedureName}. Id: {Id}",
model?.ProcedureName, model?.Id);
var list = model == null ? _procedureStorage.GetFullList() :
_procedureStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public ProcedureViewModel? ReadElement(ProcedureSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ProcedureName: {ProcedureName}. Id: {Id}",
model.ProcedureName, model.Id);
var element = _procedureStorage.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(ProcedureBindingModel model)
{
CheckModel(model);
if (_procedureStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ProcedureBindingModel model)
{
CheckModel(model);
if (_procedureStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ProcedureBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_procedureStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ProcedureBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ProcedureName))
{
throw new ArgumentNullException("Нет названия процедуры", nameof(model.ProcedureName));
}
if (model.ProcedurePrice <= 0)
{
throw new ArgumentNullException("Цена процедуры должна быть больше 0",
nameof(model.ProcedurePrice));
}
_logger.LogInformation("Procedure. ProcedureName: {ProcedureName}. Cost: {Cost}. Id: {Id}",
model.ProcedureName, model.ProcedurePrice, model.Id);
var element = _procedureStorage.GetElement(new ProcedureSearchModel
{
ProcedureName = model.ProcedureName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Процедура с таким названием уже есть");
}
}
}
}