File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed
Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments