Skip to content

Commit 407214c

Browse files
committed
Add direct connections query to AirportRepository and AirportService
1 parent d29e544 commit 407214c

File tree

6 files changed

+127
-117
lines changed

6 files changed

+127
-117
lines changed

src/main/java/org/couchbase/quickstart/springdata/config/DBSetupRunner.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ public class DBSetupRunner implements CommandLineRunner {
2626

2727
@Override
2828
public void run(String... strings) throws Exception {
29-
Optional<Airline> airline = airlineRepository.findById("airline_10"); // SFO
29+
Optional<Airline> airline = airlineRepository.findById("airline_10");
3030
if (airline.isPresent()) {
31-
System.out.println("got SFO: " + airline.get());
31+
System.out.println("got airline_10: " + airline.get());
3232
} else {
33-
System.out.println("SFO not found");
33+
System.out.println("airline_10 not found");
3434
}
3535

36-
Optional<Airport> airport = airportRepository.findById("airport_1254"); // SFO
36+
Optional<Airport> airport = airportRepository.findById("airport_1254");
3737
if (airport.isPresent()) {
38-
System.out.println("got SFO: " + airport.get());
38+
System.out.println("got airport_1254: " + airport.get());
3939
} else {
40-
System.out.println("SFO not found");
40+
System.out.println("airport_1254 not found");
4141
}
4242

43-
Optional<Route> route = routeRepository.findById("route_10000"); // SFO
43+
Optional<Route> route = routeRepository.findById("route_10000");
4444
if (route.isPresent()) {
45-
System.out.println("got SFO: " + route.get());
45+
System.out.println("got route_10000: " + route.get());
4646
} else {
47-
System.out.println("SFO not found");
47+
System.out.println("route_10000 not found");
4848
}
4949
try {
5050
} catch (Exception e) {
Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package org.couchbase.quickstart.springdata.controller;
22

3-
43
import java.util.Optional;
54

65
import org.couchbase.quickstart.springdata.model.Airport;
76
import org.couchbase.quickstart.springdata.services.AirportService;
87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.data.domain.Page;
10-
import org.springframework.data.domain.Pageable;
9+
import org.springframework.data.domain.PageRequest;
1110
import org.springframework.http.HttpStatus;
1211
import org.springframework.http.ResponseEntity;
1312
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -17,43 +16,54 @@
1716
import org.springframework.web.bind.annotation.PutMapping;
1817
import org.springframework.web.bind.annotation.RequestBody;
1918
import org.springframework.web.bind.annotation.RequestMapping;
19+
import org.springframework.web.bind.annotation.RequestParam;
2020
import org.springframework.web.bind.annotation.RestController;
2121

2222
@RestController
2323
@RequestMapping("/api/v1/airport")
2424
public class AirportController {
2525

26-
@Autowired
27-
private AirportService airportService;
28-
29-
@PostMapping("/{id}")
30-
public ResponseEntity<Airport> createAirport(@PathVariable String id, @RequestBody Airport airport) {
31-
Airport createdAirport = airportService.createAirport(id, airport);
32-
return new ResponseEntity<>(createdAirport, HttpStatus.CREATED);
33-
}
34-
35-
@GetMapping("/{id}")
36-
public ResponseEntity<Airport> getAirport(@PathVariable String id) {
37-
Optional<Airport> airport = airportService.getAirportById(id);
38-
return airport.map(value -> new ResponseEntity<>(value, HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
39-
}
40-
41-
@PutMapping("/{id}")
42-
public ResponseEntity<Airport> updateAirport(@PathVariable String id, @RequestBody Airport airport) {
43-
Airport updatedAirport = airportService.updateAirport(id, airport);
44-
return new ResponseEntity<>(updatedAirport, HttpStatus.OK);
45-
}
46-
47-
@DeleteMapping("/{id}")
48-
public ResponseEntity<Void> deleteAirport(@PathVariable String id) {
49-
airportService.deleteAirport(id);
50-
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
51-
}
52-
53-
@GetMapping("/list")
54-
public ResponseEntity<Page<Airport>> listAirports(Pageable pageable) {
55-
Page<Airport> airports = airportService.getAllAirports(pageable);
56-
return new ResponseEntity<>(airports, HttpStatus.OK);
57-
}
26+
@Autowired
27+
private AirportService airportService;
28+
29+
@PostMapping("/{id}")
30+
public ResponseEntity<Airport> createAirport(@PathVariable String id, @RequestBody Airport airport) {
31+
Airport createdAirport = airportService.createAirport(id, airport);
32+
return new ResponseEntity<>(createdAirport, HttpStatus.CREATED);
33+
}
34+
35+
@GetMapping("/{id}")
36+
public ResponseEntity<Airport> getAirport(@PathVariable String id) {
37+
Optional<Airport> airport = airportService.getAirportById(id);
38+
return airport.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
39+
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
40+
}
41+
42+
@PutMapping("/{id}")
43+
public ResponseEntity<Airport> updateAirport(@PathVariable String id, @RequestBody Airport airport) {
44+
Airport updatedAirport = airportService.updateAirport(id, airport);
45+
return new ResponseEntity<>(updatedAirport, HttpStatus.OK);
46+
}
47+
48+
@DeleteMapping("/{id}")
49+
public ResponseEntity<Void> deleteAirport(@PathVariable String id) {
50+
airportService.deleteAirport(id);
51+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
52+
}
53+
54+
@GetMapping("/list")
55+
public ResponseEntity<Page<Airport>> listAirports(@RequestParam(defaultValue = "0") int page,
56+
@RequestParam(defaultValue = "10") int size) {
57+
Page<Airport> airports = airportService.getAllAirports(PageRequest.of(page, size));
58+
return new ResponseEntity<>(airports, HttpStatus.OK);
59+
}
5860

61+
@GetMapping("/direct-connections")
62+
public ResponseEntity<Page<Airport>> listDirectConnections(
63+
@RequestParam String airport,
64+
@RequestParam(defaultValue = "0") int page,
65+
@RequestParam(defaultValue = "10") int size) {
66+
Page<Airport> airports = airportService.getDirectConnections(airport, PageRequest.of(page, size));
67+
return new ResponseEntity<>(airports, HttpStatus.OK);
68+
}
5969
}

src/main/java/org/couchbase/quickstart/springdata/controller/RouteController.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import org.couchbase.quickstart.springdata.services.RouteService;
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.data.domain.Page;
9-
import org.springframework.data.domain.Pageable;
9+
import org.springframework.data.domain.PageRequest;
1010
import org.springframework.http.HttpStatus;
1111
import org.springframework.http.ResponseEntity;
1212
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -16,43 +16,46 @@
1616
import org.springframework.web.bind.annotation.PutMapping;
1717
import org.springframework.web.bind.annotation.RequestBody;
1818
import org.springframework.web.bind.annotation.RequestMapping;
19+
import org.springframework.web.bind.annotation.RequestParam;
1920
import org.springframework.web.bind.annotation.RestController;
2021

2122
@RestController
2223
@RequestMapping("/api/v1/route")
2324
public class RouteController {
2425

25-
@Autowired
26-
private RouteService routeService;
27-
28-
@PostMapping("/{id}")
29-
public ResponseEntity<Route> createRoute(@PathVariable String id, @RequestBody Route route) {
30-
Route createdRoute = routeService.createRoute(id, route);
31-
return new ResponseEntity<>(createdRoute, HttpStatus.CREATED);
32-
}
33-
34-
@GetMapping("/{id}")
35-
public ResponseEntity<Route> getRoute(@PathVariable String id) {
36-
Optional<Route> route = routeService.getRouteById(id);
37-
return route.map(value -> new ResponseEntity<>(value, HttpStatus.OK)).orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
38-
}
39-
40-
@PutMapping("/{id}")
41-
public ResponseEntity<Route> updateRoute(@PathVariable String id, @RequestBody Route route) {
42-
Route updatedRoute = routeService.updateRoute(id, route);
43-
return new ResponseEntity<>(updatedRoute, HttpStatus.OK);
44-
}
45-
46-
@DeleteMapping("/{id}")
47-
public ResponseEntity<Void> deleteRoute(@PathVariable String id) {
48-
routeService.deleteRoute(id);
49-
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
50-
}
51-
52-
@GetMapping("/list")
53-
public ResponseEntity<Page<Route>> listRoutes(Pageable pageable) {
54-
Page<Route> routes = routeService.getAllRoutes(pageable);
55-
return new ResponseEntity<>(routes, HttpStatus.OK);
26+
@Autowired
27+
private RouteService routeService;
28+
29+
@PostMapping("/{id}")
30+
public ResponseEntity<Route> createRoute(@PathVariable String id, @RequestBody Route route) {
31+
Route createdRoute = routeService.createRoute(id, route);
32+
return new ResponseEntity<>(createdRoute, HttpStatus.CREATED);
33+
}
34+
35+
@GetMapping("/{id}")
36+
public ResponseEntity<Route> getRoute(@PathVariable String id) {
37+
Optional<Route> route = routeService.getRouteById(id);
38+
return route.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
39+
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
40+
}
41+
42+
@PutMapping("/{id}")
43+
public ResponseEntity<Route> updateRoute(@PathVariable String id, @RequestBody Route route) {
44+
Route updatedRoute = routeService.updateRoute(id, route);
45+
return new ResponseEntity<>(updatedRoute, HttpStatus.OK);
46+
}
47+
48+
@DeleteMapping("/{id}")
49+
public ResponseEntity<Void> deleteRoute(@PathVariable String id) {
50+
routeService.deleteRoute(id);
51+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
52+
}
53+
54+
@GetMapping("/list")
55+
public ResponseEntity<Page<Route>> listRoutes(@RequestParam(defaultValue = "0") int page,
56+
@RequestParam(defaultValue = "10") int size) {
57+
Page<Route> routes = routeService.getAllRoutes(PageRequest.of(page, size));
58+
return new ResponseEntity<>(routes, HttpStatus.OK);
5659
}
5760

5861
}

src/main/java/org/couchbase/quickstart/springdata/repository/AirlineRepository.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,16 @@
1818
@Repository("airlineRepository")
1919
public interface AirlineRepository extends CouchbaseRepository<Airline, String> {
2020

21-
// Create the query to find all airlines
22-
23-
// @Query("SELECT META().id AS _ID, callsign, country, iata, icao, name, type
24-
// FROM #{#n1ql.bucket}")
25-
// @Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter}")
2621
@Query("#{#n1ql.selectEntity}")
2722
Page<Airline> findAll(Pageable pageable);
2823

29-
// @Query("SELECT META().id AS _ID, callsign, country, iata, icao, name, type
30-
// FROM #{#n1ql.bucket} AND country = 'United States'")
3124
@Query("#{#n1ql.selectEntity} WHERE country = $1")
3225
Page<Airline> findByCountry(String country, Pageable pageable);
3326

34-
// @Query("#{#n1ql.selectEntity} FROM (SELECT DISTINCT META(airline).id AS
35-
// airlineId FROM route " +
36-
// "JOIN airline ON route.airlineid = META(airline).id " +
37-
// "WHERE route.destinationairport = $1) AS subquery " +
38-
// "JOIN #{#n1ql.bucket} AS air ON META(air).id = subquery.airlineId")
39-
4027
@Query("SELECT META(air).id AS __id, air.callsign, air.country, air.iata, air.icao, air.id, air.name, air.type " +
4128
"FROM (SELECT DISTINCT META(airline).id AS airlineId FROM route " +
4229
"JOIN airline ON route.airlineid = META(airline).id " +
43-
"WHERE route.destinationairport = 'MRS') AS subquery " +
30+
"WHERE route.destinationairport = $1) AS subquery " +
4431
"JOIN #{#n1ql.bucket} AS air ON META(air).id = subquery.airlineId")
4532
Page<Airline> findByDestinationAirport(String destinationAirport, Pageable pageable);
4633

src/main/java/org/couchbase/quickstart/springdata/repository/AirportRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ public interface AirportRepository extends CouchbaseRepository<Airport, String>,
2222
@Query("SELECT META().id AS _ID, type, airportname, city, country, faa, icao, tz, geo AS geo FROM #{#n1ql.bucket} WHERE type = 'airport'")
2323
Page<Airport> findAll(Pageable pageable);
2424

25+
@Query("SELECT DISTINCT META(airport).id AS __id,airport.* " +
26+
"FROM airport as airport " +
27+
"JOIN route as route ON airport.faa = route.sourceairport " +
28+
"WHERE airport.faa = $1 AND route.stops = 0")
29+
Page<Airport> getDirectConnections(String targetAirportCode, Pageable pageable);
30+
2531
}

src/main/java/org/couchbase/quickstart/springdata/services/AirportService.java

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,37 @@
1212
@Service
1313
public class AirportService {
1414

15-
@Autowired
16-
private AirportRepository airportRepository;
17-
18-
public Page<Airport> getAllAirports(Pageable pageable) {
19-
return airportRepository.findAll(pageable);
20-
}
21-
22-
public Optional<Airport> getAirportById(String id) {
23-
return airportRepository.findById(id);
24-
}
25-
26-
public Airport saveAirport(Airport airport) {
27-
return airportRepository.save(airport);
28-
}
29-
30-
public void deleteAirport(String id) {
31-
airportRepository.deleteById(id);
32-
}
33-
34-
public Airport createAirport(String id, Airport airport) {
35-
airport.setId(id);
36-
return airportRepository.save(airport);
37-
}
38-
39-
public Airport updateAirport(String id, Airport airport) {
40-
airport.setId(id);
41-
return airportRepository.save(airport);
42-
}
15+
@Autowired
16+
private AirportRepository airportRepository;
17+
18+
public Page<Airport> getAllAirports(Pageable pageable) {
19+
return airportRepository.findAll(pageable);
20+
}
21+
22+
public Optional<Airport> getAirportById(String id) {
23+
return airportRepository.findById(id);
24+
}
25+
26+
public Airport saveAirport(Airport airport) {
27+
return airportRepository.save(airport);
28+
}
29+
30+
public void deleteAirport(String id) {
31+
airportRepository.deleteById(id);
32+
}
33+
34+
public Airport createAirport(String id, Airport airport) {
35+
airport.setId(id);
36+
return airportRepository.save(airport);
37+
}
38+
39+
public Airport updateAirport(String id, Airport airport) {
40+
airport.setId(id);
41+
return airportRepository.save(airport);
42+
}
43+
44+
public Page<Airport> getDirectConnections(String id, Pageable pageable) {
45+
return airportRepository.getDirectConnections(id, pageable);
46+
}
4347

4448
}

0 commit comments

Comments
 (0)