11package com .hoangtien2k3 .paymentservice .api ;
22
33import com .hoangtien2k3 .paymentservice .dto .PaymentDto ;
4- import com .hoangtien2k3 .paymentservice .dto .response .collection .DtoCollectionResponse ;
5- import com .hoangtien2k3 .paymentservice .security .JwtValidate ;
64import com .hoangtien2k3 .paymentservice .service .PaymentService ;
5+ import io .swagger .annotations .ApiOperation ;
6+ import io .swagger .annotations .ApiResponse ;
7+ import io .swagger .annotations .ApiResponses ;
78import lombok .RequiredArgsConstructor ;
89import lombok .extern .slf4j .Slf4j ;
9- import org .apache .http .HttpStatus ;
1010import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .data .domain .Page ;
12+ import org .springframework .http .HttpStatus ;
1113import org .springframework .http .ResponseEntity ;
14+ import org .springframework .security .access .prepost .PreAuthorize ;
1215import org .springframework .web .bind .annotation .*;
1316import reactor .core .publisher .Mono ;
1417
1518import javax .validation .Valid ;
1619import javax .validation .constraints .NotBlank ;
1720import javax .validation .constraints .NotNull ;
21+ import java .util .Collections ;
1822import java .util .List ;
1923
20- @ Slf4j
21- @ RequiredArgsConstructor
2224@ RestController
2325@ RequestMapping ("/api/payments" )
26+ @ Slf4j
27+ @ RequiredArgsConstructor
2428public class PaymentController {
2529
2630 @ Autowired
2731 private final PaymentService paymentService ;
28- @ Autowired
29- private final JwtValidate jwtValidate ;
3032
33+ @ ApiOperation (value = "Get all payment" , notes = "Retrieve a list of all payment." )
34+ @ ApiResponses ({
35+ @ ApiResponse (code = 200 , message = "Payments retrieved successfully" , response = List .class ),
36+ @ ApiResponse (code = 204 , message = "No content" , response = ResponseEntity .class )
37+ })
3138 @ GetMapping
32- public ResponseEntity <DtoCollectionResponse <PaymentDto >> findAll () {
33- log .info ("PaymentDto List, controller; fetch all payments" );
34- return ResponseEntity .ok (new DtoCollectionResponse <>(this .paymentService .findAll ()));
39+ @ PreAuthorize ("hasAuthority('ADMIN')" )
40+ public Mono <ResponseEntity <List <PaymentDto >>> findAll () {
41+ log .info ("*** PaymentDto List, controller; fetch all categories *" );
42+ return paymentService .findAll ()
43+ .map (ResponseEntity ::ok )
44+ .defaultIfEmpty (ResponseEntity .ok (Collections .emptyList ()));
3545 }
3646
37- // @GetMapping
38- // public ResponseEntity<DtoCollectionResponse<PaymentDto>> findAll() {
39- // log.info("PaymentDto List, controller; fetch all payments");
40- //
41- // // Gọi phương thức findAll() từ PaymentService để lấy danh sách PaymentDto
42- // List<PaymentDto> paymentDtos = this.paymentService.findAll();
43- //
44- // // Trả về danh sách PaymentDto trong ResponseEntity
45- // return ResponseEntity.ok(new DtoCollectionResponse<>(paymentDtos));
46- // }
47-
48- @ GetMapping ("/inventory-test" )
49- @ ResponseBody
50- public Mono <ResponseEntity <List <String >>> callServiceB () {
51- String serviceBUrl = "http://localhost:8083/api/inventory?productName=iphone_13,iphone_13_red" ;
52-
53- return paymentService .callServiceB (serviceBUrl )
47+ @ ApiOperation (value = "Get all payments with paging" , notes = "Retrieve a paginated list of all payments." )
48+ @ ApiResponses ({
49+ @ ApiResponse (code = 200 , message = "Payments retrieved successfully" , response = Page .class ),
50+ @ ApiResponse (code = 204 , message = "No content" , response = ResponseEntity .class )
51+ })
52+ @ GetMapping ("/all" )
53+ @ PreAuthorize ("hasAuthority('ADMIN')" )
54+ public Mono <ResponseEntity <Page <PaymentDto >>> findAll (@ RequestParam (defaultValue = "0" ) int page ,
55+ @ RequestParam (defaultValue = "10" ) int size ,
56+ @ RequestParam (defaultValue = "paymentId" ) String sortBy ,
57+ @ RequestParam (defaultValue = "asc" ) String sortOrder ) {
58+ return paymentService .findAll (page , size , sortBy , sortOrder )
5459 .map (ResponseEntity ::ok )
55- .defaultIfEmpty (ResponseEntity .notFound ().build ());
60+ .defaultIfEmpty (ResponseEntity .noContent ().build ());
5661 }
5762
63+ @ ApiOperation (value = "Get payment by ID" , notes = "Retrieve cart information based on the provided ID." )
64+ @ ApiResponses ({
65+ @ ApiResponse (code = 200 , message = "Payment retrieved successfully" , response = PaymentDto .class ),
66+ @ ApiResponse (code = 404 , message = "Payment not found" , response = ResponseEntity .class )
67+ })
5868 @ GetMapping ("/{paymentId}" )
59- public ResponseEntity <PaymentDto > findById (@ PathVariable ("paymentId" )
60- @ NotBlank (message = "Input must not be blank" )
61- @ Valid final Integer paymentId ) {
62- log .info ("PaymentDto, resource; fetch payment by id" );
63- return ResponseEntity .ok (this .paymentService .findById (paymentId ));
69+ @ PreAuthorize ("hasAuthority('USER') or hasAuthority('ADMIN')" )
70+ public ResponseEntity <Mono <PaymentDto >> findById (@ PathVariable ("paymentId" )
71+ @ NotBlank (message = "Input must not be blank" )
72+ @ Valid final String paymentId ) {
73+ log .info ("*** PaymentDto, resource; fetch cart by id *" );
74+ return ResponseEntity .ok (paymentService .findById (Integer .parseInt (paymentId )));
6475 }
6576
77+ @ ApiOperation (value = "Save payment" , notes = "Save a new payment." )
78+ @ ApiResponses ({
79+ @ ApiResponse (code = 200 , message = "Payment saved successfully" , response = PaymentDto .class ),
80+ @ ApiResponse (code = 500 , message = "Internal Server Error" , response = ResponseEntity .class )
81+ })
6682 @ PostMapping
67- public ResponseEntity < PaymentDto > save ( @ RequestHeader ( name = "Authorization" ) String authorizationHeader ,
68- @ RequestBody @ NotNull ( message = "Input must not be NULL" )
69- @ Valid final PaymentDto paymentDto ) {
70- if (! jwtValidate . validateTokenUserService ( authorizationHeader ) ) {
71- ResponseEntity . status ( HttpStatus . SC_UNAUTHORIZED ). build ( );
72- }
73- log . info ( "PaymentDto, resource; save payment" );
74- return ResponseEntity .ok ( this . paymentService . save ( paymentDto ));
83+ @ PreAuthorize ( "hasAuthority('USER')" )
84+ public Mono < ResponseEntity < PaymentDto >> save ( @ RequestBody
85+ @ NotNull ( message = "Input must not be NULL!" )
86+ @ Valid final PaymentDto paymentDto ) {
87+ log . info ( "*** PaymentDto, resource; save payments *" );
88+ return paymentService . save ( paymentDto )
89+ . map ( ResponseEntity :: ok )
90+ . defaultIfEmpty ( ResponseEntity .status ( HttpStatus . INTERNAL_SERVER_ERROR ). build ( ));
7591 }
7692
93+ @ ApiOperation (value = "Update payment" , notes = "Update an existing payment." )
94+ @ ApiResponses ({
95+ @ ApiResponse (code = 200 , message = "Payment updated successfully" , response = PaymentDto .class ),
96+ @ ApiResponse (code = 404 , message = "Payment not found" , response = ResponseEntity .class )
97+ })
7798 @ PutMapping
78- public ResponseEntity <PaymentDto > update (@ RequestHeader (name = "Authorization" ) String authorizationHeader ,
79- @ RequestBody @ NotNull (message = "Input must not be NULL" )
80- @ Valid final PaymentDto paymentDto ) {
81- if (!jwtValidate .validateTokenUserService (authorizationHeader )) {
82- ResponseEntity .status (HttpStatus .SC_UNAUTHORIZED ).build ();
83- }
84- log .info ("PaymentDto, resource; update payment" );
85- return ResponseEntity .ok (this .paymentService .update (paymentDto ));
99+ @ PreAuthorize ("hasAuthority('ADMIN')" )
100+ public Mono <ResponseEntity <PaymentDto >> update (@ RequestBody
101+ @ NotNull (message = "Input must not be NULL" )
102+ @ Valid final PaymentDto paymentDto ) {
103+ log .info ("*** CartDto, resource; update cart *" );
104+ return paymentService .update (paymentDto )
105+ .map (ResponseEntity ::ok )
106+ .defaultIfEmpty (ResponseEntity .notFound ().build ());
107+ }
108+
109+
110+ @ ApiOperation (value = "Update payment by ID" , notes = "Update an existing cart based on the provided ID." )
111+ @ ApiResponses ({
112+ @ ApiResponse (code = 200 , message = "Payment updated successfully" , response = PaymentDto .class ),
113+ @ ApiResponse (code = 404 , message = "Payment not found" , response = ResponseEntity .class )
114+ })
115+ @ PutMapping ("/{paymentId}" )
116+ @ PreAuthorize ("hasAuthority('USER')" )
117+ public Mono <ResponseEntity <PaymentDto >> update (@ PathVariable ("paymentId" )
118+ @ NotBlank (message = "Input must not be blank" )
119+ @ Valid final Integer paymentId ,
120+ @ RequestBody
121+ @ NotNull (message = "Input must not be NULL" )
122+ @ Valid final PaymentDto paymentDto ) {
123+ log .info ("*** PaymentDto, resource; update cart with paymentId *" );
124+ return paymentService .update (paymentId , paymentDto )
125+ .map (ResponseEntity ::ok )
126+ .defaultIfEmpty (ResponseEntity .notFound ().build ());
86127 }
87128
129+ @ ApiOperation (value = "Delete payment by ID" , notes = "Delete a cart based on the provided ID." )
130+ @ ApiResponses ({
131+ @ ApiResponse (code = 200 , message = "Payment deleted successfully" , response = Boolean .class ),
132+ @ ApiResponse (code = 404 , message = "Payment not found" , response = ResponseEntity .class )
133+ })
88134 @ DeleteMapping ("/{paymentId}" )
89- public ResponseEntity <Boolean > deleteById (@ RequestHeader (name = "Authorization" ) String authorizationHeader ,
90- @ PathVariable ("paymentId" ) final String paymentId ) {
91- if (!jwtValidate .validateTokenUserService (authorizationHeader )) {
92- ResponseEntity .status (HttpStatus .SC_UNAUTHORIZED ).build ();
93- }
94- log .info ("Boolean, resource; delete payment by id" );
95- this .paymentService .deleteById (Integer .parseInt (paymentId ));
96- return ResponseEntity .ok (true );
135+ @ PreAuthorize ("hasAuthority('USER')" )
136+ public Mono <ResponseEntity <Boolean >> deleteById (@ PathVariable ("paymentId" ) final Integer paymentId ) {
137+ log .info ("*** Boolean, resource; delete payment by id *" );
138+ return paymentService .deleteById (paymentId )
139+ .thenReturn (ResponseEntity .ok (true ))
140+ .defaultIfEmpty (ResponseEntity .status (HttpStatus .NOT_FOUND ).body (false ));
97141 }
98142
99- }
143+ }
0 commit comments