2021-03-28 19:15:55 +04:00
|
|
|
|
using ModuleTools.BindingModels;
|
|
|
|
|
using ModuleTools.Enums;
|
|
|
|
|
using ModuleTools.Interfaces;
|
|
|
|
|
using ModuleTools.Models;
|
|
|
|
|
using ModuleTools.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace ModuleTools.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Базовый класс для логики сущности
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="G"></typeparam>
|
|
|
|
|
/// <typeparam name="S"></typeparam>
|
|
|
|
|
/// <typeparam name="L"></typeparam>
|
|
|
|
|
/// <typeparam name="E"></typeparam>
|
2021-04-02 20:46:41 +04:00
|
|
|
|
public class GenericBusinessLogic<G, S, L, E> : CoreBusinessLogic
|
2021-03-28 19:15:55 +04:00
|
|
|
|
where G : GetBindingModel
|
|
|
|
|
where S : SetBindingModel
|
|
|
|
|
where L : ListViewModel<E>
|
|
|
|
|
where E : ElementViewModel
|
|
|
|
|
{
|
2021-03-28 19:58:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис с хранилищем данных
|
|
|
|
|
/// </summary>
|
2021-03-29 12:13:47 +04:00
|
|
|
|
protected IGenerticEntityService<G, S> Service { get; set; }
|
2021-03-28 19:15:55 +04:00
|
|
|
|
|
2021-03-28 19:58:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Тип операции, скоторым работает логика
|
|
|
|
|
/// </summary>
|
2021-03-28 19:15:55 +04:00
|
|
|
|
protected readonly AccessOperation _serviceOperation;
|
|
|
|
|
|
2021-03-28 19:58:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Название сущности
|
|
|
|
|
/// </summary>
|
2021-03-28 19:15:55 +04:00
|
|
|
|
protected readonly string _entity;
|
|
|
|
|
|
2021-03-28 19:58:42 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возможен ли просмотр без авторизации
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected bool _allowSimpleView = true;
|
|
|
|
|
|
2021-03-29 12:13:47 +04:00
|
|
|
|
public GenericBusinessLogic(IGenerticEntityService<G, S> service, string entity, AccessOperation serviceOperation)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
|
|
|
|
Service = service;
|
|
|
|
|
_entity = entity;
|
|
|
|
|
_serviceOperation = serviceOperation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проверка доступности операции для пользователя
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected bool NoAccess(AccessBindingModel model, AccessType type)
|
|
|
|
|
{
|
2021-04-02 20:46:41 +04:00
|
|
|
|
if (_security.CheckAccess(new SecurityManagerCheckAccessModel(model, _serviceOperation, type, _entity)))
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
return false;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
2021-04-02 20:46:41 +04:00
|
|
|
|
Errors.Add(("Ошибка безопасности", _security.ErrorMessage));
|
2021-03-29 23:16:11 +04:00
|
|
|
|
return true;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение списка записей
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public L GetList(G model)
|
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
model.HaveRight = !NoAccess(model, AccessType.View);
|
2021-03-28 20:26:25 +04:00
|
|
|
|
if (model.HaveRight && !_allowSimpleView)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2021-03-28 19:58:42 +04:00
|
|
|
|
throw new MethodAccessException("Нет прав на получение списка");
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
var result = Service.Read(model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(Errors);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as L;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка получения", ex.Message));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public E GetElement(G model)
|
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-29 23:16:11 +04:00
|
|
|
|
model.HaveRight = !NoAccess(model, AccessType.View);
|
2021-03-28 20:26:25 +04:00
|
|
|
|
if (model.HaveRight && !_allowSimpleView)
|
2021-03-28 19:15:55 +04:00
|
|
|
|
{
|
2021-03-28 19:58:42 +04:00
|
|
|
|
throw new MethodAccessException("Нет прав на получение списка");
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
var result = Service.Read(model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(Errors);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as E;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка получения", ex.Message));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public E Create(S model)
|
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess(model, AccessType.Change))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var result = Service.Create(model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(Errors);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as E;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка создания", ex.Message));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Изменение записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public E Update(S model)
|
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess(model, AccessType.Change))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var result = Service.Update(model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(Errors);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:24:53 +04:00
|
|
|
|
return result.Result as E;
|
2021-03-28 19:15:55 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка изменения", ex.Message));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Delete(G model)
|
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess(model, AccessType.Delete))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
var result = Service.Delete(model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(Errors);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка удаления", ex.Message));
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|