-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/tfip/strava/controller/WeatherRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package tfip.strava.controller; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static tfip.strava.util.Constants.*; | ||
|
||
import java.util.Optional; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import tfip.strava.model.Weather; | ||
import tfip.strava.service.WeatherApiService; | ||
|
||
@RestController | ||
@RequestMapping(path = API_WEATHER_ENDPOINT, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public class WeatherRestController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(WeatherRestController.class); | ||
|
||
@Autowired | ||
private WeatherApiService weatherSvc; | ||
|
||
@GetMapping() | ||
public ResponseEntity<String> getAllUsers() { | ||
Optional<Weather> weather = weatherSvc.getWeather(); | ||
if (weather.isEmpty()) { | ||
logger.info("FORECAST >>>>> NOT AVAILABLE."); | ||
return ResponseEntity | ||
.notFound() | ||
.build(); | ||
} else { | ||
logger.info("FORECAST >>>>> RETRIEVED."); | ||
return ResponseEntity | ||
.ok() | ||
.body(new Gson() | ||
.toJson(weather.get())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package tfip.strava.model; | ||
|
||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Weather { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Weather.class); | ||
|
||
private String description; | ||
private double temp; | ||
|
||
public Weather() { | ||
} | ||
|
||
public Weather(String description, double temp) { | ||
this.description = description; | ||
this.temp = temp; | ||
} | ||
|
||
public String getDescription() { | ||
return this.description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public double getTemp() { | ||
return this.temp; | ||
} | ||
|
||
public void setTemp(double temp) { | ||
this.temp = temp; | ||
} | ||
|
||
public static Weather toWeather(String json) { | ||
JsonObject openWeather = JsonParser | ||
.parseString(json) | ||
.getAsJsonObject(); | ||
return new Weather( | ||
openWeather | ||
.get("weather") | ||
.getAsJsonArray() | ||
.get(0) | ||
.getAsJsonObject() | ||
.get("description") | ||
.getAsString(), | ||
|
||
openWeather | ||
.get("main") | ||
.getAsJsonObject() | ||
.get("temp") | ||
.getAsDouble()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters