stylist / backend / Stylist.Domain / Models / ResponseResult.cs
ResponseResult.cs
Raw

namespace Stylist.Domain.Models
{
    public class ResponseResult
    {
        public static readonly ResponseResult Ok = new ResponseResult();
        public static ResponseResult NotFound(string entityName) => new ResponseResult($"Entity {entityName} hasn't been found.", isError: true);
        public static ResponseResult ShouldNotBeEmpty(params string[] entityNames) => new ResponseResult($"Properties {string.Join(", ", entityNames)} should not be empty.", isError: true);
        public static ResponseResult Error(string message) => new ResponseResult(message, isError: true);

        public ResponseResult(bool isError = false)
        {
            IsError = isError;
        }

        public ResponseResult(string message, bool isError = false)
        {
            Message = message;
            IsError = isError;
        }

        public string Message { get; private set; }

        public bool IsError { get; private set; }

        public void Deconstruct(out bool isError, out string message)
        {
            isError = IsError;
            message = Message;
        }

        public static ResponseResult operator &(ResponseResult first, ResponseResult second)
        {
            return new ResponseResult
            {
                IsError = first.IsError || second.IsError,
                Message = string.Join('\n', first.Message, second.Message)
            };
        }
    }

    public class ResponseResult<T> : ResponseResult where T : notnull
    {
        public ResponseResult(T data, bool isError = false)
            : base(isError)
        {
            Data = data;
        }

        public ResponseResult(T data, string message, bool isError = false)
            : base(message, isError)
        {
            Data = data;
        }

        public T Data { get; private set; }

        public static ResponseResult<T> Error(string message = "An error occured.", T data = default)
            => new(data, message, true);
        public static ResponseResult<T> NotFound(string entityName, T data = default)
            => new(data, $"Entity {entityName} hasn't been found.", isError: true);
        public static ResponseResult<T> Conflict(string entityName, string propertyName, T data = default)
            => new ResponseResult<T>(data, $"Entity {entityName} with that {propertyName} already exists.", isError: true);
        public new static ResponseResult<T> ShouldNotBeEmpty(params string[] entityNames)
            => new(default, $"Properties {string.Join(", ", entityNames)} should not be empty.", isError: true);

        public void Deconstruct(out bool isError, out string message, out T data)
        {
            isError = IsError;
            message = Message;
            data = Data;
        }

        public static implicit operator ResponseResult<T>(T data)
        {
            return new ResponseResult<T>(data);
        }
    }


}