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

Weather Added , DB logic is isolatedm new URLs Created

parent d09c6d47
...@@ -10,6 +10,26 @@ ...@@ -10,6 +10,26 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>DataBase</artifactId> <artifactId>DataBase</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
......
package org.database;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Date;
@SpringBootApplication
public class DataBaseApp
{
public static void main(String[] args)
{
init();
SpringApplication.run(DataBaseApp.class, args);
}
public static void init()
{
Date date = new Date();
Flight flight1 = new Flight("Las Vegas" , date , "LA21");
Flight flight2 = new Flight("Tartous" , date , "SY11");
Flight flight3 = new Flight("Venice" , date , "VEN584");
FlightDB.addFlight(flight1);
FlightDB.addFlight(flight2);
FlightDB.addFlight(flight3);
User user1 = new User("1" , "Salameh" , flight3);
user1.addFlight(flight2);
User user2 = new User("2" , "Einstein" , flight2);
user2.addFlight(flight1);
User user3 = new User("3" , "Gambler " , flight1);
user1.addFlight(flight3);
UserDB.addUser(user1);
UserDB.addUser(user2);
UserDB.addUser(user3);
}
}
...@@ -18,4 +18,15 @@ public class Flight ...@@ -18,4 +18,15 @@ public class Flight
{ {
return flightID; return flightID;
} }
public String getDestination()
{
return destination;
}
@Override
public String toString()
{
return "Flight ID : " + flightID + "\nDestination is : " + destination + "\nDate: "+date;
}
} }
package org.database; package org.database;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.Map; import java.util.Map;
@RestController
@RequestMapping("/flights-db")
public class FlightDB public class FlightDB
{ {
public static Map<String , Flight> flightsDB; public static Map<String , Flight> flightsDB;
...@@ -12,9 +19,10 @@ public class FlightDB ...@@ -12,9 +19,10 @@ public class FlightDB
flightsDB.put(id , flight); flightsDB.put(id , flight);
} }
public static Flight getFlight(String flightID) @RequestMapping("/{flightID}")
public static Flight getFlight(@PathVariable("flightID") String flightID)
{ {
if (flightsDB.containsKey(flightID)) return flightsDB.get(flightID); if (flightsDB.containsKey(flightID)) return flightsDB.get(flightID);
return null; return new Flight("",new Date(),"");
} }
} }
...@@ -16,18 +16,22 @@ public class User ...@@ -16,18 +16,22 @@ public class User
flights.add(flight.getFlightID()); flights.add(flight.getFlightID());
FlightDB.addFlight(flight); FlightDB.addFlight(flight);
} }
public void addFlight(Flight flight)
{
flights.add(flight.getFlightID());
FlightDB.addFlight(flight);
}
public String getUserID() { public String getUserID() {
return userID; return userID;
} }
public List<Flight> getFlights() public List<String> getFlightsIDs()
{ {
List<Flight> ans = new ArrayList<>(); return flights;
for (String id : flights) }
{
ans.add(FlightDB.getFlight(id)); public String getName() {
} return name;
return ans;
} }
} }
package org.database; package org.database;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map; import java.util.Map;
@RestController
@RequestMapping("/users-db")
public class UserDB public class UserDB
{ {
public static Map<String , User> usersDB; public static Map<String , User> usersDB;
...@@ -11,7 +17,9 @@ public class UserDB ...@@ -11,7 +17,9 @@ public class UserDB
if (usersDB.containsKey(id)) return; if (usersDB.containsKey(id)) return;
usersDB.put(id , user); usersDB.put(id , user);
} }
public static User getUser(String userID)
@RequestMapping("/{userID}")
public static User getUser(@PathVariable("userID")String 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,12 @@ ...@@ -27,6 +27,12 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>WeatherService</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
......
package org.userservice; package org.userservice;
import org.database.Flight;
import org.database.User;
import org.database.UserDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; 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 org.springframework.web.client.RestTemplate;
import org.wheatherservice.Weather;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -12,11 +19,38 @@ import java.util.List; ...@@ -12,11 +19,38 @@ import java.util.List;
@RequestMapping("/flights") @RequestMapping("/flights")
public class UserService public class UserService
{ {
@Autowired
RestTemplate restTemplate;
String UserDBURL = "http://localhost:8081/users-db/";
String FlightDBURL = "http://localhost:8081/flights-db/";
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)
{ {
User user = UserDB.getUser(userID); User user = restTemplate.getForObject(UserDBURL+userID , User.class);
if (user == null) return null; if (user == null) return new ArrayList<>();
return user.getFlights(); System.out.println("Welcome : " + user.getName());
List<Flight> ans = new ArrayList<>();
for (String flightID : user.getFlightsIDs())
{
Flight flight = restTemplate.getForObject(FlightDBURL+flightID , Flight.class);
ans.add(flight);
}
System.out.println(ans);
return ans;
}
@RequestMapping("/weather/{flightID}")
public Weather getWeather(@PathVariable("flightID") String flightID)
{
Flight flight = restTemplate.getForObject(FlightDBURL+flightID , Flight.class);
if (flight.getDestination().isEmpty() ) return new Weather();
String city = flight.getDestination();
Weather weather = restTemplate.getForObject(WeatherURL+city , Weather.class);
System.out.println(weather);
return null;
} }
} }
...@@ -6,31 +6,22 @@ import org.database.User; ...@@ -6,31 +6,22 @@ import org.database.User;
import org.database.UserDB; 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.web.client.RestTemplate;
import java.util.Date; import java.util.Date;
@SpringBootApplication @SpringBootApplication
public class UserServiceApp public class UserServiceApp
{ {
public static void main(String[] args) public static void main(String[] args)
{ {
init();
SpringApplication.run(UserServiceApp.class, args); SpringApplication.run(UserServiceApp.class, args);
} }
public static void init() @Bean
public RestTemplate getRestTemplate()
{ {
Date date = new Date(); return new RestTemplate();
Flight flight1 = new Flight("Las Vegas" , date , "LA21");
Flight flight2 = new Flight("Tartous" , date , "SY11");
Flight flight3 = new Flight("Venice" , date , "VEN584");
FlightDB.addFlight(flight1);
FlightDB.addFlight(flight2);
FlightDB.addFlight(flight3);
User user1 = new User("1" , "Salameh" , flight3);
User user2 = new User("2" , "Einstein" , flight2);
User user3 = new User("3" , "Gambler " , flight1);
UserDB.addUser(user1);
UserDB.addUser(user2);
UserDB.addUser(user3);
} }
} }
\ No newline at end of file
server.port=8081 server.port=8082
\ No newline at end of file \ No newline at end of file
...@@ -21,6 +21,12 @@ ...@@ -21,6 +21,12 @@
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>6.0.13</version> <version>6.0.13</version>
</dependency> </dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>DataBase</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
......
package org.wheatherservice; package org.wheatherservice;
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 weatherDescription;
double temperature;
double humidity;
double windSpeed;
public Weather()
{
cityName = "";
weatherDescription = "";
temperature = humidity = windSpeed = 0.0;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getWeatherDescription() {
return weatherDescription;
}
public void setWeatherDescription(String weatherDescription) {
this.weatherDescription = weatherDescription;
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double 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
public String toString()
{
if (cityName.isEmpty())
{
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;
return ans;
}
} }
package org.wheatherservice; package org.wheatherservice;
import org.database.Flight;
import org.database.FlightDB;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -14,10 +16,20 @@ public class WeatherService ...@@ -14,10 +16,20 @@ public class WeatherService
@Autowired @Autowired
RestTemplate restTemplate; RestTemplate restTemplate;
@RequestMapping("/{flightID}") String FlightDBURL = "http://localhost:8081/flights-db/";
public Weather getWeather(@PathVariable("flightID") String flightID) String myAPIKey = "d5e468d45e234c749b9123443230105";
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)
{ {
if (city.isEmpty())
{
return new Weather();
}
return null; String url = prefURL + city + sufURL;
Weather weather = restTemplate.getForObject(url , Weather.class);
return weather;
} }
} }
server.port=8082 server.port=8083
\ No newline at end of file \ No newline at end of file
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