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 : ApiResponse { [JsonPropertyName("data")] public T? Data { get; init; } public static ApiResponse Success(T data) => new() { Result = true, Code = ErrorCodes.Success, Data = data }; public static ApiResponse Success(T data, string msg) => new() { Result = true, Code = ErrorCodes.Success, Data = data, Msg = msg }; public new static ApiResponse Fail(string code, string msg) => new() { Result = false, Code = code, Msg = msg }; } public class ValidationErrorData { [JsonPropertyName("fieldErrors")] public List FieldErrors { get; init; } = []; } public static class ApiResponseExtensions { public static ApiResponse ValidationFail(string code, string msg, List fieldErrors) => new() { Result = false, Code = code, Msg = msg, Data = new ValidationErrorData { FieldErrors = fieldErrors } }; }