Skip to content

Commit

Permalink
This should fix the json encoding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFox161 committed Dec 9, 2024
1 parent 1212dba commit 74b5595
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.dreamexposure.discal.core.`object`.new.model.discal.v2

data class EventListV2Model(
val events: List<EventV2Model>,
val message: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.dreamexposure.discal.server.endpoints.v2.event.list


import com.fasterxml.jackson.databind.ObjectMapper
import discord4j.common.util.Snowflake
import kotlinx.coroutines.reactor.mono
import kotlinx.serialization.encodeToString
import org.dreamexposure.discal.core.annotations.SecurityRequirement
import org.dreamexposure.discal.core.business.CalendarService
import org.dreamexposure.discal.core.logger.LOGGER
import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventListV2Model
import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventV2Model
import org.dreamexposure.discal.core.utils.GlobalVal
import org.dreamexposure.discal.server.utils.Authentication
import org.dreamexposure.discal.server.utils.responseMessage
import org.json.JSONException
import org.json.JSONObject
import org.springframework.http.server.reactive.ServerHttpResponse
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono
import java.time.Instant

@RestController
@RequestMapping("/v2/events/list")
class ListEventMonthEndpoint(
private val authentication: Authentication,
private val calendarService: CalendarService,
private val objectMapper: ObjectMapper
) {
@PostMapping("/date", produces = ["application/json"])
@SecurityRequirement(disableSecurity = true, scopes = [])
fun listByMonth(swe: ServerWebExchange, response: ServerHttpResponse, @RequestBody rBody: String): Mono<String> {
return authentication.authenticate(swe).flatMap { authState ->
if (!authState.success) {
response.rawStatusCode = authState.status
return@flatMap Mono.just(GlobalVal.JSON_FORMAT.encodeToString(authState))
}

//Handle request
val body = JSONObject(rBody)
val guildId = Snowflake.of(body.getString("guild_id"))
val calendarNumber = body.getInt("calendar_number")
val start = Instant.ofEpochMilli(body.getLong("epoch_start"))
val end = Instant.ofEpochMilli(body.getLong("epoch_end"))

mono { calendarService.getCalendar(guildId, calendarNumber) }.flatMap { calendar ->
mono { calendarService.getEventsInTimeRange(guildId, calendarNumber, start, end) }.map { events ->
events.map { EventV2Model(it, calendar) }
}
}
.map { EventListV2Model(it, "Success") }
.map { objectMapper.writeValueAsString(it) }
.doOnNext { response.rawStatusCode = GlobalVal.STATUS_SUCCESS }
.switchIfEmpty(responseMessage("Calendar not found")
.doOnNext { response.rawStatusCode = GlobalVal.STATUS_NOT_FOUND }
)
}.onErrorResume(JSONException::class.java) {
LOGGER.trace("[API-v2] JSON error. Bad request?", it)

response.rawStatusCode = GlobalVal.STATUS_BAD_REQUEST
return@onErrorResume responseMessage("Bad Request")
}.onErrorResume {
LOGGER.error(GlobalVal.DEFAULT, "[API-v2] list events by date error", it)

response.rawStatusCode = GlobalVal.STATUS_INTERNAL_ERROR
return@onErrorResume responseMessage("Internal Server Error")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package org.dreamexposure.discal.server.endpoints.v2.calendar
package org.dreamexposure.discal.server.endpoints.v2.event.list

import com.fasterxml.jackson.databind.ObjectMapper
import discord4j.common.util.Snowflake
import kotlinx.coroutines.reactor.mono
import org.dreamexposure.discal.core.logger.LOGGER
import kotlinx.serialization.encodeToString
import org.dreamexposure.discal.core.annotations.SecurityRequirement
import org.dreamexposure.discal.core.business.CalendarService
import org.dreamexposure.discal.core.logger.LOGGER
import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventListV2Model
import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventV2Model
import org.dreamexposure.discal.core.utils.GlobalVal
import org.dreamexposure.discal.server.utils.Authentication
import org.dreamexposure.discal.server.utils.responseMessage
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.springframework.http.server.reactive.ServerHttpResponse
Expand All @@ -20,9 +22,6 @@ import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono
import java.time.Instant
import kotlinx.serialization.encodeToString
import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventV2Model


@RestController
@RequestMapping("/v2/events/list")
Expand Down Expand Up @@ -50,12 +49,14 @@ class ListEventMonthEndpoint(

mono { calendarService.getCalendar(guildId, calendarNumber) }.flatMap { calendar ->
mono { calendarService.getEventsInMonth(guildId, calendarNumber, start, daysInMonth) }.map { events ->
events.map { objectMapper.writeValueAsString(EventV2Model(it, calendar)) }
events.map { EventV2Model(it, calendar) }
}
}.map(::JSONArray)
.map { JSONObject().put("events", it).put("message", "Success").toString() }
}
.map { EventListV2Model(it, "Success") }
.map { objectMapper.writeValueAsString(it) }
.doOnNext { response.rawStatusCode = GlobalVal.STATUS_SUCCESS }
.switchIfEmpty(responseMessage("Calendar not found")
.switchIfEmpty(
responseMessage("Calendar not found")
.doOnNext { response.rawStatusCode = GlobalVal.STATUS_NOT_FOUND }
)
}.onErrorResume(JSONException::class.java) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import kotlinx.serialization.encodeToString
import org.dreamexposure.discal.core.annotations.SecurityRequirement
import org.dreamexposure.discal.core.business.CalendarService
import org.dreamexposure.discal.core.logger.LOGGER
import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventListV2Model
import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventV2Model
import org.dreamexposure.discal.core.utils.GlobalVal
import org.dreamexposure.discal.server.utils.Authentication
import org.dreamexposure.discal.server.utils.responseMessage
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.springframework.http.server.reactive.ServerHttpResponse
Expand Down Expand Up @@ -48,10 +48,11 @@ class ListEventRangeEndpoint(

mono { calendarService.getCalendar(guildId, calendarNumber) }.flatMap { calendar ->
mono { calendarService.getEventsInTimeRange(guildId, calendarNumber, start, end) }.map { events ->
events.map { objectMapper.writeValueAsString(EventV2Model(it, calendar)) }
events.map { EventV2Model(it, calendar) }
}
}.map(::JSONArray)
.map { JSONObject().put("events", it).put("message", "Success").toString() }
}
.map { EventListV2Model(it, "Success") }
.map { objectMapper.writeValueAsString(it) }
.doOnNext { response.rawStatusCode = GlobalVal.STATUS_SUCCESS }
.switchIfEmpty(responseMessage("Calendar not found")
.doOnNext { response.rawStatusCode = GlobalVal.STATUS_NOT_FOUND }
Expand Down

0 comments on commit 74b5595

Please sign in to comment.