Skip to content

Commit 28264ca

Browse files
add resp
1 parent 2b5554e commit 28264ca

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Controllers/BaseController.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Text.Json.Serialization;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace PaymentCoreServiceApi.Controllers;
5+
6+
public class ApiResponseModel
7+
{
8+
public int Code { get; set; }
9+
public string Message { get; set; } = string.Empty;
10+
11+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
12+
public object? Data { get; set; }
13+
}
14+
15+
public abstract class BaseController : ControllerBase
16+
{
17+
protected IActionResult ApiResponse(int code, string message, object? data = null)
18+
{
19+
var response = new ApiResponseModel
20+
{
21+
Code = code,
22+
Message = message,
23+
Data = data
24+
};
25+
26+
return Ok(response);
27+
}
28+
29+
protected IActionResult SuccessResponse(object? data = null)
30+
{
31+
return ApiResponse(200, "Success", data);
32+
}
33+
34+
protected IActionResult ErrorResponse(string message, int code = 400)
35+
{
36+
return ApiResponse(code, message);
37+
}
38+
}

Controllers/UsersController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PaymentCoreServiceApi.Controllers;
99
[ApiController]
1010
[Route("api/[controller]")]
1111
[Authorize] // Yêu cầu authentication cho tất cả các endpoints trong controller
12-
public class UsersController : ControllerBase
12+
public class UsersController : BaseController
1313
{
1414
private readonly IMediator _mediator;
1515

@@ -30,6 +30,6 @@ public async Task<IActionResult> Create([FromBody] CreateUserCommand command)
3030
public async Task<IActionResult> GetProfile()
3131
{
3232
var profile = await _mediator.Send(new GetUserProfileQuery());
33-
return Ok(profile);
33+
return SuccessResponse(profile);
3434
}
3535
}

0 commit comments

Comments
 (0)