diff --git a/build.gradle b/build.gradle index c0eea9af9d581aac4107e1a0e47a10052327dafb..ae87069a9f61648390fabc7a0f7d88c3ba443fff 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/HolidaysController.kt b/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/HolidaysController.kt new file mode 100644 index 0000000000000000000000000000000000000000..dcfc2a1a565952587a726523c6b17baa83fcbb68 --- /dev/null +++ b/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/HolidaysController.kt @@ -0,0 +1,32 @@ +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 diff --git a/src/main/kotlin/de/thk/gm/gdw/todolist/models/Holiday.kt b/src/main/kotlin/de/thk/gm/gdw/todolist/models/Holiday.kt new file mode 100644 index 0000000000000000000000000000000000000000..69c3010fd41997e663f43b7b988e4a7daff9aceb --- /dev/null +++ b/src/main/kotlin/de/thk/gm/gdw/todolist/models/Holiday.kt @@ -0,0 +1,8 @@ +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 diff --git a/src/main/kotlin/de/thk/gm/gdw/todolist/services/HolidaysService.kt b/src/main/kotlin/de/thk/gm/gdw/todolist/services/HolidaysService.kt new file mode 100644 index 0000000000000000000000000000000000000000..944757b6694c7bd75f80d1f44c28e4727f1d8855 --- /dev/null +++ b/src/main/kotlin/de/thk/gm/gdw/todolist/services/HolidaysService.kt @@ -0,0 +1,9 @@ +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 diff --git a/src/main/kotlin/de/thk/gm/gdw/todolist/services/HolidaysServiceImpl.kt b/src/main/kotlin/de/thk/gm/gdw/todolist/services/HolidaysServiceImpl.kt new file mode 100644 index 0000000000000000000000000000000000000000..7337d8da5a25ad15c76c5e97a6e0bbabf514d520 --- /dev/null +++ b/src/main/kotlin/de/thk/gm/gdw/todolist/services/HolidaysServiceImpl.kt @@ -0,0 +1,41 @@ +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 diff --git a/src/main/resources/templates/users/showUsers.ftlh b/src/main/resources/templates/users/showUsers.ftlh index 108b7ceeb60cfad15fc7b96178f9931cb27aee74..c78c48720897ce234e142b8243ba8f5f07f05663 100644 --- a/src/main/resources/templates/users/showUsers.ftlh +++ b/src/main/resources/templates/users/showUsers.ftlh @@ -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 diff --git a/src/test/kotlin/de/thk/gm/gdw/todolist/HTTPClient.kt b/src/test/kotlin/de/thk/gm/gdw/todolist/HTTPClient.kt new file mode 100644 index 0000000000000000000000000000000000000000..6f4bae09657bce5f279de1a72854a42421f79142 --- /dev/null +++ b/src/test/kotlin/de/thk/gm/gdw/todolist/HTTPClient.kt @@ -0,0 +1,16 @@ +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 diff --git a/src/test/kotlin/de/thk/gm/gdw/todolist/JSONDemo.kt b/src/test/kotlin/de/thk/gm/gdw/todolist/JSONDemo.kt new file mode 100644 index 0000000000000000000000000000000000000000..34bf988c0379d2fe9da5530edc31d421791c7bf6 --- /dev/null +++ b/src/test/kotlin/de/thk/gm/gdw/todolist/JSONDemo.kt @@ -0,0 +1,15 @@ +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 diff --git a/src/test/resources/person.json b/src/test/resources/person.json new file mode 100644 index 0000000000000000000000000000000000000000..d76da6de04bdd2590db3e58c2ca337d273a14cfc --- /dev/null +++ b/src/test/resources/person.json @@ -0,0 +1,7 @@ +{ + "name": "Nguyen", + "surname": "Viet", + "age": 38, + "hobbies" : ["football", "efootball","calisthenics"], + "isProfessor": true +} \ No newline at end of file diff --git a/src/test/resources/persons.json b/src/test/resources/persons.json new file mode 100644 index 0000000000000000000000000000000000000000..6cc5160594fcb06398420e86335cb3acefe19690 --- /dev/null +++ b/src/test/resources/persons.json @@ -0,0 +1,17 @@ +[ + { + "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