-
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.
WIP - Still working on this refactor, I like it I think
- Loading branch information
1 parent
0343ad2
commit 7f160ca
Showing
13 changed files
with
270 additions
and
84 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
core/src/main/kotlin/org/dreamexposure/discal/core/business/CalendarService.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,65 @@ | ||
package org.dreamexposure.discal.core.business | ||
|
||
import discord4j.common.util.Snowflake | ||
import kotlinx.coroutines.reactor.awaitSingle | ||
import org.dreamexposure.discal.CalendarCache | ||
import org.dreamexposure.discal.core.database.CalendarRepository | ||
import org.dreamexposure.discal.core.`object`.new.Calendar | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class DefaultCalendarService( | ||
private val calendarRepository: CalendarRepository, | ||
private val calendarCache: CalendarCache, | ||
) : CalendarService { | ||
override suspend fun getAllCalendars(guildId: Snowflake): List<Calendar> { | ||
var calendars = calendarCache.get(guildId.asLong())?.toList() | ||
if (calendars != null) return calendars | ||
|
||
calendars = calendarRepository.findAllByGuildId(guildId.asLong()) | ||
.map(::Calendar) | ||
.collectList() | ||
.awaitSingle() | ||
|
||
calendarCache.put(guildId.asLong(), calendars.toTypedArray()) | ||
return calendars | ||
} | ||
|
||
override suspend fun getCalendar(guildId: Snowflake, number: Int): Calendar? { | ||
return getAllCalendars(guildId).first { it.number == number } | ||
} | ||
|
||
override suspend fun updateCalendar(calendar: Calendar) { | ||
calendarRepository.updateCalendarByGuildIdAndCalendarNumber( | ||
guildId = calendar.guildId.asLong(), | ||
calendarNumber = calendar.number, | ||
host = calendar.host.name, | ||
calendarId = calendar.id, | ||
calendarAddress = calendar.address, | ||
external = calendar.external, | ||
credentialId = calendar.secrets.credentialId, | ||
privateKey = calendar.secrets.privateKey, | ||
accessToken = calendar.secrets.encryptedAccessToken, | ||
refreshToken = calendar.secrets.encryptedRefreshToken, | ||
expiresAt = calendar.secrets.expiresAt.toEpochMilli(), | ||
).awaitSingle() | ||
|
||
val cached = calendarCache.get(calendar.guildId.asLong()) | ||
if (cached != null) { | ||
val newList = cached.toMutableList() | ||
newList.removeIf { it.number == calendar.number } | ||
calendarCache.put(calendar.guildId.asLong(), (newList + calendar).toTypedArray()) | ||
} | ||
} | ||
|
||
} | ||
|
||
interface CalendarService { | ||
// TODO: Need a function to invalidate cache because bot and API are using Db Manager | ||
|
||
suspend fun getAllCalendars(guildId: Snowflake): List<Calendar> | ||
|
||
suspend fun getCalendar(guildId: Snowflake, number: Int): Calendar? | ||
|
||
suspend fun updateCalendar(calendar: Calendar) | ||
} |
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
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
Oops, something went wrong.