-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This should fix the json encoding issues
- Loading branch information
1 parent
1212dba
commit 74b5595
Showing
4 changed files
with
95 additions
and
14 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.../main/kotlin/org/dreamexposure/discal/core/object/new/model/discal/v2/EventListV2Model.kt
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,6 @@ | ||
package org.dreamexposure.discal.core.`object`.new.model.discal.v2 | ||
|
||
data class EventListV2Model( | ||
val events: List<EventV2Model>, | ||
val message: String, | ||
) |
73 changes: 73 additions & 0 deletions
73
...n/kotlin/org/dreamexposure/discal/server/endpoints/v2/event/list/ListEventDateEndpoint.kt
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,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") | ||
} | ||
} | ||
} |
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