Commit 36254668 authored by mohammad.salama's avatar mohammad.salama

All Working Fine

parent c44eabf8
...@@ -12,4 +12,9 @@ ...@@ -12,4 +12,9 @@
</profile> </profile>
</annotationProcessing> </annotationProcessing>
</component> </component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="DataBase" options="" />
</option>
</component>
</project> </project>
\ No newline at end of file
...@@ -7,6 +7,14 @@ ...@@ -7,6 +7,14 @@
<groupId>org.example</groupId> <groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>
<!--<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/>
</parent-->
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>DataBase</artifactId> <artifactId>DataBase</artifactId>
...@@ -23,6 +31,11 @@ ...@@ -23,6 +31,11 @@
<version>6.1.1</version> <version>6.1.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
......
...@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication; ...@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Date; import java.util.Date;
import java.util.Map;
@SpringBootApplication @SpringBootApplication
public class DataBaseApp public class DataBaseApp
...@@ -22,13 +23,19 @@ public class DataBaseApp ...@@ -22,13 +23,19 @@ public class DataBaseApp
FlightDB.addFlight(flight1); FlightDB.addFlight(flight1);
FlightDB.addFlight(flight2); FlightDB.addFlight(flight2);
FlightDB.addFlight(flight3); FlightDB.addFlight(flight3);
User user1 = new User("1" , "Salameh" , flight3);
User user1 = new User("1" , "Salameh");
user1.addFlight(flight2); user1.addFlight(flight2);
User user2 = new User("2" , "Einstein" , flight2);
user2.addFlight(flight1);
User user3 = new User("3" , "Gambler " , flight1);
user1.addFlight(flight3); user1.addFlight(flight3);
User user2 = new User("2" , "Einstein");
user2.addFlight(flight1);
user2.addFlight(flight2);
User user3 = new User("3" , "Gambler");
user3.addFlight(flight1);
user3.addFlight(flight3);
UserDB.addUser(user1); UserDB.addUser(user1);
UserDB.addUser(user2); UserDB.addUser(user2);
UserDB.addUser(user3); UserDB.addUser(user3);
......
...@@ -8,6 +8,7 @@ public class Flight ...@@ -8,6 +8,7 @@ public class Flight
private Date date; private Date date;
private String flightID; private String flightID;
public Flight () {}
public Flight(String destination, Date date, String flightID) { public Flight(String destination, Date date, String flightID) {
this.destination = destination; this.destination = destination;
this.date = date; this.date = date;
...@@ -29,4 +30,20 @@ public class Flight ...@@ -29,4 +30,20 @@ public class Flight
{ {
return "Flight ID : " + flightID + "\nDestination is : " + destination + "\nDate: "+date; return "Flight ID : " + flightID + "\nDestination is : " + destination + "\nDate: "+date;
} }
public void setDestination(String destination) {
this.destination = destination;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public void setFlightID(String flightID) {
this.flightID = flightID;
}
} }
...@@ -5,13 +5,14 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -5,13 +5,14 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
@RestController @RestController
@RequestMapping("/flights-db") @RequestMapping("/flights-db")
public class FlightDB public class FlightDB
{ {
public static Map<String , Flight> flightsDB; public static Map<String , Flight> flightsDB = new HashMap<>();
public static void addFlight(Flight flight) public static void addFlight(Flight flight)
{ {
String id = flight.getFlightID(); String id = flight.getFlightID();
......
...@@ -7,31 +7,56 @@ public class User ...@@ -7,31 +7,56 @@ public class User
{ {
private String userID; private String userID;
private String name; private String name;
private List<String> flights = new ArrayList<>(); private List<String> flights;
public User(String userID, String name, Flight flight) public User () {}
public User(String userID, String name)
{ {
this.userID = userID; this.userID = userID;
this.name = name; this.name = name;
flights.add(flight.getFlightID()); flights = new ArrayList<>();
FlightDB.addFlight(flight);
} }
public void addFlight(Flight flight) public void addFlight(Flight flight)
{ {
flights.add(flight.getFlightID()); flights.add(flight.getFlightID());
FlightDB.addFlight(flight);
} }
public String getUserID() { public String getUserID() {
return userID; return userID;
} }
public List<String> getFlightsIDs() public String getName() {
{ return name;
}
public void setUserID(String userID) {
this.userID = userID;
}
public void setName(String name) {
this.name = name;
}
public List<String> getFlights() {
return flights; return flights;
} }
public String getName() { public void setFlights(List<String> flights) {
return name; this.flights = flights;
} }
@Override
public String toString()
{
String ans = "Name : "+name+"\n";
ans += "ID : " + userID + "\n";
for (String f:flights)
{
ans += FlightDB.getFlight(f);
ans+="\n";
}
return ans;
}
} }
...@@ -4,13 +4,14 @@ import org.springframework.web.bind.annotation.PathVariable; ...@@ -4,13 +4,14 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
@RestController @RestController
@RequestMapping("/users-db") @RequestMapping("/users-db")
public class UserDB public class UserDB
{ {
public static Map<String , User> usersDB; public static Map<String , User> usersDB = new HashMap<>();
public static void addUser(User user) public static void addUser(User user)
{ {
String id = user.getUserID(); String id = user.getUserID();
...@@ -21,6 +22,7 @@ public class UserDB ...@@ -21,6 +22,7 @@ public class UserDB
@RequestMapping("/{userID}") @RequestMapping("/{userID}")
public static User getUser(@PathVariable("userID")String userID) public static User getUser(@PathVariable("userID")String userID)
{ {
System.out.println(usersDB.get(userID));
if (usersDB.containsKey(userID)) return usersDB.get(userID); if (usersDB.containsKey(userID)) return usersDB.get(userID);
return null; return null;
} }
......
server.port=8081
\ No newline at end of file
...@@ -27,6 +27,11 @@ ...@@ -27,6 +27,11 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>WeatherService</artifactId> <artifactId>WeatherService</artifactId>
......
...@@ -10,20 +10,21 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -10,20 +10,21 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.wheatherservice.Weather; import org.wheatherservice.Weather;
import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/flights") @RequestMapping("/users")
public class UserService public class UserService
{ {
@Autowired @Autowired
RestTemplate restTemplate; RestTemplate restTemplate;
String UserDBURL = "http://localhost:8081/users-db/"; String UserDBURL = "http://localhost:8081/users-db/";
String muURL1 = "http://localhost:8082/users/{user id}";
String muURL2 = "http://localhost:8082/users/weather/{flight id}";
String FlightDBURL = "http://localhost:8081/flights-db/"; String FlightDBURL = "http://localhost:8081/flights-db/";
String WeatherURL = "http://localhost:8083/weather/"; String WeatherURL = "http://localhost:8083/weather/";
@RequestMapping("/{userID}") @RequestMapping("/{userID}")
public List<Flight> getFlights(@PathVariable("userID") String userID) public List<Flight> getFlights(@PathVariable("userID") String userID)
{ {
...@@ -31,7 +32,9 @@ public class UserService ...@@ -31,7 +32,9 @@ public class UserService
if (user == null) return new ArrayList<>(); if (user == null) return new ArrayList<>();
System.out.println("Welcome : " + user.getName()); System.out.println("Welcome : " + user.getName());
List<Flight> ans = new ArrayList<>(); List<Flight> ans = new ArrayList<>();
for (String flightID : user.getFlightsIDs()) System.out.println("Ans size is " + user.getFlights().size());
for (String flightID : user.getFlights())
{ {
Flight flight = restTemplate.getForObject(FlightDBURL+flightID , Flight.class); Flight flight = restTemplate.getForObject(FlightDBURL+flightID , Flight.class);
ans.add(flight); ans.add(flight);
...@@ -43,6 +46,7 @@ public class UserService ...@@ -43,6 +46,7 @@ public class UserService
@RequestMapping("/weather/{flightID}") @RequestMapping("/weather/{flightID}")
public Weather getWeather(@PathVariable("flightID") String flightID) public Weather getWeather(@PathVariable("flightID") String flightID)
{ {
System.out.println("Getting Weather Req");
Flight flight = restTemplate.getForObject(FlightDBURL+flightID , Flight.class); Flight flight = restTemplate.getForObject(FlightDBURL+flightID , Flight.class);
if (flight.getDestination().isEmpty() ) return new Weather(); if (flight.getDestination().isEmpty() ) return new Weather();
...@@ -51,6 +55,6 @@ public class UserService ...@@ -51,6 +55,6 @@ public class UserService
System.out.println(weather); System.out.println(weather);
return null; return weather;
} }
} }
package org.userservice; package org.userservice;
import org.database.Flight;
import org.database.FlightDB;
import org.database.User;
import org.database.UserDB;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.Date;
@SpringBootApplication @SpringBootApplication
public class UserServiceApp public class UserServiceApp
{ {
......
server.port=8081 server.port=8082
\ No newline at end of file \ No newline at end of file
...@@ -27,6 +27,26 @@ ...@@ -27,6 +27,26 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>com.twitter</groupId>
<artifactId>parquet-hadoop-bundle</artifactId>
<version>1.6.0rc3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.1</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
......
package org.wheatherservice;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import parquet.org.codehaus.jackson.JsonNode;
import parquet.org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
public class TestExternalAPI
{
public static void main(String[] args) throws IOException, JSONException {
String myAPIKey = "57afea8fc6e142f29f2115632242604";
String prefURL = "http://api.weatherapi.com/v1/current.json?key="+myAPIKey + "&q=";
String sufURL = "&aqi=yes";
String url = prefURL + "London" + sufURL;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String jsonResponse = EntityUtils.toString(entity);
JSONObject json = new JSONObject(jsonResponse);
// Extract location details
JSONObject location = json.getJSONObject("location");
String locationName = location.getString("name");
String country = location.getString("country");
// Extract current temperature in Celsius
JSONObject current = json.getJSONObject("current");
double tempC = current.getDouble("temp_c");
// Print the extracted values
System.out.println("Location Name: " + locationName);
System.out.println("Temperature in Celsius: " + tempC);
System.out.println("Country: " + country);
}
}
package org.wheatherservice; package org.wheatherservice;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.io.IOException;
public class Weather public class Weather
{ {
/**
*
*
ErrorWarning.Text = forecastInformation.CityName;
WeatherDescription.Text += forecastInformation.WeatherDescription;
Temperature.Text += forecastInformation.Temperature.ToString()+ " Celecius";
Humidity.Text += forecastInformation.Humidity.ToString() + "%";
WindSpeed.Text += forecastInformation.WindSpeed.ToString() + "km per hour";
string myAPIKey = "d5e468d45e234c749b9123443230105";
string url = "http://api.weatherapi.com/v1/current.json?key=" + myAPIKey + "&q=" + cityName + "&aqi=yes";
var response = JsonConvert.DeserializeObject<dynamic>(result);
var forecastInformation = new ForecastInformation
{
CityName = response.location.name,
WeatherDescription = response.current.condition.text,
Temperature = response.current.temp_c,
Humidity = response.current.humidity,
WindSpeed = response.current.wind_kph
};
return forecastInformation;
* */
String cityName; String cityName;
String weatherDescription; String Country;
double temperature; double temperature;
double humidity;
double windSpeed;
public Weather() public Weather()
{ {
cityName = "";
weatherDescription = ""; }
temperature = humidity = windSpeed = 0.0;
public Weather(String url) throws IOException, JSONException
{
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String jsonResponse = EntityUtils.toString(entity);
JSONObject json = new JSONObject(jsonResponse);
JSONObject location = json.getJSONObject("location");
String locationName = location.getString("name");
String country = location.getString("country");
JSONObject current = json.getJSONObject("current");
double tempC = current.getDouble("temp_c");
this.cityName = locationName;
this.Country = country;
this.temperature = tempC;
} }
...@@ -47,12 +56,12 @@ public class Weather ...@@ -47,12 +56,12 @@ public class Weather
this.cityName = cityName; this.cityName = cityName;
} }
public String getWeatherDescription() { public String getCountry() {
return weatherDescription; return Country;
} }
public void setWeatherDescription(String weatherDescription) { public void setCountry(String country) {
this.weatherDescription = weatherDescription; Country = country;
} }
public double getTemperature() { public double getTemperature() {
...@@ -63,22 +72,6 @@ public class Weather ...@@ -63,22 +72,6 @@ public class Weather
this.temperature = temperature; this.temperature = temperature;
} }
public double getHumidity() {
return humidity;
}
public void setHumidity(double humidity) {
this.humidity = humidity;
}
public double getWindSpeed() {
return windSpeed;
}
public void setWindSpeed(double windSpeed) {
this.windSpeed = windSpeed;
}
@Override @Override
public String toString() public String toString()
{ {
...@@ -86,11 +79,9 @@ public class Weather ...@@ -86,11 +79,9 @@ public class Weather
{ {
return "No Information to be Printed"; return "No Information to be Printed";
} }
String ans = "Forecast in " + cityName+ " : \n"; String ans = "Location Name: " + cityName+"\n" +
ans += "Temperature = " + temperature + " degree\n"; "Temperature in Celsius: " + temperature + "\n" +
ans += "Wind Speed = " + windSpeed + " km/h\n"; "Country: " + Country+"\n";
ans += "Humidity : " + humidity + "\n";
ans += "Further More : " + weatherDescription;
return ans; return ans;
} }
......
package org.wheatherservice; package org.wheatherservice;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.codehaus.jettison.json.JSONException;
import org.database.Flight; import org.database.Flight;
import org.database.FlightDB; import org.database.FlightDB;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -8,6 +16,9 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -8,6 +16,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.net.http.HttpClient;
@RestController @RestController
@RequestMapping("/weather") @RequestMapping("/weather")
...@@ -15,21 +26,23 @@ public class WeatherService ...@@ -15,21 +26,23 @@ public class WeatherService
{ {
@Autowired @Autowired
RestTemplate restTemplate; RestTemplate restTemplate;
String myAPIKey = "57afea8fc6e142f29f2115632242604";
String FlightDBURL = "http://localhost:8081/flights-db/";
String myAPIKey = "d5e468d45e234c749b9123443230105";
String prefURL = "http://api.weatherapi.com/v1/current.json?key="+myAPIKey + "&q="; String prefURL = "http://api.weatherapi.com/v1/current.json?key="+myAPIKey + "&q=";
String sufURL = "&aqi=yes"; String sufURL = "&aqi=yes";
@RequestMapping("/{city}") @RequestMapping("/{city}")
public Weather getWeather(@PathVariable("city") String city) public Weather getWeather(@PathVariable("city") String city) throws IOException {
{
if (city.isEmpty()) if (city.isEmpty())
{ {
return new Weather(); return new Weather();
} }
Weather weather = new Weather();
String url = prefURL + city + sufURL; try
Weather weather = restTemplate.getForObject(url , Weather.class); {
weather = new Weather(prefURL + city + sufURL);
} catch (JSONException e)
{
//log
}
return weather; return weather;
} }
} }
server.port=8082 server.port=8083
\ No newline at end of file \ No newline at end of file
myAPIKey = "57afea8fc6e142f29f2115632242604";
prefURL = "http://api.weatherapi.com/v1/current.json?key="+myAPIKey + "&q="; //city
sufURL = "&days=1&aqi=no&alerts=no";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment