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

Merge branch 'Integration_feiertage_API_video' into 'main'

implement holiday service and controllers

See merge request !4
parents 9718e385 613336fe
No related branches found
No related tags found
1 merge request!4implement holiday service and controllers
......@@ -31,7 +31,7 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation "org.json:json:20230227"
implementation 'org.json:json:20231013'
}
kotlin {
......
package de.thk.gm.gdw.todolist.controllers
import de.thk.gm.gdw.todolist.models.Holiday
import de.thk.gm.gdw.todolist.services.HolidaysService
import org.json.JSONObject
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.LocalDate
@RestController
class HolidaysController (private val holidaysService: HolidaysService) {
@GetMapping("/holidays")
fun getHolidays() : List<Holiday> {
return holidaysService.getHolidays()
}
@GetMapping("/holidays", params = ["date"])
fun getHoliday(date: LocalDate) : Holiday {
val holiday = holidaysService.getHolidays(date)
if (holiday != null) {
return holiday
} else {
throw ResponseStatusException(HttpStatus.NOT_FOUND, "No Holidays found for $date")
}
}
}
\ No newline at end of file
package de.thk.gm.gdw.todolist.models
import java.time.LocalDate
class Holiday {
var name: String? = null
var date: LocalDate? = null
}
\ No newline at end of file
package de.thk.gm.gdw.todolist.services
import de.thk.gm.gdw.todolist.models.Holiday
import java.time.LocalDate
interface HolidaysService {
fun getHolidays(): List<Holiday>
fun getHolidays(date: LocalDate): Holiday?
}
\ No newline at end of file
package de.thk.gm.gdw.todolist.services
import de.thk.gm.gdw.todolist.models.Holiday
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
import java.time.LocalDate
@Service
class HolidaysServiceImpl (private val client : HttpClient = HttpClient.newBuilder().build()) : HolidaysService {
override fun getHolidays(): List<Holiday> {
val request = HttpRequest.newBuilder().GET().uri(URI.create("https://feiertage-api.de/api/?nur_land=NW")).build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString())
val json = JSONObject(response.body())
val holidays = ArrayList<Holiday>()
json.keySet().forEach { key ->
val holidayJson = json.getJSONObject(key)
val holiday = Holiday()
holiday.name = key
holiday.date = LocalDate.parse(holidayJson.getString("datum"))
holidays.add(holiday)
}
return holidays
}
override fun getHolidays(date: LocalDate): Holiday? {
val holidays = getHolidays()
for (holiday in holidays) {
if(holiday.date == date) {
return holiday
}
}
return null
}
}
\ No newline at end of file
......@@ -9,5 +9,4 @@
<input placeholder="max.mustermann@example.org" name="email">
<button>Create user</button>
</form>
<h2>Temperature ${temperature}</h2>
</@base.layout>
\ No newline at end of file
package de.thk.gm.gdw.todolist
import org.json.JSONArray
import org.json.JSONObject
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
fun main (args: Array<String>) {
val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder().GET().uri(URI.create("https://feiertage-api.de/api/?nur_land=NW")).build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString())
println(response.body())
}
\ No newline at end of file
package de.thk.gm.gdw.todolist
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
fun main(args: Array<String>) {
val personFile = File("src/test/resources/person.json").readText()
val person = JSONObject(personFile)
println(person.get("isProfessor"))
val personsFile = File("src/test/resources/persons.json").readText()
val persons = JSONArray(personsFile)
println(persons.getJSONObject(1).get("name"))
}
\ No newline at end of file
{
"name": "Nguyen",
"surname": "Viet",
"age": 38,
"hobbies" : ["football", "efootball","calisthenics"],
"isProfessor": true
}
\ No newline at end of file
[
{
"name": "Nguyen",
"surname": "Viet",
"age": 38,
"hobbies" : ["football", "efootball","calisthenics"],
"isProfessor": true
},
{
"name": "Bruegger",
"surname": "Yannic",
"age": 23,
"hobbies" : ["Linux", "Java","Spring Framework"],
"isProfessor": false
}
]
\ 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