Skip to content
Snippets Groups Projects
Commit 2ab10a02 authored by Hoai Viet Nguyen's avatar Hoai Viet Nguyen
Browse files

remove demo Weatherservice

parent 5f670c76
Branches frist_ui_elements_with_freemarker
No related tags found
1 merge request!3Frist ui elements with freemarker
package de.thk.gm.gdw.todolist.controllers package de.thk.gm.gdw.todolist.controllers
import de.thk.gm.gdw.todolist.services.WeatherService
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.stereotype.Controller import org.springframework.stereotype.Controller
import org.springframework.ui.Model import org.springframework.ui.Model
...@@ -10,12 +9,11 @@ import org.springframework.web.bind.annotation.RequestMapping ...@@ -10,12 +9,11 @@ import org.springframework.web.bind.annotation.RequestMapping
@Controller @Controller
@RequestMapping(produces = [MediaType.TEXT_HTML_VALUE]) @RequestMapping(produces = [MediaType.TEXT_HTML_VALUE])
class UsersController (private val usersRestController: UsersRestController, private val weatherService: WeatherService) { class UsersController (private val usersRestController: UsersRestController) {
@GetMapping("/") @GetMapping("/")
fun showUsers(model: Model): String { fun showUsers(model: Model): String {
val users = usersRestController.getUsers() val users = usersRestController.getUsers()
model.addAttribute("users", users) model.addAttribute("users", users)
model.addAttribute("temperature", weatherService.getCurrentWeather())
return "users/showUsers" return "users/showUsers"
} }
......
package de.thk.gm.gdw.todolist.services
interface WeatherService {
fun getCurrentWeather() : Float
}
\ No newline at end of file
package de.thk.gm.gdw.todolist.services
import org.json.JSONObject
import org.springframework.stereotype.Service
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
@Service
class WeatherServiceImpl : WeatherService {
override fun getCurrentWeather() : Float {
val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder().GET().uri(URI.create("https://api.open-meteo.com/v1/forecast?latitude=51.0261&longitude=7.5647&current=temperature_2m,rain")).build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString())
val json = JSONObject(response.body())
return json.getJSONObject("current").getFloat("temperature_2m")
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment