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 @@
<modelVersion>4.0.0</modelVersion>
<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>
<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
{
return flightID;
}
public String getDestination()
{
return destination;
}
@Override
public String toString()
{
return "Flight ID : " + flightID + "\nDestination is : " + destination + "\nDate: "+date;
}
}
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;
@RestController
@RequestMapping("/flights-db")
public class FlightDB
{
public static Map<String , Flight> flightsDB;
......@@ -12,9 +19,10 @@ public class FlightDB
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);
return null;
return new Flight("",new Date(),"");
}
}
......@@ -16,18 +16,22 @@ public class User
flights.add(flight.getFlightID());
FlightDB.addFlight(flight);
}
public void addFlight(Flight flight)
{
flights.add(flight.getFlightID());
FlightDB.addFlight(flight);
}
public String getUserID() {
return userID;
}
public List<Flight> getFlights()
public List<String> getFlightsIDs()
{
List<Flight> ans = new ArrayList<>();
for (String id : flights)
{
ans.add(FlightDB.getFlight(id));
}
return ans;
return flights;
}
public String getName() {
return name;
}
}
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;
@RestController
@RequestMapping("/users-db")
public class UserDB
{
public static Map<String , User> usersDB;
......@@ -11,7 +17,9 @@ public class UserDB
if (usersDB.containsKey(id)) return;
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);
return null;
......
server.port=8081
\ No newline at end of file
......@@ -27,6 +27,12 @@
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>WeatherService</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
......
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.RequestMapping;
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;
......@@ -12,11 +19,38 @@ import java.util.List;
@RequestMapping("/flights")
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}")
public List<Flight> getFlights(@PathVariable("userID") String userID)
{
User user = UserDB.getUser(userID);
if (user == null) return null;
return user.getFlights();
User user = restTemplate.getForObject(UserDBURL+userID , User.class);
if (user == null) return new ArrayList<>();
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;
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
{
public static void main(String[] args)
{
init();
SpringApplication.run(UserServiceApp.class, args);
}
public static void init()
@Bean
public RestTemplate getRestTemplate()
{
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);
User user2 = new User("2" , "Einstein" , flight2);
User user3 = new User("3" , "Gambler " , flight1);
UserDB.addUser(user1);
UserDB.addUser(user2);
UserDB.addUser(user3);
return new RestTemplate();
}
}
\ No newline at end of file
server.port=8081
\ No newline at end of file
server.port=8082
\ No newline at end of file
......@@ -21,6 +21,12 @@
<artifactId>spring-web</artifactId>
<version>6.0.13</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>DataBase</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
......
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;
import org.database.Flight;
import org.database.FlightDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -14,10 +16,20 @@ public class WeatherService
@Autowired
RestTemplate restTemplate;
@RequestMapping("/{flightID}")
public Weather getWeather(@PathVariable("flightID") String flightID)
String FlightDBURL = "http://localhost:8081/flights-db/";
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
\ No newline at end of file
server.port=8083
\ 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