using System.Text.Json.Serialization;
namespace DigitalData.Core.Application.Abstraction.DTO
{
///
/// Represents the result of an operation, containing information about its success or failure,
/// messages for the client, and notices for logging.
///
public class Result
{
///
/// Gets or sets a value indicating whether the operation was successful.
///
public bool IsSuccess { get; set; } = false;
///
/// Gets a value indicating whether the operation failed.
///
public bool IsFailed => !IsSuccess;
///
/// Gets a list of messages intended for the client.
///
public List Messages { get; init; } = new();
///
/// Gets a list of notices intended for logging purposes. This property is ignored during JSON serialization.
///
[JsonIgnore]
public List Notices = new();
///
/// Creates a with the specified data.
///
/// The type of the data.
/// The data to include in the result.
/// A new instance.
public DataResult Data(T data) => new()
{
IsSuccess = IsSuccess,
Messages = Messages,
Notices = Notices,
Data = data
};
///
/// Checks if any notice has the specified flag.
///
/// The flag to check.
/// True if any notice has the specified flag; otherwise, false.
public bool HasFlag(Enum flag) => Notices.Any(n => n.Flag?.ToString() == flag.ToString());
///
/// Checks if any notice has any of the specified flags.
///
/// The flags to check.
/// True if any notice has any of the specified flags; otherwise, false.
public bool HasAnyFlag(params Enum[] flags) => flags.Any(HasFlag);
///
/// Creates a new successful .
///
/// A new successful .
public static Result Success() => new() { IsSuccess = true };
///
/// Creates a new failed .
///
/// A new failed .
public static Result Fail() => new() { IsSuccess = false };
///
/// Creates a new successful with the specified data.
///
/// The type of the data.
/// The data to include in the result.
/// A new successful with the specified data.
public static DataResult Success(T data) => new()
{
IsSuccess = true,
Data = data
};
///
/// Creates a new failed with no data.
///
/// The type of the data.
/// A new failed with no data.
#pragma warning disable CS8601 // Possible null reference assignment.
public static DataResult Fail() => new()
{
IsSuccess = false,
Data = default
};
#pragma warning restore CS8601 // Possible null reference assignment.
}
}