- FieldError DTO 공통화 (SPMS.Domain/Common) - ValidationErrorData + ApiResponse.ValidationFail() 추가 - InvalidModelStateResponseFactory로 ModelState 에러 ApiResponse 변환 - Controller Unauthorized 응답 throw SpmsException으로 통일 (에러코드 102) - MessageValidationService ValidationErrorDto → FieldError 교체 Closes #164
49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System.Text.Json.Serialization;
|
|
|
|
namespace SPMS.Domain.Common;
|
|
|
|
public class ApiResponse
|
|
{
|
|
[JsonPropertyName("result")]
|
|
public bool Result { get; init; }
|
|
|
|
[JsonPropertyName("code")]
|
|
public string Code { get; init; } = ErrorCodes.Success;
|
|
|
|
[JsonPropertyName("msg")]
|
|
public string Msg { get; init; } = string.Empty;
|
|
|
|
public static ApiResponse Success()
|
|
=> new() { Result = true, Code = ErrorCodes.Success };
|
|
|
|
public static ApiResponse Fail(string code, string msg)
|
|
=> new() { Result = false, Code = code, Msg = msg };
|
|
}
|
|
|
|
public class ApiResponse<T> : ApiResponse
|
|
{
|
|
[JsonPropertyName("data")]
|
|
public T? Data { get; init; }
|
|
|
|
public static ApiResponse<T> Success(T data)
|
|
=> new() { Result = true, Code = ErrorCodes.Success, Data = data };
|
|
|
|
public static ApiResponse<T> Success(T data, string msg)
|
|
=> new() { Result = true, Code = ErrorCodes.Success, Data = data, Msg = msg };
|
|
|
|
public new static ApiResponse<T> Fail(string code, string msg)
|
|
=> new() { Result = false, Code = code, Msg = msg };
|
|
}
|
|
|
|
public class ValidationErrorData
|
|
{
|
|
[JsonPropertyName("fieldErrors")]
|
|
public List<FieldError> FieldErrors { get; init; } = [];
|
|
}
|
|
|
|
public static class ApiResponseExtensions
|
|
{
|
|
public static ApiResponse<ValidationErrorData> ValidationFail(string code, string msg, List<FieldError> fieldErrors)
|
|
=> new() { Result = false, Code = code, Msg = msg, Data = new ValidationErrorData { FieldErrors = fieldErrors } };
|
|
}
|