Skip to content

Commit 496204d

Browse files
committed
Add Dockerfile and make repository classes final
1 parent 63bd9fa commit 496204d

File tree

11 files changed

+94
-13
lines changed

11 files changed

+94
-13
lines changed

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# NOT WORKING
2+
FROM gradle:7.3.3-jdk11 AS build
3+
COPY --chown=gradle:gradle . /home/gradle/src
4+
WORKDIR /home/gradle/src
5+
RUN gradle build --no-daemon
6+
7+
FROM openjdk:11-jre-slim
8+
9+
EXPOSE 8080
10+
11+
RUN mkdir /app
12+
13+
COPY --from=build /home/gradle/src/build/libs/*.jar /app/java-springdata-quickstart.jar
14+
15+
ENTRYPOINT ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Djava.security.egd=file:/dev/./urandom","-jar","/app/java-springdata-quickstart.jar"]

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
group = 'org.couchbase.quickstart.springdata'
88
version = '0.0.1-SNAPSHOT'
99
sourceCompatibility = '1.8'
10+
archivesBaseName = 'java-springdata-quickstart'
1011

1112
repositories {
1213
mavenCentral()

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.couchbase.quickstart.springdata.models.Airline;
88
import org.couchbase.quickstart.springdata.services.AirlineService;
9+
import org.springframework.dao.DataRetrievalFailureException;
910
import org.springframework.data.domain.Page;
1011
import org.springframework.data.domain.PageRequest;
1112
import org.springframework.http.HttpStatus;
@@ -24,17 +25,24 @@
2425
import com.couchbase.client.core.error.DocumentNotFoundException;
2526

2627
import io.swagger.v3.oas.annotations.Operation;
28+
import lombok.extern.slf4j.Slf4j;
2729

30+
@Slf4j
2831
@RestController
2932
@RequestMapping("/api/v1/airline")
3033
public class AirlineController {
3134

32-
private AirlineService airlineService;
35+
private final AirlineService airlineService;
3336

3437
public AirlineController(AirlineService airlineService) {
3538
this.airlineService = airlineService;
3639
}
3740

41+
// All Errors
42+
private static final String INTERNAL_SERVER_ERROR = "Internal server error";
43+
private static final String DOCUMENT_NOT_FOUND = "Document not found";
44+
private static final String DOCUMENT_ALREADY_EXISTS = "Document already exists";
45+
3846
@Operation(summary = "Get an airline by ID")
3947
@GetMapping("/{id}")
4048
public ResponseEntity<Airline> getAirline(@PathVariable String id) {
@@ -43,8 +51,10 @@ public ResponseEntity<Airline> getAirline(@PathVariable String id) {
4351
return airline.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
4452
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
4553
} catch (DocumentNotFoundException e) {
54+
log.error(DOCUMENT_NOT_FOUND, e);
4655
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4756
} catch (Exception e) {
57+
log.error(INTERNAL_SERVER_ERROR, e);
4858
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
4959
}
5060
}
@@ -56,8 +66,10 @@ public ResponseEntity<Airline> createAirline(@Valid @RequestBody Airline airline
5666
Airline newAirline = airlineService.createAirline(airline);
5767
return new ResponseEntity<>(newAirline, HttpStatus.CREATED);
5868
} catch (DocumentExistsException e) {
69+
log.error(DOCUMENT_ALREADY_EXISTS, e);
5970
return new ResponseEntity<>(HttpStatus.CONFLICT);
6071
} catch (Exception e) {
72+
log.error(INTERNAL_SERVER_ERROR, e);
6173
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
6274
}
6375
}
@@ -72,9 +84,11 @@ public ResponseEntity<Airline> updateAirline(@PathVariable String id, @Valid @Re
7284
} else {
7385
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7486
}
75-
} catch (DocumentNotFoundException e) {
87+
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
88+
log.error(DOCUMENT_NOT_FOUND, e);
7689
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7790
} catch (Exception e) {
91+
log.error(INTERNAL_SERVER_ERROR, e);
7892
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
7993
}
8094
}
@@ -85,9 +99,11 @@ public ResponseEntity<Void> deleteAirline(@PathVariable String id) {
8599
try {
86100
airlineService.deleteAirline(id);
87101
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
88-
} catch (DocumentNotFoundException e) {
102+
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
103+
log.error(DOCUMENT_NOT_FOUND, e);
89104
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
90105
} catch (Exception e) {
106+
log.error(INTERNAL_SERVER_ERROR, e);
91107
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
92108
}
93109
}
@@ -96,8 +112,13 @@ public ResponseEntity<Void> deleteAirline(@PathVariable String id) {
96112
@GetMapping("/list")
97113
public ResponseEntity<Page<Airline>> listAirlines(@RequestParam(defaultValue = "0") int page,
98114
@RequestParam(defaultValue = "10") int size) {
99-
Page<Airline> airlines = airlineService.getAllAirlines(PageRequest.of(page, size));
100-
return new ResponseEntity<>(airlines, HttpStatus.OK);
115+
try {
116+
Page<Airline> airlines = airlineService.getAllAirlines(PageRequest.of(page, size));
117+
return new ResponseEntity<>(airlines, HttpStatus.OK);
118+
} catch (Exception e) {
119+
log.error(INTERNAL_SERVER_ERROR, e);
120+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
121+
}
101122
}
102123

103124
@Operation(summary = "List all airlines by country")
@@ -111,6 +132,7 @@ public ResponseEntity<Page<Airline>> listAirlinesByCountry(
111132
Page<Airline> airlines = airlineService.findByCountry(country, PageRequest.of(page, size));
112133
return new ResponseEntity<>(airlines, HttpStatus.OK);
113134
} catch (Exception e) {
135+
log.error(INTERNAL_SERVER_ERROR, e);
114136
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
115137
}
116138
}
@@ -128,6 +150,7 @@ public ResponseEntity<Page<Airline>> listAirlinesByDestinationAirport(
128150

129151
return new ResponseEntity<>(airlines, HttpStatus.OK);
130152
} catch (Exception e) {
153+
log.error(INTERNAL_SERVER_ERROR, e);
131154
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
132155
}
133156
}

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.couchbase.quickstart.springdata.models.Airport;
88
import org.couchbase.quickstart.springdata.services.AirportService;
9+
import org.springframework.dao.DataRetrievalFailureException;
910
import org.springframework.data.domain.Page;
1011
import org.springframework.data.domain.PageRequest;
1112
import org.springframework.http.HttpStatus;
@@ -24,27 +25,36 @@
2425
import com.couchbase.client.core.error.DocumentNotFoundException;
2526

2627
import io.swagger.v3.oas.annotations.Operation;
28+
import lombok.extern.slf4j.Slf4j;
2729

2830
@RestController
2931
@RequestMapping("/api/v1/airport")
32+
@Slf4j
3033
public class AirportController {
3134

32-
private AirportService airportService;
35+
private final AirportService airportService;
3336

3437
public AirportController(AirportService airportService) {
3538
this.airportService = airportService;
3639
}
3740

41+
// All Errors
42+
private static final String INTERNAL_SERVER_ERROR = "Internal server error";
43+
private static final String DOCUMENT_NOT_FOUND = "Document not found";
44+
private static final String DOCUMENT_ALREADY_EXISTS = "Document already exists";
45+
3846
@Operation(summary = "Get an airport by ID")
3947
@GetMapping("/{id}")
4048
public ResponseEntity<Airport> getAirport(@PathVariable String id) {
4149
try {
4250
Optional<Airport> airport = airportService.getAirportById(id);
4351
return airport.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
4452
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
45-
} catch (DocumentNotFoundException e) {
53+
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
54+
log.error(DOCUMENT_NOT_FOUND, e);
4655
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4756
} catch (Exception e) {
57+
log.error(INTERNAL_SERVER_ERROR, e);
4858
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
4959
}
5060
}
@@ -56,8 +66,10 @@ public ResponseEntity<Airport> createAirport(@PathVariable String id, @Valid @Re
5666
Airport newAirport = airportService.createAirport(airport);
5767
return new ResponseEntity<>(newAirport, HttpStatus.CREATED);
5868
} catch (DocumentExistsException e) {
69+
log.error(DOCUMENT_ALREADY_EXISTS, e);
5970
return new ResponseEntity<>(HttpStatus.CONFLICT);
6071
} catch (Exception e) {
72+
log.error(INTERNAL_SERVER_ERROR, e);
6173
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
6274
}
6375

@@ -71,11 +83,14 @@ public ResponseEntity<Airport> updateAirport(@PathVariable String id, @Valid @Re
7183
if (updatedAirport != null) {
7284
return new ResponseEntity<>(updatedAirport, HttpStatus.OK);
7385
} else {
86+
log.error(DOCUMENT_NOT_FOUND);
7487
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7588
}
76-
} catch (DocumentNotFoundException e) {
89+
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
90+
log.error(DOCUMENT_NOT_FOUND, e);
7791
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7892
} catch (Exception e) {
93+
log.error(INTERNAL_SERVER_ERROR, e);
7994
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
8095
}
8196
}
@@ -87,8 +102,10 @@ public ResponseEntity<Void> deleteAirport(@PathVariable String id) {
87102
airportService.deleteAirport(id);
88103
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
89104
} catch (DocumentNotFoundException e) {
105+
log.error(DOCUMENT_NOT_FOUND, e);
90106
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
91107
} catch (Exception e) {
108+
log.error(INTERNAL_SERVER_ERROR, e);
92109
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
93110
}
94111
}
@@ -100,6 +117,7 @@ public ResponseEntity<Page<Airport>> listAirports(@RequestParam(defaultValue = "
100117
Page<Airport> airports = airportService.getAllAirports(PageRequest.of(page, size));
101118
return new ResponseEntity<>(airports, HttpStatus.OK);
102119
} catch (Exception e) {
120+
log.error(INTERNAL_SERVER_ERROR, e);
103121
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
104122
}
105123
}
@@ -113,6 +131,7 @@ public ResponseEntity<Page<Airport>> listDirectConnections(
113131
Page<Airport> airports = airportService.getDirectConnections(airport, PageRequest.of(page, size));
114132
return new ResponseEntity<>(airports, HttpStatus.OK);
115133
} catch (Exception e) {
134+
log.error(INTERNAL_SERVER_ERROR, e);
116135
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
117136
}
118137
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.couchbase.quickstart.springdata.models.Route;
88
import org.couchbase.quickstart.springdata.services.RouteService;
9+
import org.springframework.dao.DataRetrievalFailureException;
910
import org.springframework.data.domain.Page;
1011
import org.springframework.data.domain.PageRequest;
1112
import org.springframework.http.HttpStatus;
@@ -24,9 +25,11 @@
2425
import com.couchbase.client.core.error.DocumentNotFoundException;
2526

2627
import io.swagger.v3.oas.annotations.Operation;
28+
import lombok.extern.slf4j.Slf4j;
2729

2830
@RestController
2931
@RequestMapping("/api/v1/route")
32+
@Slf4j
3033
public class RouteController {
3134

3235
private final RouteService routeService;
@@ -35,6 +38,11 @@ public RouteController(RouteService routeService) {
3538
this.routeService = routeService;
3639
}
3740

41+
// All Errors
42+
private static final String INTERNAL_SERVER_ERROR = "Internal server error";
43+
private static final String DOCUMENT_NOT_FOUND = "Document not found";
44+
private static final String DOCUMENT_ALREADY_EXISTS = "Document already exists";
45+
3846
@Operation(summary = "Get a route by ID")
3947
@GetMapping("/{id}")
4048
public ResponseEntity<Route> getRoute(@PathVariable String id) {
@@ -43,8 +51,10 @@ public ResponseEntity<Route> getRoute(@PathVariable String id) {
4351
return route.map(value -> new ResponseEntity<>(value, HttpStatus.OK))
4452
.orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
4553
} catch (DocumentNotFoundException e) {
54+
log.error(DOCUMENT_NOT_FOUND, e);
4655
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
4756
} catch (Exception e) {
57+
log.error(INTERNAL_SERVER_ERROR, e);
4858
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
4959
}
5060
}
@@ -56,8 +66,10 @@ public ResponseEntity<Route> createRoute(@Valid @RequestBody Route route) {
5666
Route newRoute = routeService.createRoute(route);
5767
return new ResponseEntity<>(newRoute, HttpStatus.CREATED);
5868
} catch (DocumentExistsException e) {
69+
log.error(DOCUMENT_ALREADY_EXISTS, e);
5970
return new ResponseEntity<>(HttpStatus.CONFLICT);
6071
} catch (Exception e) {
72+
log.error(INTERNAL_SERVER_ERROR, e);
6173
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
6274
}
6375

@@ -71,11 +83,14 @@ public ResponseEntity<Route> updateRoute(@PathVariable String id, @Valid @Reques
7183
if (updatedRoute != null) {
7284
return new ResponseEntity<>(updatedRoute, HttpStatus.OK);
7385
} else {
86+
log.error(DOCUMENT_NOT_FOUND);
7487
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7588
}
7689
} catch (DocumentNotFoundException e) {
90+
log.error(DOCUMENT_NOT_FOUND, e);
7791
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
7892
} catch (Exception e) {
93+
log.error(INTERNAL_SERVER_ERROR, e);
7994
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
8095
}
8196
}
@@ -86,9 +101,11 @@ public ResponseEntity<Void> deleteRoute(@PathVariable String id) {
86101
try {
87102
routeService.deleteRoute(id);
88103
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
89-
} catch (DocumentNotFoundException e) {
104+
} catch (DocumentNotFoundException | DataRetrievalFailureException e) {
105+
log.error(DOCUMENT_NOT_FOUND, e);
90106
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
91107
} catch (Exception e) {
108+
log.error(INTERNAL_SERVER_ERROR, e);
92109
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
93110
}
94111
}
@@ -100,6 +117,7 @@ public ResponseEntity<Page<Route>> listRoutes(@RequestParam(defaultValue = "0")
100117
Page<Route> routes = routeService.getAllRoutes(PageRequest.of(page, size));
101118
return new ResponseEntity<>(routes, HttpStatus.OK);
102119
} catch (Exception e) {
120+
log.error(INTERNAL_SERVER_ERROR, e);
103121
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
104122
}
105123
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import org.springframework.data.couchbase.repository.Scope;
99
import org.springframework.data.domain.Page;
1010
import org.springframework.data.domain.Pageable;
11+
import org.springframework.stereotype.Repository;
1112

1213
import com.couchbase.client.java.query.QueryScanConsistency;
1314

1415
@Scope("inventory")
1516
@Collection("airline")
17+
@Repository
1618
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
1719
public interface AirlineRepository extends CouchbaseRepository<Airline, String> {
1820

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import org.springframework.data.couchbase.repository.Scope;
99
import org.springframework.data.domain.Page;
1010
import org.springframework.data.domain.Pageable;
11+
import org.springframework.stereotype.Repository;
1112

1213
import com.couchbase.client.java.query.QueryScanConsistency;
1314

1415
@Scope("inventory")
1516
@Collection("airport")
17+
@Repository
1618
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
1719
public interface AirportRepository extends CouchbaseRepository<Airport, String> {
1820

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import org.springframework.data.couchbase.repository.Scope;
1010
import org.springframework.data.domain.Page;
1111
import org.springframework.data.domain.Pageable;
12+
import org.springframework.stereotype.Repository;
1213

1314
import com.couchbase.client.java.query.QueryScanConsistency;
1415

1516
@Scope("inventory")
1617
@Collection("route")
18+
@Repository
1719
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
1820
public interface RouteRepository extends CouchbaseRepository<Route, String>, DynamicProxyable<RouteRepository> {
1921

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55

66
import org.couchbase.quickstart.springdata.models.Airline;
77
import org.couchbase.quickstart.springdata.repository.AirlineRepository;
8-
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.data.domain.Page;
109
import org.springframework.data.domain.Pageable;
1110
import org.springframework.stereotype.Service;
1211

1312
@Service
1413
public class AirlineService {
1514

16-
private AirlineRepository airlineRepository;
15+
private final AirlineRepository airlineRepository;
1716

1817
public AirlineService(AirlineRepository airlineRepository) {
1918
this.airlineRepository = airlineRepository;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@Service
1212
public class AirportService {
1313

14-
private AirportRepository airportRepository;
14+
private final AirportRepository airportRepository;
1515

1616
public AirportService(AirportRepository airportRepository) {
1717
this.airportRepository = airportRepository;

0 commit comments

Comments
 (0)