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

All Working Fine

parent c44eabf8
......@@ -12,4 +12,9 @@
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="DataBase" options="" />
</option>
</component>
</project>
\ No newline at end of file
......@@ -7,6 +7,14 @@
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</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>
<artifactId>DataBase</artifactId>
......@@ -23,6 +31,11 @@
<version>6.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
......
......@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Date;
import java.util.Map;
@SpringBootApplication
public class DataBaseApp
......@@ -22,13 +23,19 @@ public class DataBaseApp
FlightDB.addFlight(flight1);
FlightDB.addFlight(flight2);
FlightDB.addFlight(flight3);
User user1 = new User("1" , "Salameh" , flight3);
User user1 = new User("1" , "Salameh");
user1.addFlight(flight2);
User user2 = new User("2" , "Einstein" , flight2);
user2.addFlight(flight1);
User user3 = new User("3" , "Gambler " , flight1);
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(user2);
UserDB.addUser(user3);
......
......@@ -8,6 +8,7 @@ public class Flight
private Date date;
private String flightID;
public Flight () {}
public Flight(String destination, Date date, String flightID) {
this.destination = destination;
this.date = date;
......@@ -29,4 +30,20 @@ public class Flight
{
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;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/flights-db")
public class FlightDB
{
public static Map<String , Flight> flightsDB;
public static Map<String , Flight> flightsDB = new HashMap<>();
public static void addFlight(Flight flight)
{
String id = flight.getFlightID();
......
......@@ -7,31 +7,56 @@ public class User
{
private String userID;
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.name = name;
flights.add(flight.getFlightID());
FlightDB.addFlight(flight);
flights = new ArrayList<>();
}
public void addFlight(Flight flight)
{
flights.add(flight.getFlightID());
FlightDB.addFlight(flight);
}
public String getUserID() {
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;
}
public String getName() {
return name;
public void setFlights(List<String> flights) {
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;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/users-db")
public class UserDB
{
public static Map<String , User> usersDB;
public static Map<String , User> usersDB = new HashMap<>();
public static void addUser(User user)
{
String id = user.getUserID();
......@@ -21,6 +22,7 @@ public class UserDB
@RequestMapping("/{userID}")
public static User getUser(@PathVariable("userID")String userID)
{
System.out.println(usersDB.get(userID));
if (usersDB.containsKey(userID)) return usersDB.get(userID);
return null;
}
......
server.port=8081
\ No newline at end of file
......@@ -27,6 +27,11 @@
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>WeatherService</artifactId>
......
......@@ -10,20 +10,21 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.wheatherservice.Weather;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/flights")
@RequestMapping("/users")
public class UserService
{
@Autowired
RestTemplate restTemplate;
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 WeatherURL = "http://localhost:8083/weather/";
@RequestMapping("/{userID}")
public List<Flight> getFlights(@PathVariable("userID") String userID)
{
......@@ -31,7 +32,9 @@ public class UserService
if (user == null) return new ArrayList<>();
System.out.println("Welcome : " + user.getName());
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);
ans.add(flight);
......@@ -43,6 +46,7 @@ public class UserService
@RequestMapping("/weather/{flightID}")
public Weather getWeather(@PathVariable("flightID") String flightID)
{
System.out.println("Getting Weather Req");
Flight flight = restTemplate.getForObject(FlightDBURL+flightID , Flight.class);
if (flight.getDestination().isEmpty() ) return new Weather();
......@@ -51,6 +55,6 @@ public class UserService
System.out.println(weather);
return null;
return weather;
}
}
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.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
@SpringBootApplication
public class UserServiceApp
{
......
server.port=8081
\ No newline at end of file
server.port=8082
\ No newline at end of file
......@@ -27,6 +27,26 @@
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</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>
<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;
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
{
/**
*
*
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 weatherDescription;
String Country;
double temperature;
double humidity;
double windSpeed;
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
this.cityName = cityName;
}
public String getWeatherDescription() {
return weatherDescription;
public String getCountry() {
return Country;
}
public void setWeatherDescription(String weatherDescription) {
this.weatherDescription = weatherDescription;
public void setCountry(String country) {
Country = country;
}
public double getTemperature() {
......@@ -63,22 +72,6 @@ public class Weather
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
public String toString()
{
......@@ -86,11 +79,9 @@ public class Weather
{
return "No Information to be Printed";
}
String ans = "Forecast in " + cityName+ " : \n";
ans += "Temperature = " + temperature + " degree\n";
ans += "Wind Speed = " + windSpeed + " km/h\n";
ans += "Humidity : " + humidity + "\n";
ans += "Further More : " + weatherDescription;
String ans = "Location Name: " + cityName+"\n" +
"Temperature in Celsius: " + temperature + "\n" +
"Country: " + Country+"\n";
return ans;
}
......
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.FlightDB;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -8,6 +16,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.net.http.HttpClient;
@RestController
@RequestMapping("/weather")
......@@ -15,21 +26,23 @@ public class WeatherService
{
@Autowired
RestTemplate restTemplate;
String FlightDBURL = "http://localhost:8081/flights-db/";
String myAPIKey = "d5e468d45e234c749b9123443230105";
String myAPIKey = "57afea8fc6e142f29f2115632242604";
String prefURL = "http://api.weatherapi.com/v1/current.json?key="+myAPIKey + "&q=";
String sufURL = "&aqi=yes";
@RequestMapping("/{city}")
public Weather getWeather(@PathVariable("city") String city)
{
public Weather getWeather(@PathVariable("city") String city) throws IOException {
if (city.isEmpty())
{
return new Weather();
}
String url = prefURL + city + sufURL;
Weather weather = restTemplate.getForObject(url , Weather.class);
Weather weather = new Weather();
try
{
weather = new Weather(prefURL + city + sufURL);
} catch (JSONException e)
{
//log
}
return weather;
}
}
server.port=8082
\ No newline at end of file
server.port=8083
\ 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