2023-02-21 19:55:32 +01:00
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace Shared.Models;
|
2023-02-09 10:25:42 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A standard response for service calls.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Return data type</typeparam>
|
|
|
|
|
public class ServiceResult<T> : ServiceResult
|
|
|
|
|
{
|
|
|
|
|
public T? Data { get; set; }
|
|
|
|
|
|
2023-02-17 18:29:20 +01:00
|
|
|
|
public ServiceResult() { }
|
|
|
|
|
|
2023-02-09 10:25:42 +01:00
|
|
|
|
public ServiceResult(T? data)
|
|
|
|
|
{
|
|
|
|
|
Data = data;
|
|
|
|
|
}
|
2023-02-21 19:55:32 +01:00
|
|
|
|
|
2023-02-09 10:25:42 +01:00
|
|
|
|
public ServiceResult(T? data, ServiceError error) : base(error)
|
|
|
|
|
{
|
|
|
|
|
Data = data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ServiceResult(ServiceError error) : base(error)
|
|
|
|
|
{
|
2023-02-21 19:55:32 +01:00
|
|
|
|
|
2023-02-09 10:25:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ServiceResult
|
|
|
|
|
{
|
2023-02-11 18:09:29 +01:00
|
|
|
|
public bool Succeeded => Error is null;
|
2023-02-09 10:25:42 +01:00
|
|
|
|
|
|
|
|
|
public ServiceError? Error { get; set; }
|
|
|
|
|
|
|
|
|
|
public ServiceResult(ServiceError? error)
|
|
|
|
|
{
|
|
|
|
|
error ??= ServiceError.DefaultError;
|
|
|
|
|
|
|
|
|
|
Error = error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ServiceResult() { }
|
|
|
|
|
|
|
|
|
|
#region Helper Methods
|
|
|
|
|
|
|
|
|
|
public static ServiceResult Failed(ServiceError error)
|
|
|
|
|
{
|
|
|
|
|
return new ServiceResult(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ServiceResult<T> Failed<T>(ServiceError error)
|
|
|
|
|
{
|
|
|
|
|
return new ServiceResult<T>(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ServiceResult<T> Failed<T>(T data, ServiceError error)
|
|
|
|
|
{
|
|
|
|
|
return new ServiceResult<T>(data, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ServiceResult<T> Success<T>(T data)
|
|
|
|
|
{
|
|
|
|
|
return new ServiceResult<T>(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|