1+ package org.dreamexposure.discal.server.endpoints.v2.event.list
2+
3+
4+ import com.fasterxml.jackson.databind.ObjectMapper
5+ import discord4j.common.util.Snowflake
6+ import kotlinx.coroutines.reactor.mono
7+ import kotlinx.serialization.encodeToString
8+ import org.dreamexposure.discal.core.annotations.SecurityRequirement
9+ import org.dreamexposure.discal.core.business.CalendarService
10+ import org.dreamexposure.discal.core.logger.LOGGER
11+ import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventListV2Model
12+ import org.dreamexposure.discal.core.`object`.new.model.discal.v2.EventV2Model
13+ import org.dreamexposure.discal.core.utils.GlobalVal
14+ import org.dreamexposure.discal.server.utils.Authentication
15+ import org.dreamexposure.discal.server.utils.responseMessage
16+ import org.json.JSONException
17+ import org.json.JSONObject
18+ import org.springframework.http.server.reactive.ServerHttpResponse
19+ import org.springframework.web.bind.annotation.PostMapping
20+ import org.springframework.web.bind.annotation.RequestBody
21+ import org.springframework.web.bind.annotation.RequestMapping
22+ import org.springframework.web.bind.annotation.RestController
23+ import org.springframework.web.server.ServerWebExchange
24+ import reactor.core.publisher.Mono
25+ import java.time.Instant
26+
27+ @RestController
28+ @RequestMapping(" /v2/events/list" )
29+ class ListEventMonthEndpoint (
30+ private val authentication : Authentication ,
31+ private val calendarService : CalendarService ,
32+ private val objectMapper : ObjectMapper
33+ ) {
34+ @PostMapping(" /date" , produces = [" application/json" ])
35+ @SecurityRequirement(disableSecurity = true , scopes = [])
36+ fun listByMonth (swe : ServerWebExchange , response : ServerHttpResponse , @RequestBody rBody : String ): Mono <String > {
37+ return authentication.authenticate(swe).flatMap { authState ->
38+ if (! authState.success) {
39+ response.rawStatusCode = authState.status
40+ return @flatMap Mono .just(GlobalVal .JSON_FORMAT .encodeToString(authState))
41+ }
42+
43+ // Handle request
44+ val body = JSONObject (rBody)
45+ val guildId = Snowflake .of(body.getString(" guild_id" ))
46+ val calendarNumber = body.getInt(" calendar_number" )
47+ val start = Instant .ofEpochMilli(body.getLong(" epoch_start" ))
48+ val end = Instant .ofEpochMilli(body.getLong(" epoch_end" ))
49+
50+ mono { calendarService.getCalendar(guildId, calendarNumber) }.flatMap { calendar ->
51+ mono { calendarService.getEventsInTimeRange(guildId, calendarNumber, start, end) }.map { events ->
52+ events.map { EventV2Model (it, calendar) }
53+ }
54+ }
55+ .map { EventListV2Model (it, " Success" ) }
56+ .map { objectMapper.writeValueAsString(it) }
57+ .doOnNext { response.rawStatusCode = GlobalVal .STATUS_SUCCESS }
58+ .switchIfEmpty(responseMessage(" Calendar not found" )
59+ .doOnNext { response.rawStatusCode = GlobalVal .STATUS_NOT_FOUND }
60+ )
61+ }.onErrorResume(JSONException ::class .java) {
62+ LOGGER .trace(" [API-v2] JSON error. Bad request?" , it)
63+
64+ response.rawStatusCode = GlobalVal .STATUS_BAD_REQUEST
65+ return @onErrorResume responseMessage(" Bad Request" )
66+ }.onErrorResume {
67+ LOGGER .error(GlobalVal .DEFAULT , " [API-v2] list events by date error" , it)
68+
69+ response.rawStatusCode = GlobalVal .STATUS_INTERNAL_ERROR
70+ return @onErrorResume responseMessage(" Internal Server Error" )
71+ }
72+ }
73+ }
0 commit comments