114 lines
4.0 KiB
C#
114 lines
4.0 KiB
C#
|
using ModuleTools.Enums;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace ModuleTools.Models
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Результат любой операции с сущностью
|
|||
|
/// </summary>
|
|||
|
public class OperationResultModel
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Успешность операции (количество ошибок = 0)
|
|||
|
/// </summary>
|
|||
|
public bool IsSucceeded => Errors.Count == 0;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Статус операции
|
|||
|
/// </summary>
|
|||
|
public ResultServiceStatusCode StatusCode { get; set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Спсиок ошибок
|
|||
|
/// </summary>
|
|||
|
public List<(string Title, string Message)> Errors { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Объект, получаемый по результатам операции
|
|||
|
/// </summary>
|
|||
|
public object Result { get; private set; }
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Конструктор по умолчанию
|
|||
|
/// </summary>
|
|||
|
public OperationResultModel()
|
|||
|
{
|
|||
|
Errors = new List<(string Title, string Message)>();
|
|||
|
StatusCode = ResultServiceStatusCode.Success;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Успешно
|
|||
|
/// </summary>
|
|||
|
/// <param name="obj"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static OperationResultModel Success(object obj)
|
|||
|
{
|
|||
|
return new OperationResultModel
|
|||
|
{
|
|||
|
Result = obj,
|
|||
|
Errors = new List<(string Title, string Message)>(),
|
|||
|
StatusCode = ResultServiceStatusCode.Success
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавление простой ошибки
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="value"></param>
|
|||
|
public static OperationResultModel Error(string key, string value) => Error(key, value, ResultServiceStatusCode.Error);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавление простой ошибки со сменой статуса
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="error"></param>
|
|||
|
/// <param name="statusCode"></param>
|
|||
|
public static OperationResultModel Error(string key, string error, ResultServiceStatusCode statusCode)
|
|||
|
{
|
|||
|
var model = new OperationResultModel();
|
|||
|
model.Errors.Add((key, error));
|
|||
|
model.StatusCode = statusCode;
|
|||
|
return model;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавление ошибки
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <param name="error"></param>
|
|||
|
public static OperationResultModel Error(Exception error) => Error(error, ResultServiceStatusCode.Error);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавление ошибки
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <param name="error"></param>
|
|||
|
/// <param name="statusCode"></param>
|
|||
|
public static OperationResultModel Error(Exception error, ResultServiceStatusCode statusCode) => Error("Ошибка", error, statusCode);
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Добавление ошибки (включая вложеннные)
|
|||
|
/// </summary>
|
|||
|
/// <param name="model"></param>
|
|||
|
/// <param name="key"></param>
|
|||
|
/// <param name="error"></param>
|
|||
|
/// <param name="statusCode"></param>
|
|||
|
public static OperationResultModel Error(string key, Exception error, ResultServiceStatusCode statusCode)
|
|||
|
{
|
|||
|
var model = new OperationResultModel();
|
|||
|
model.Errors.Add((key, error.Message));
|
|||
|
while (error.InnerException != null)
|
|||
|
{
|
|||
|
error = error.InnerException;
|
|||
|
model.Errors.Add(("Inner error:", error.Message));
|
|||
|
}
|
|||
|
model.StatusCode = statusCode;
|
|||
|
return model;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|