50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using _2021_backend.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.Serialization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace _2021_backend.Utils
|
|
{
|
|
public abstract class ResponseDto
|
|
{
|
|
[DataMember]
|
|
public string Status;
|
|
}
|
|
[DataContract]
|
|
public class SuccessResponseDto : ResponseDto
|
|
{
|
|
[DataMember]
|
|
public new string Status { get; set; }
|
|
[DataMember(EmitDefaultValue = false)]
|
|
public object Data { get; set; }
|
|
public SuccessResponseDto()
|
|
{
|
|
Status = "success";
|
|
}
|
|
}
|
|
[DataContract]
|
|
public class ErrorResponseDto : ResponseDto
|
|
{
|
|
[DataMember]
|
|
public new string Status { get; set; }
|
|
[DataMember(EmitDefaultValue = false)]
|
|
public string ErrorMsg { get; set; }
|
|
public ErrorResponseDto()
|
|
{
|
|
Status = "error";
|
|
}
|
|
}
|
|
public static class ApiResponse
|
|
{
|
|
public static SuccessResponseDto Success(object data) => new SuccessResponseDto { Data = data };
|
|
public static ErrorResponseDto Error(string errorMsg) => new ErrorResponseDto { ErrorMsg = errorMsg };
|
|
|
|
public static ErrorResponseDto Error(string errorMsg, status stat)
|
|
{
|
|
return new ErrorResponseDto { ErrorMsg = errorMsg, Status = Enum.GetName(typeof(status), stat) };
|
|
}
|
|
}
|
|
}
|