Source

main / Source / Excel / Excel.BizLogic / Models / ServiceResponse.cs

Full commit
using System;

namespace DataUp.Excel.BizLogic
{
    public class ServiceResponse
    {
        /// <summary>
        /// Gets or sets the value indicating whether the operation is successful or not.
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        /// Gets or sets the message of the response.
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// Create Failure status.
        /// </summary>
        /// <param name="exception">Exception instance.</param>
        /// <returns>Service response instance.</returns>
        public static ServiceResponse CreateFailureStatus(Exception exception)
        {
            return new ServiceResponse() { IsSuccess = false, Message = exception.Message };
        }

        /// <summary>
        /// Create failure status.
        /// </summary>
        /// <param name="customMessage">Custom message.</param>
        /// <returns>Service response instance.</returns>
        public static ServiceResponse CreateFailureStatus(string customMessage)
        {
            return new ServiceResponse() { IsSuccess = false, Message = customMessage };
        }

        /// <summary>
        /// Create success status.
        /// </summary>
        /// <param name="customMessage"></param>
        /// <returns>Service response instance.</returns>
        public static ServiceResponse CreateSuccessStatus(string customMessage)
        {
            return new ServiceResponse() { IsSuccess = true, Message = customMessage };
        }

        /// <summary>
        /// Create success status.
        /// </summary>
        /// <returns>Service response instance.</returns>
        public static ServiceResponse CreateSuccessStatus()
        {
            return new ServiceResponse() { IsSuccess = true };
        }

        /// <summary>
        /// Create success status.
        /// </summary>
        /// <param name="message">Custom message.</param>
        /// <returns>Service response instance.</returns>
        public static ServiceResponse CreateStatus(string message)
        {
            ServiceResponse status = new ServiceResponse();

            string[] respose = message.Split('|');

            if (respose.Length > 1)
            {
                bool success;
                bool.TryParse(respose[0], out success);

                status.IsSuccess = success;
                status.Message = respose[1];
            }
            else
            {
                status.IsSuccess = true;
                status.Message = message;
            }

            return status;
        }
    }
}