Skip to content

Commit ed117f1

Browse files
committed
initial cheapest flight search version
1 parent 8b8a4df commit ed117f1

File tree

9 files changed

+146
-54
lines changed

9 files changed

+146
-54
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@
9696
<artifactId>commons-lang3</artifactId>
9797
<version>3.10</version>
9898
</dependency>
99+
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
100+
<dependency>
101+
<groupId>com.google.guava</groupId>
102+
<artifactId>guava</artifactId>
103+
<version>29.0-jre</version>
104+
</dependency>
105+
99106
</dependencies>
100107
<build>
101108
<plugins>

src/main/java/com/miro/flightadvisor/controllers/CityController.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ public Optional<List<Comment>> comments() {
5858
return cityService.allCommentsForCity();
5959
}
6060

61-
@GetMapping("/routes")
62-
public Optional<List<Route>> getRoutes() {
63-
return cityService.allRoutes();
64-
}
65-
6661
@PostMapping("/cities/{cityId}/comments")
6762
@ResponseStatus(HttpStatus.CREATED)
6863
public ResponseEntity<String> addCommentForCity(@PathVariable("cityId") String cityId, @RequestBody @Valid CommentBean commentBean) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.miro.flightadvisor.controllers;
2+
3+
import com.miro.flightadvisor.beans.CommentBean;
4+
import com.miro.flightadvisor.entities.Comment;
5+
import com.miro.flightadvisor.entities.Route;
6+
import com.miro.flightadvisor.services.CityService;
7+
import com.miro.flightadvisor.services.RouteService;
8+
import com.sun.istack.NotNull;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.*;
13+
14+
import javax.validation.Valid;
15+
import java.util.List;
16+
import java.util.Optional;
17+
18+
@RestController
19+
public class FlightRouteController {
20+
21+
private RouteService routeService;
22+
23+
24+
@Autowired
25+
public FlightRouteController(RouteService routeService) {
26+
this.routeService = routeService;
27+
}
28+
29+
30+
@GetMapping("/routes/find-cheapest/{src}/{dest}")
31+
public Optional<?> getCity(@NotNull @PathVariable(value = "src") String src, @PathVariable(value = "dest") String dest) {
32+
return routeService.getCheapestFlight(src, dest);
33+
}
34+
35+
36+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.miro.flightadvisor.entities;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
import java.math.BigDecimal;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
@Data
11+
@NoArgsConstructor
12+
public class CheapestFlight {
13+
private BigDecimal cheapest;
14+
private BigDecimal total;
15+
private List<Route> routes = new ArrayList<>();
16+
17+
public CheapestFlight(BigDecimal cheapest, BigDecimal total, List<Route> routes) {
18+
this.cheapest = cheapest;
19+
this.total = total;
20+
this.routes = routes;
21+
}
22+
23+
24+
}

src/main/java/com/miro/flightadvisor/entities/Route.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Route {
4242
@Column(name = "stops", length = 1)
4343
private Integer stops;
4444

45-
@Column(name = "equipment", length = 3)
45+
@Column(name = "equipment", length = 24)
4646
private String equipment;
4747

4848
@Column(name = "flight_cost")

src/main/java/com/miro/flightadvisor/repositories/AirportRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77
import java.util.List;
88
import java.util.Optional;
99

10-
public interface AirportRepository extends JpaRepository<Airport, Integer> {
10+
public interface AirportRepository extends JpaRepository<Airport, Long> {
11+
12+
@Query("select a.providedAirportId from Airport a where a.city=:city")
13+
List<Integer> getOnlyAirportId(String city);
14+
1115
Optional<Airport> findByName(String name);
1216

17+
Optional<List<Airport>> findByCity(String city);
18+
1319
Optional<Airport> findByProvidedAirportId(Integer providedAirportId);
1420
}

src/main/java/com/miro/flightadvisor/services/CityService.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@ public CityService(CityRepository cityRepository, CommentRepository commentRepos
3737
this.routeRepository = routeRepository;
3838
}
3939

40-
public Optional<List<Route>> allRoutes() {
41-
return Optional.of(routeRepository.findAll());
42-
}
43-
4440
public void addCity(CityBean cityBean) {
45-
City city = new City();
46-
city.setCountry(cityBean.getCountry());
47-
city.setDescription(cityBean.getDescription());
48-
city.setName(cityBean.getName());
49-
this.cityRepository.save(city);
41+
City newCity = new City();
42+
newCity.setCountry(cityBean.getCountry());
43+
newCity.setDescription(cityBean.getDescription());
44+
newCity.setName(cityBean.getName());
45+
Optional<City> city = cityRepository.findByName(newCity.getName());
46+
if (city.isPresent()) {
47+
return;
48+
} else {
49+
this.cityRepository.save(newCity);
50+
}
5051
}
5152

5253
public Optional<List<City>> allCities() {
Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
package com.miro.flightadvisor.services;
22

3+
import ch.qos.logback.core.db.BindDataSourceToJNDIAction;
4+
import com.google.common.collect.ImmutableList;
5+
import com.google.common.collect.Lists;
36
import com.miro.flightadvisor.beans.AirportBean;
47
import com.miro.flightadvisor.beans.RouteBean;
58
import com.miro.flightadvisor.entities.Airport;
9+
import com.miro.flightadvisor.entities.CheapestFlight;
610
import com.miro.flightadvisor.entities.Route;
11+
import com.miro.flightadvisor.exception.FlightAdvisorRuntimeException;
712
import com.miro.flightadvisor.repositories.AirportRepository;
813
import com.miro.flightadvisor.repositories.RouteRepository;
914
import org.springframework.beans.factory.annotation.Autowired;
1015
import org.springframework.stereotype.Service;
16+
import sun.corba.Bridge;
1117

12-
import java.util.List;
13-
import java.util.Optional;
18+
import javax.persistence.criteria.CriteriaBuilder;
19+
import java.math.BigDecimal;
20+
import java.util.*;
21+
import java.util.stream.Collector;
22+
import java.util.stream.Collectors;
23+
import java.util.stream.IntStream;
24+
import java.util.stream.Stream;
1425

1526
@Service
1627
public class RouteService {
1728

1829
private final RouteRepository routeRepository;
30+
private final AirportRepository airportRepository;
1931

2032
@Autowired
21-
public RouteService(RouteRepository routeRepository) {
33+
public RouteService(RouteRepository routeRepository, AirportRepository airportRepository) {
2234
this.routeRepository = routeRepository;
35+
this.airportRepository = airportRepository;
2336
}
2437

25-
2638
public void saveRoute(RouteBean routeBean) {
2739
Integer sourceAirportId = routeBean.getSourceAirportId();
2840
Integer destinationAirportId = routeBean.getDestinationAirportId();
@@ -34,4 +46,49 @@ public void saveRoute(RouteBean routeBean) {
3446
routeRepository.save(newRoute);
3547
}
3648
}
49+
50+
public Optional<List<Route>> allRoutes() {
51+
return Optional.of(routeRepository.findAll());
52+
}
53+
54+
public Optional<CheapestFlight> getCheapestFlight(String src, String dest) {
55+
56+
List<Integer> sources = airportRepository.getOnlyAirportId(src);
57+
List<Integer> destinations = airportRepository.getOnlyAirportId(dest);
58+
return calculateAndReturnData(sources, destinations);
59+
}
60+
61+
public Optional<CheapestFlight> calculateAndReturnData(List<Integer> sources, List<Integer> destinations) {
62+
List<List<Integer>> airportIds = Lists.cartesianProduct(ImmutableList.of(
63+
sources,
64+
destinations));
65+
List<Route> routes = new ArrayList<>();
66+
airportIds.stream().forEachOrdered(a -> {
67+
Integer source = a.get(0);
68+
Integer destination = a.get(1);
69+
Optional<Route> route = routeRepository.findBySourceAirportIdAndDestinationAirportId(source, destination);
70+
if (route.isPresent()) {
71+
routes.add(route.get());
72+
}
73+
});
74+
75+
if (routes.size() > 0) {
76+
List<BigDecimal> totalCosts = new ArrayList<>();
77+
routes.stream().forEach(r -> {
78+
totalCosts.add(r.getFlightCost());
79+
});
80+
81+
BigDecimal total = totalCosts.stream().reduce((i, j) -> i.add(j)).get();
82+
BigDecimal cheapest = totalCosts.stream().reduce((i, j) -> i.min(j)).get();
83+
84+
CheapestFlight cheapestFlight = new CheapestFlight(cheapest, total, routes);
85+
return Optional.of(cheapestFlight);
86+
}
87+
BigDecimal total = new BigDecimal("0.0");
88+
BigDecimal cheapest = new BigDecimal("0.0");
89+
CheapestFlight cheapestFlight = new CheapestFlight(cheapest, total, routes);
90+
return Optional.of(cheapestFlight);
91+
92+
}
93+
3794
}

src/main/resources/data.sql

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)