Skip to content

Commit

Permalink
Whatever
Browse files Browse the repository at this point in the history
  • Loading branch information
pvepamb1 committed Sep 26, 2019
1 parent 7a37b80 commit de562db
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 32 deletions.
48 changes: 25 additions & 23 deletions components/flights/src/main/java/com/pcf/tripit/flights/Flight.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.pcf.tripit.flights;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
Expand All @@ -12,27 +11,30 @@ public class Flight implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String airlinesName;
private String flightNumber;
private String arrival;
private String departure;
private Date arrivalTime;
private Date departureTime;
private String to;
private String from;
private int cost;
boolean booked;
private int ticketsLeft;

public Flight() {
public Flight(String to) {

this.to = to;
}

public Flight(String airlinesName, String flightNumber, String arrival, String departure, int cost, boolean booked) {
public Flight(String airlinesName, String flightNumber, Date arrivalTime, Date departureTime, String to, int cost, int ticketsLeft) {
this.airlinesName = airlinesName;
this.flightNumber = flightNumber;
this.arrival = arrival;
this.departure = departure;
this.arrivalTime = arrivalTime;
this.departureTime = departureTime;
this.to = to;
this.cost = cost;
this.booked = booked;
this.ticketsLeft = ticketsLeft;
}

public long getId() {
Expand All @@ -59,20 +61,20 @@ public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}

public String getArrival() {
return arrival;
public Date getArrivalTime() {
return arrivalTime;
}

public void setArrival(String arrival) {
this.arrival = arrival;
public void setArrivalTime(Date arrivalTime) {
this.arrivalTime = arrivalTime;
}

public String getDeparture() {
return departure;
public Date getDepartureTime() {
return departureTime;
}

public void setDeparture(String departure) {
this.departure = departure;
public void setDepartureTime(Date departureTime) {
this.departureTime = departureTime;
}

public int getCost() {
Expand All @@ -83,11 +85,11 @@ public void setCost(int cost) {
this.cost = cost;
}

public boolean isBooked() {
return booked;
public int getTicketsLeft() {
return ticketsLeft;
}

public void setBooked(boolean booked) {
this.booked = booked;
public void setTicketsLeft(int ticketsLeft) {
this.ticketsLeft = ticketsLeft;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.pcf.tripit.flights;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/flights")
public class FlightController {
Expand All @@ -14,4 +22,27 @@ public FlightController(FlightRepository flightRepository) {
this.flightRepository = flightRepository;
}

Logger LOGGER = LoggerFactory.getLogger(FlightController.class);

@GetMapping("/filter")
public Iterable<Flight> getFlights(@RequestParam String to, @RequestParam String from, @RequestParam String date) throws ParseException {
Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
Iterable<Flight> flights = flightRepository.findByToAndFrom(to, from);
List<Flight> available = new ArrayList<>();
for (Flight flight:flights) {
if(flight.getDepartureTime().getDate()==date1.getDate() && flight.getTicketsLeft()!=0)
available.add(flight);
}
return available;
}

@GetMapping("/book")
public ResponseEntity<Flight> bookFlight(@RequestParam String name, @RequestParam String flightNumber, @RequestParam String from,
@RequestParam String to, @RequestParam String departureTime, @RequestParam String arrivalTime) throws ParseException {
LOGGER.info("In book");
Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(departureTime);
Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(arrivalTime);
return new ResponseEntity<>(HttpStatus.CREATED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
@Repository
public interface FlightRepository extends CrudRepository<Flight, Long> {

Iterable<Flight> findByToAndFromAndDate(String to, String from, String date);

Iterable<Flight> findByToAndFrom(String to, String from);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.pcf.tripit.hotels.bookings.BookingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.pcf.tripit.flightui;

public class Fcontroller {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.pcf.tripit.flightui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestOperations;

import java.util.ArrayList;
import java.util.List;

public class FlightClient {

private static ParameterizedTypeReference<List<FlightUI>> flightListType = new ParameterizedTypeReference<List<FlightUI>>() {
};
private static ParameterizedTypeReference<FlightUI> flightType = new ParameterizedTypeReference<FlightUI>() {
};
private String flightURL;
private RestOperations restOperations;
private static final int CACHE_SIZE = 5;
private final List<FlightUI> lastRead = new ArrayList<>(CACHE_SIZE);
private static final Logger log = LoggerFactory.getLogger(FlightClient.class);

public FlightClient(String flightURL, RestOperations restOperations) {
this.flightURL = flightURL;
this.restOperations = restOperations;
}

public void create(FlightUI hotel) {
restOperations.postForEntity(flightURL, hotel, FlightUI.class);
}

// @HystrixCommand(fallbackMethod="getAllFallback",commandProperties = {
// @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
//})
public List<FlightUI> getAll() {
List<FlightUI> read = restOperations.exchange(flightURL, HttpMethod.GET, null, flightListType).getBody();
log.debug("Read {} podcasts from {}", read.size(), flightURL);

lastRead.clear();
int copyCount = (read.size() < CACHE_SIZE) ? read.size() : CACHE_SIZE;
for (int i =0; i < copyCount; i++)
lastRead.add(read.get(i));
log.debug("Copied {} hotels into the cache", copyCount);

return read;
}

public List<FlightUI> getDates(String begin, String end){
String finalUrl = flightURL +"/filter?begin="+begin+"&end="+end;
return restOperations.exchange(finalUrl, HttpMethod.GET, null, flightListType).getBody();
}

public List<FlightUI> getAllFallback() {
log.debug("Returning {} hotels from the fallback method", lastRead.size());

return lastRead;
}

public FlightUI bookHotel(String name, String address, String city, String begin, String end){
String finalUrl = flightURL +"/book?name="+name+"&address="+address+"&city="+city+"&begin="+begin+"&end="+end;
FlightUI hotelUI = restOperations.exchange(finalUrl, HttpMethod.GET, null, flightType).getBody();
return hotelUI;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.pcf.tripit.flightui;

public class FlightInitialList {
}
83 changes: 83 additions & 0 deletions components/ui/src/main/java/com/pcf/tripit/flightui/FlightUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.pcf.tripit.flightui;

import java.util.Date;

public class FlightUI {

private String airlinesName;
private String flightNumber;
private String departureTime;
private String arrivalTime;
private String from;
private String to;
private int cost;

public FlightUI() {
}

public FlightUI(String airlinesName, String flightNumber, String departureTime, String arrivalTime, String from, String to, int cost) {
this.airlinesName = airlinesName;
this.flightNumber = flightNumber;
this.departureTime = departureTime;
this.arrivalTime = arrivalTime;
this.from = from;
this.to = to;
this.cost = cost;
}

public String getAirlinesName() {
return airlinesName;
}

public void setAirlinesName(String airlinesName) {
this.airlinesName = airlinesName;
}

public String getFlightNumber() {
return flightNumber;
}

public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}

public String getDepartureTime() {
return departureTime;
}

public void setDepartureTime(String departureTime) {
this.departureTime = departureTime;
}

public String getArrivalTime() {
return arrivalTime;
}

public void setArrivalTime(String arrivalTime) {
this.arrivalTime = arrivalTime;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public int getCost() {
return cost;
}

public void setCost(int cost) {
this.cost = cost;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,4 @@ public HotelUI bookHotel(String name, String address, String city, String begin,
return hotelUI;
}

//upcoming enhancement

/* public HotelUI bookHotel2(String name, String address, String city, String begin, String end){
String finalUrl = hotelURL+"/book";
HotelUI hotelUI = restOperations.exchange(finalUrl, HttpMethod.POST, new HotelUI(name, address, city), hotelType).getBody();
return hotelUI;
}*/
}

0 comments on commit de562db

Please sign in to comment.