|
6 | 6 |
|
7 | 7 | import org.couchbase.quickstart.springdata.models.Airline; |
8 | 8 | import org.couchbase.quickstart.springdata.services.AirlineService; |
9 | | -import org.springframework.beans.factory.annotation.Autowired; |
10 | 9 | import org.springframework.data.domain.Page; |
11 | 10 | import org.springframework.data.domain.PageRequest; |
12 | 11 | import org.springframework.http.HttpStatus; |
|
21 | 20 | import org.springframework.web.bind.annotation.RequestParam; |
22 | 21 | import org.springframework.web.bind.annotation.RestController; |
23 | 22 |
|
| 23 | +import com.couchbase.client.core.error.DocumentExistsException; |
| 24 | +import com.couchbase.client.core.error.DocumentNotFoundException; |
| 25 | + |
| 26 | +import io.swagger.v3.oas.annotations.Operation; |
| 27 | + |
24 | 28 | @RestController |
25 | 29 | @RequestMapping("/api/v1/airline") |
26 | 30 | public class AirlineController { |
27 | 31 |
|
28 | 32 | private AirlineService airlineService; |
29 | 33 |
|
30 | | - @Autowired |
31 | 34 | public AirlineController(AirlineService airlineService) { |
32 | 35 | this.airlineService = airlineService; |
33 | 36 | } |
34 | 37 |
|
35 | | - @PostMapping("/{id}") |
36 | | - public ResponseEntity<Airline> createAirline(@PathVariable String id, @Valid @RequestBody Airline airline) { |
37 | | - Airline createdAirline = airlineService.createAirline(id, airline); |
38 | | - return new ResponseEntity<>(createdAirline, HttpStatus.CREATED); |
39 | | - } |
40 | | - |
| 38 | + @Operation(summary = "Get an airline by ID") |
41 | 39 | @GetMapping("/{id}") |
42 | 40 | public ResponseEntity<Airline> getAirline(@PathVariable String id) { |
43 | | - Optional<Airline> airline = airlineService.getAirlineById(id); |
44 | | - return airline.map(value -> new ResponseEntity<>(value, HttpStatus.OK)) |
45 | | - .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
| 41 | + try { |
| 42 | + Optional<Airline> airline = airlineService.getAirlineById(id); |
| 43 | + return airline.map(value -> new ResponseEntity<>(value, HttpStatus.OK)) |
| 44 | + .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
| 45 | + } catch (DocumentNotFoundException e) { |
| 46 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 47 | + } catch (Exception e) { |
| 48 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 49 | + } |
46 | 50 | } |
47 | 51 |
|
| 52 | + @Operation(summary = "Create an airline") |
| 53 | + @PostMapping("/{id}") |
| 54 | + public ResponseEntity<Airline> createAirline(@Valid @RequestBody Airline airline) { |
| 55 | + try { |
| 56 | + Airline newAirline = airlineService.createAirline(airline); |
| 57 | + return new ResponseEntity<>(newAirline, HttpStatus.CREATED); |
| 58 | + } catch (DocumentExistsException e) { |
| 59 | + return new ResponseEntity<>(HttpStatus.CONFLICT); |
| 60 | + } catch (Exception e) { |
| 61 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Operation(summary = "Update an airline") |
48 | 66 | @PutMapping("/{id}") |
49 | 67 | public ResponseEntity<Airline> updateAirline(@PathVariable String id, @Valid @RequestBody Airline airline) { |
50 | | - Airline updatedAirline = airlineService.updateAirline(id, airline); |
51 | | - return new ResponseEntity<>(updatedAirline, HttpStatus.OK); |
| 68 | + try { |
| 69 | + Airline updatedAirline = airlineService.updateAirline(id, airline); |
| 70 | + if (updatedAirline != null) { |
| 71 | + return new ResponseEntity<>(updatedAirline, HttpStatus.OK); |
| 72 | + } else { |
| 73 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 74 | + } |
| 75 | + } catch (DocumentNotFoundException e) { |
| 76 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 77 | + } catch (Exception e) { |
| 78 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 79 | + } |
52 | 80 | } |
53 | 81 |
|
| 82 | + @Operation(summary = "Delete an airline") |
54 | 83 | @DeleteMapping("/{id}") |
55 | 84 | public ResponseEntity<Void> deleteAirline(@PathVariable String id) { |
56 | | - airlineService.deleteAirline(id); |
57 | | - return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 85 | + try { |
| 86 | + airlineService.deleteAirline(id); |
| 87 | + return new ResponseEntity<>(HttpStatus.NO_CONTENT); |
| 88 | + } catch (DocumentNotFoundException e) { |
| 89 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 90 | + } catch (Exception e) { |
| 91 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 92 | + } |
58 | 93 | } |
59 | 94 |
|
| 95 | + @Operation(summary = "List all airlines") |
60 | 96 | @GetMapping("/list") |
61 | 97 | public ResponseEntity<Page<Airline>> listAirlines(@RequestParam(defaultValue = "0") int page, |
62 | 98 | @RequestParam(defaultValue = "10") int size) { |
63 | 99 | Page<Airline> airlines = airlineService.getAllAirlines(PageRequest.of(page, size)); |
64 | 100 | return new ResponseEntity<>(airlines, HttpStatus.OK); |
65 | 101 | } |
66 | 102 |
|
| 103 | + @Operation(summary = "List all airlines by country") |
67 | 104 | @GetMapping("/country/{country}") |
68 | 105 | public ResponseEntity<Page<Airline>> listAirlinesByCountry( |
69 | 106 | @PathVariable String country, |
70 | 107 | @RequestParam(defaultValue = "0") int page, |
71 | 108 | @RequestParam(defaultValue = "10") int size) { |
72 | | - Page<Airline> airlines = airlineService.findByCountry(country, PageRequest.of(page, size)); |
73 | | - return new ResponseEntity<>(airlines, HttpStatus.OK); |
| 109 | + try { |
| 110 | + |
| 111 | + Page<Airline> airlines = airlineService.findByCountry(country, PageRequest.of(page, size)); |
| 112 | + return new ResponseEntity<>(airlines, HttpStatus.OK); |
| 113 | + } catch (Exception e) { |
| 114 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 115 | + } |
74 | 116 | } |
75 | 117 |
|
| 118 | + @Operation(summary = "List all airlines by desination airport") |
76 | 119 | @GetMapping("/destination/{destinationAirport}") |
77 | 120 | public ResponseEntity<Page<Airline>> listAirlinesByDestinationAirport( |
78 | 121 | @PathVariable String destinationAirport, |
79 | 122 | @RequestParam(defaultValue = "0") int page, |
80 | 123 | @RequestParam(defaultValue = "10") int size) { |
81 | 124 |
|
82 | | - Page<Airline> airlines = airlineService.findByDestinationAirport(destinationAirport, |
83 | | - PageRequest.of(page, size)); |
| 125 | + try { |
| 126 | + Page<Airline> airlines = airlineService.findByDestinationAirport(destinationAirport, |
| 127 | + PageRequest.of(page, size)); |
84 | 128 |
|
85 | | - return new ResponseEntity<>(airlines, HttpStatus.OK); |
| 129 | + return new ResponseEntity<>(airlines, HttpStatus.OK); |
| 130 | + } catch (Exception e) { |
| 131 | + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); |
| 132 | + } |
86 | 133 | } |
87 | 134 | } |
0 commit comments