11package dev .luismachadoreis .flighttracker .server .ping .api ;
22
3+ import dev .luismachadoreis .flighttracker .server .ping .application .CreatePingCommand ;
4+ import dev .luismachadoreis .flighttracker .server .ping .application .GetRecentPingsQuery ;
35import dev .luismachadoreis .flighttracker .server .ping .application .dto .PingDTO ;
4- import dev .luismachadoreis .flighttracker .server .ping .application .usecase .CreatePingUseCase ;
5- import dev .luismachadoreis .flighttracker .server .ping .application .usecase .GetPingsUseCase ;
6- import io .swagger .v3 .oas .annotations .Operation ;
7- import io .swagger .v3 .oas .annotations .Parameter ;
8- import io .swagger .v3 .oas .annotations .media .Content ;
9- import io .swagger .v3 .oas .annotations .media .Schema ;
10- import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
11- import io .swagger .v3 .oas .annotations .tags .Tag ;
12- import org .springframework .beans .factory .annotation .Value ;
6+ import dev .luismachadoreis .flighttracker .server .common .application .cqs .mediator .Mediator ;
7+
138import org .springframework .http .ResponseEntity ;
149import org .springframework .web .bind .annotation .*;
10+ import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
1511
12+ import java .net .URI ;
1613import java .util .List ;
14+ import java .util .UUID ;
1715import java .util .Optional ;
16+ import lombok .extern .slf4j .Slf4j ;
17+ import io .swagger .v3 .oas .annotations .tags .Tag ;
18+ import io .swagger .v3 .oas .annotations .Operation ;
19+ import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
20+ import io .swagger .v3 .oas .annotations .media .Content ;
21+ import io .swagger .v3 .oas .annotations .media .Schema ;
22+ import org .springframework .beans .factory .annotation .Value ;
23+ import org .springframework .web .bind .annotation .RequestMapping ;
24+ import org .springframework .web .bind .annotation .RestController ;
1825
19- @ Tag ( name = "Ping API" , description = "API for managing flight position pings" )
26+ @ Slf4j
2027@ RestController
21- @ RequestMapping ("/api/pings" )
28+ @ RequestMapping ("/api/v2/pings" )
29+ @ Tag (name = "Ping API" , description = "API for managing flight position pings" )
2230public class PingController {
2331
24- private final CreatePingUseCase createPingUseCase ;
25- private final GetPingsUseCase getPingsUseCase ;
26- private final int defaultRequestLimit ;
32+ private final Mediator mediator ;
33+ private final Integer defaultRequestLimit ;
2734
28- public PingController (CreatePingUseCase createPingUseCase , GetPingsUseCase getPingsUseCase , @ Value ("${default.request.limit:50}" ) int defaultRequestLimit ) {
29- this .createPingUseCase = createPingUseCase ;
30- this .getPingsUseCase = getPingsUseCase ;
35+ public PingController (Mediator mediator , @ Value ("${default.request.limit:50}" ) Integer defaultRequestLimit ) {
36+ this .mediator = mediator ;
3137 this .defaultRequestLimit = defaultRequestLimit ;
3238 }
3339
@@ -43,8 +49,17 @@ public PingController(CreatePingUseCase createPingUseCase, GetPingsUseCase getPi
4349 }
4450 )
4551 @ PostMapping
46- public ResponseEntity <PingDTO > createPing (@ RequestBody PingDTO pingDTO ) {
47- return ResponseEntity .ok (createPingUseCase .execute (pingDTO ));
52+ public ResponseEntity <Void > createPing (@ RequestBody PingDTO pingDTO ) {
53+ UUID pingId = mediator .send (new CreatePingCommand (pingDTO ));
54+
55+ URI location = ServletUriComponentsBuilder
56+ .fromCurrentRequest ()
57+ .path ("/{id}" )
58+ .buildAndExpand (pingId )
59+ .toUri ();
60+
61+ log .info ("Ping created with id: {}" , pingId );
62+ return ResponseEntity .created (location ).build ();
4863 }
4964
5065 @ Operation (
@@ -59,15 +74,15 @@ public ResponseEntity<PingDTO> createPing(@RequestBody PingDTO pingDTO) {
5974 }
6075 )
6176 @ GetMapping
62- public ResponseEntity <List <PingDTO >> getPings (
63- @ Parameter (
64- description = "Maximum number of pings to retrieve" ,
65- example = "50"
66- )
67- @ RequestParam (required = false ) Integer limit
68- ) {
69- return ResponseEntity .ok (getPingsUseCase .execute (
70- Optional .ofNullable (limit ).orElse (defaultRequestLimit )
71- ));
77+ public ResponseEntity <List <PingDTO >> getRecentPings (@ RequestParam (required = false ) Integer limit ) {
78+ List <PingDTO > pings = mediator .send (
79+ new GetRecentPingsQuery (
80+ Optional .ofNullable (limit ).orElse (defaultRequestLimit )
81+ )
82+ );
83+
84+ log .info ("Recent pings retrieved: {}" , pings );
85+ return ResponseEntity .ok (pings );
7286 }
87+
7388}
0 commit comments