diff --git a/src/main/java/edu/hackathon/controller/AnalyticsController.java b/src/main/java/edu/hackathon/controller/AnalyticsController.java index 4a48b37..fcd67b2 100644 --- a/src/main/java/edu/hackathon/controller/AnalyticsController.java +++ b/src/main/java/edu/hackathon/controller/AnalyticsController.java @@ -3,7 +3,10 @@ */ package edu.hackathon.controller; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; @@ -17,7 +20,10 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; +import edu.hackathon.rest.domain.AnalyticsWrapper; import edu.hackathon.rest.domain.BookingAnalytics; +import edu.hackathon.rest.domain.Country; +import edu.hackathon.rest.domain.CountryAnalytics; import edu.hackathon.service.AnalyticsService; /** @@ -46,10 +52,35 @@ public String healthcheck() { */ @RequestMapping("/forecast/{from}/{to}/{type}") @ResponseBody - public HttpEntity> bookingForecast(@PathVariable String from, @PathVariable String to, + public HttpEntity bookingForecast(@PathVariable String from, @PathVariable String to, @PathVariable String type) { - return new ResponseEntity<>( - analyticsService.forecastBookingCost(getDateFromString(from), getDateFromString(to)), HttpStatus.OK); + + AnalyticsWrapper analyticsWrapper = new AnalyticsWrapper(); + List bookingAnalyticsList = analyticsService.forecastBookingCost(getDateFromString(from), getDateFromString(to)); + + Map countryDetailsMap = new HashMap<>(); + for (BookingAnalytics bookingAnalytics : bookingAnalyticsList) { + if (bookingAnalytics.getCountries() != null) { + for (Country country : bookingAnalytics.getCountries()) { + if (countryDetailsMap.get(country.getCode()) != null) { + CountryAnalytics countryAnalytics = countryDetailsMap.get(country.getCode()); + countryAnalytics.setCode(country.getCode()); + countryAnalytics.setName(country.getName()); + countryAnalytics.updateNewDetails(country); + countryDetailsMap.put(country.getCode(), countryAnalytics); + } else { + CountryAnalytics countryAnalytics = new CountryAnalytics(country.getAncillaryCount(), + country.getBookingCount(), country.getAncillaryPrice(), country.getBookingPrice(), country.getCode(), country.getName()); + countryDetailsMap.put(country.getCode(), countryAnalytics); + } + + } + } + } + + analyticsWrapper.setBookingAnalyticsList(bookingAnalyticsList); + analyticsWrapper.setCountryAnalyticsList(new ArrayList<>(countryDetailsMap.values()));//.toArray()); + return new ResponseEntity<>(analyticsWrapper, HttpStatus.OK); } /** diff --git a/src/main/java/edu/hackathon/rest/domain/AbstractAnalyticsItem.java b/src/main/java/edu/hackathon/rest/domain/AbstractAnalyticsItem.java index 89891ff..50a4ac2 100644 --- a/src/main/java/edu/hackathon/rest/domain/AbstractAnalyticsItem.java +++ b/src/main/java/edu/hackathon/rest/domain/AbstractAnalyticsItem.java @@ -48,6 +48,18 @@ public void setAncillaryCount(BigInteger ancillaryCount) { this.ancillaryCount = ancillaryCount; } + public void addAncillaryCount(BigInteger ancillaryCount) { + this.ancillaryCount.add(ancillaryCount); + } + + public void updateBookingPrice(Price bookingPrice) { + this.bookingPrice.setAmount(this.bookingPrice.getAmount() + bookingPrice.getAmount()); + } + + public void updateAncillaryPrice(Price ancillaryPrice) { + this.ancillaryPrice.setAmount(this.ancillaryPrice.getAmount() + ancillaryPrice.getAmount()); + } + public Price getAncillaryPrice() { return ancillaryPrice; } diff --git a/src/main/java/edu/hackathon/rest/domain/AnalyticsWrapper.java b/src/main/java/edu/hackathon/rest/domain/AnalyticsWrapper.java new file mode 100644 index 0000000..9715d3c --- /dev/null +++ b/src/main/java/edu/hackathon/rest/domain/AnalyticsWrapper.java @@ -0,0 +1,40 @@ +package edu.hackathon.rest.domain; + +import java.util.List; + +public class AnalyticsWrapper { + + private List bookingAnalyticsList; + + private List countryAnalyticsList; + + /** + * @return the bookingAnalyticsList + */ + public List getBookingAnalyticsList() { + return bookingAnalyticsList; + } + + /** + * @param bookingAnalyticsList the bookingAnalyticsList to set + */ + public void setBookingAnalyticsList(List bookingAnalyticsList) { + this.bookingAnalyticsList = bookingAnalyticsList; + } + + /** + * @return the countryAnalyticsList + */ + public List getCountryAnalyticsList() { + return countryAnalyticsList; + } + + /** + * @param countryAnalyticsList the countryAnalyticsList to set + */ + public void setCountryAnalyticsList(List countryAnalyticsList) { + this.countryAnalyticsList = countryAnalyticsList; + } + + +} diff --git a/src/main/java/edu/hackathon/rest/domain/CountryAnalytics.java b/src/main/java/edu/hackathon/rest/domain/CountryAnalytics.java new file mode 100644 index 0000000..f1b27d8 --- /dev/null +++ b/src/main/java/edu/hackathon/rest/domain/CountryAnalytics.java @@ -0,0 +1,47 @@ +package edu.hackathon.rest.domain; + +import java.math.BigInteger; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class CountryAnalytics extends AbstractAnalyticsItem { + + @JsonProperty + private String name; + + @JsonProperty + private String code; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public CountryAnalytics(BigInteger ancillaryCount, BigInteger bookingCount, Price ancillaryPrice, Price bookingPrice, String code, String name) { + this.setAncillaryCount(ancillaryCount); + this.setBookingCount(bookingCount); + this.setBookingPrice(bookingPrice); + this.setAncillaryPrice(ancillaryPrice); + this.code = code; + this.name = name; + } + + public void updateNewDetails(Country country) { + this.addBookingCount(country.getBookingCount()); + this.addAncillaryCount(country.getAncillaryCount()); + this.updateAncillaryPrice(country.getAncillaryPrice()); + this.updateBookingPrice(country.getBookingPrice()); + } + +}