Частичная реализация бизнес логики клиента

This commit is contained in:
Leozillo73 2024-04-27 21:51:54 +04:00
parent 845f442d95
commit a4474ce0aa
3 changed files with 224 additions and 3 deletions

View File

@ -0,0 +1,124 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.BusinessLogicsContracts;
using ZooContracts.SearchModels;
using ZooContracts.StorageContracts;
using ZooContracts.ViewModels;
namespace ZooBusinessLogic.BusinessLogic
{
public class ClientLogic : IClientLogic
{
private readonly ILogger _logger;
private readonly IClientStorage _clientStorage;
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
{
_logger = logger;
_clientStorage = clientStorage;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
_logger.LogInformation("ReadList. ClientId:{Id}", 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 ClientViewModel? ReadElement(ClientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ClientFio:{ClientFio}.Id:{ Id}", model.ClientFIO, 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 bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
_logger.LogWarning("Update 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;
}
private void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ClientFIO))
{
throw new ArgumentNullException("Нет ФИО пользователя", nameof(model.ClientFIO));
}
if (string.IsNullOrEmpty(model.EMail))
{
throw new ArgumentNullException("Нет почты пользователя", nameof(model.EMail));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password));
}
_logger.LogInformation("Client. ClientFIO:{ClientFIO}.Email:{Email}.Password:{Password}.Id:{Id}",
model.ClientFIO, model.EMail, model.Password, model.Id);
var element = _clientStorage.GetElement(new ClientSearchModel
{
ClientFIO = model.ClientFIO
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с таким именем уже есть");
}
}
}
}

View File

@ -0,0 +1,96 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.BusinessLogicsContracts;
using ZooContracts.SearchModels;
using ZooContracts.StorageContracts;
using ZooContracts.ViewModels;
using ZooDataModels.Enums;
namespace ZooBusinessLogic.BusinessLogic
{
public class RouteLogic : IRouteLogic
{
private readonly ILogger _logger;
private readonly IRouteStorage _routeStorage;
public RouteLogic(ILogger<RouteLogic> logger, IRouteStorage routeStorage)
{
_logger = logger;
_routeStorage = routeStorage;
}
public List<RouteViewModel>? ReadList(RouteSearchModel? model)
{
_logger.LogInformation("ReadList. RouteName:{RouteName}. Id:{ Id}", model?.RouteName, model?.Id);
var list = model == null ? _routeStorage.GetFullList() : _routeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Create(RouteBindingModel model)
{
CheckModel(model);
if (_routeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool CheckRoutePay(RouteBindingModel model, RouteStatus status)
{
CheckModel(model);
var element = _routeStorage.GetElement(new RouteSearchModel { Id = model.Id });
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != status - 1)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
model.Status = status;
if (model.Status == RouteStatus.Неоплачен) model.DateFinish = DateTime.Now;
_routeStorage.Update(model);
return true;
}
public bool RouteWasPaid(RouteBindingModel model)
{
return CheckRoutePay(model, RouteStatus.Оплачен);
}
private void CheckModel(RouteBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.RouteName))
{
throw new ArgumentNullException("Нет названия маршрута",
nameof(model.RouteName));
}
if (model.RoutePrice <= 0)
{
throw new ArgumentNullException("Стоимость маршрута должна быть >0", nameof(model.RoutePrice));
}
_logger.LogInformation("Route. RouteName:{RouteName}.RoutePrice:{ RoutePrice} Id: { Id}", model.RouteName, model.RoutePrice, model.Id);
}
}
}

View File

@ -6,15 +6,16 @@ using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModels;
using ZooContracts.ViewModels;
using ZooDataModels.Enums;
namespace ZooContracts.BusinessLogicsContracts
{
public interface IRouteLogic
{
List<RouteViewModel>? ReadList(RouteSearchModel? model);
RouteViewModel? ReadElement(RouteSearchModel model);
bool Create(RouteBindingModel model);
bool Update(RouteBindingModel model);
bool Delete(RouteBindingModel model);
bool CheckRoutePay(RouteBindingModel model,RouteStatus status);
bool RouteWasPaid(RouteBindingModel model);
}
}