Skip to content

Commit

Permalink
feat(shared): use agenda service version 4.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed May 8, 2024
1 parent b35a13a commit 2301f8c
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import org.gdglille.devfest.database.mappers.convertTalkItemUi
import org.gdglille.devfest.database.mappers.convertToDb
import org.gdglille.devfest.database.mappers.convertToEntity
import org.gdglille.devfest.db.Conferences4HallDatabase
import org.gdglille.devfest.models.AgendaV3
import org.gdglille.devfest.models.AgendaV4
import org.gdglille.devfest.models.Session
import org.gdglille.devfest.models.ui.AgendaUi
import org.gdglille.devfest.models.ui.CategoryUi
import org.gdglille.devfest.models.ui.FiltersUi
Expand Down Expand Up @@ -63,7 +64,7 @@ class ScheduleDao(
val selectedFormatIds = selectedFormats.map { it.id }
val hasCategoryFilter = selectedCategories.isNotEmpty()
val hasFormatFilter = selectedFormats.isNotEmpty()
val talkItems = sessions
val sessionItems = sessions
.filter {
val selectedCategory =
if (hasCategoryFilter) selectedCategoryIds.contains(it.category_id) else true
Expand All @@ -82,9 +83,9 @@ class ScheduleDao(
}
}
.map {
val speakers = if (it.talk_id != null) {
val speakers = if (it.session_talk_id != null) {
db.sessionQueries
.selectSpeakersByTalkId(eventId, it.talk_id)
.selectSpeakersByTalkId(eventId, it.session_talk_id)
.executeAsList()
} else {
emptyList()
Expand All @@ -95,7 +96,7 @@ class ScheduleDao(
.associate { session ->
session.date to AgendaUi(
onlyFavorites = hasFavFilter,
talks = talkItems
sessions = sessionItems
.filter { it.startTime.startsWith(session.date) }
.sortedBy { it.slotTime }
.groupBy { it.slotTime }
Expand All @@ -119,9 +120,9 @@ class ScheduleDao(
val nextAgenda = sessions
.filter { dateTime < it.start_time.toLocalDateTime() }
.map {
val speakers = if (it.talk_id != null) {
val speakers = if (it.session_talk_id != null) {
db.sessionQueries
.selectSpeakersByTalkId(eventId, it.talk_id)
.selectSpeakersByTalkId(eventId, it.session_talk_id)
.executeAsList()
} else {
emptyList()
Expand Down Expand Up @@ -205,44 +206,56 @@ class ScheduleDao(
settings.putBoolean("ONLY_FAVORITES", false)
}

fun saveAgenda(eventId: String, agendaV3: AgendaV3) = db.transaction {
agendaV3.speakers.forEach { speaker ->
fun saveAgenda(eventId: String, agenda: AgendaV4) = db.transaction {
agenda.speakers.forEach { speaker ->
db.speakerQueries.upsertSpeaker(speaker.convertToDb(eventId))
}
agendaV3.categories.forEach { category ->
agenda.categories.forEach { category ->
db.categoryQueries.upsertCategory(category.convertToDb(eventId))
}
agendaV3.formats.forEach { format ->
agenda.formats.forEach { format ->
db.formatQueries.upsertFormat(format.convertToDb(eventId))
}
agendaV3.talks.forEach { talk ->
db.sessionQueries.upsertTalkSession(talk.convertToDb(eventId))
agenda.sessions.forEach { session ->
when (session) {
is Session.Talk -> {
db.sessionQueries.upsertTalkSession(session.convertToDb(eventId))
}
is Session.Event -> {
db.sessionQueries.upsertEventSession(session.convertToDb(eventId))
}
}
}
agendaV3.talks.forEach { talk ->
talk.speakers.forEach {
db.sessionQueries.upsertTalkWithSpeakersSession(talk.convertToDb(eventId, it))
agenda.sessions.filterIsInstance<Session.Talk>().forEach { session ->
session.speakers.forEach {
db.sessionQueries.upsertTalkWithSpeakersSession(session.convertToDb(eventId, it))
}
}
agendaV3.sessions.forEach { session ->
db.sessionQueries.upsertSession(session.convertToDb(eventId))
agenda.schedules.forEach { schedule ->
val clazz = if (agenda.sessions.find { it.id == schedule.sessionId } is Session.Talk) {
Session.Talk::class
} else {
Session.Event::class
}
db.sessionQueries.upsertSession(schedule.convertToDb(eventId, clazz))
}
clean(eventId, agendaV3)
clean(eventId, agenda)
}

private fun clean(eventId: String, agendaV3: AgendaV3) = db.transaction {
private fun clean(eventId: String, agenda: AgendaV4) = db.transaction {
val diffSpeakers = db.speakerQueries
.diffSpeakers(event_id = eventId, id = agendaV3.speakers.map { it.id })
.diffSpeakers(event_id = eventId, id = agenda.speakers.map { it.id })
.executeAsList()
db.speakerQueries.deleteSpeakers(event_id = eventId, id = diffSpeakers)
val diffCategories = db.categoryQueries
.diffCategories(event_id = eventId, id = agendaV3.categories.map { it.id })
.diffCategories(event_id = eventId, id = agenda.categories.map { it.id })
.executeAsList()
db.categoryQueries.deleteCategories(event_id = eventId, id = diffCategories)
val diffFormats = db.formatQueries
.diffFormats(event_id = eventId, id = agendaV3.formats.map { it.id })
.diffFormats(event_id = eventId, id = agenda.formats.map { it.id })
.executeAsList()
db.formatQueries.deleteFormats(event_id = eventId, id = diffFormats)
val talkIds = agendaV3.talks.map { it.id }
val talkIds = agenda.sessions.map { it.id }
val diffTalkSession = db.sessionQueries
.diffTalkSessions(event_id = eventId, id = talkIds)
.executeAsList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.gdglille.devfest.database.mappers

import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import org.gdglille.devfest.android.shared.resources.Strings
import org.gdglille.devfest.db.EventSession
import org.gdglille.devfest.db.SelectBreakSessions
import org.gdglille.devfest.db.SelectCategories
import org.gdglille.devfest.db.SelectFormats
Expand All @@ -15,16 +15,18 @@ import org.gdglille.devfest.db.SelectSessionByTalkId
import org.gdglille.devfest.db.SelectSessions
import org.gdglille.devfest.db.SelectSpeakersByTalkId
import org.gdglille.devfest.db.SelectTalksBySpeakerId
import org.gdglille.devfest.db.Session
import org.gdglille.devfest.db.TalkSession
import org.gdglille.devfest.db.TalkSessionWithSpeakers
import org.gdglille.devfest.extensions.formatHoursMinutes
import org.gdglille.devfest.models.ScheduleItemV3
import org.gdglille.devfest.models.TalkV3
import org.gdglille.devfest.models.ScheduleItemV4
import org.gdglille.devfest.models.Session
import org.gdglille.devfest.models.ui.CategoryUi
import org.gdglille.devfest.models.ui.EventSessionItemUi
import org.gdglille.devfest.models.ui.FormatUi
import org.gdglille.devfest.models.ui.TalkItemUi
import org.gdglille.devfest.models.ui.TalkUi
import kotlin.reflect.KClass
import org.gdglille.devfest.db.Session as SessionDb

private const val BREAK_TITLE = "break"
private const val MaxSpeakersCount = 3
Expand Down Expand Up @@ -107,28 +109,22 @@ fun SelectSessions.convertTalkItemUi(
)
}

fun SelectBreakSessions.convertTalkItemUi(strings: Strings): TalkItemUi {
fun SelectBreakSessions.convertTalkItemUi(strings: Strings): EventSessionItemUi {
val startDateTime = start_time.toLocalDateTime()
val endDateTime = end_time.toLocalDateTime()
val diff = endDateTime.toInstant(TimeZone.UTC).minus(startDateTime.toInstant(TimeZone.UTC))
val timeInMinutes = diff.inWholeMinutes.toInt()
return TalkItemUi(
return EventSessionItemUi(
id = id,
order = order_.toInt(),
title = strings.titles.agendaBreak,
abstract = "",
title = title,
description = description,
order = 0,
room = room,
level = null,
slotTime = startDateTime.formatHoursMinutes(),
startTime = start_time,
endTime = end_time,
timeInMinutes = timeInMinutes,
time = strings.texts.scheduleMinutes(timeInMinutes),
category = convertCategoryUi(),
speakers = persistentListOf(),
speakersAvatar = persistentListOf(),
speakersLabel = "",
isFavorite = false
time = strings.texts.scheduleMinutes(timeInMinutes)
)
}

Expand Down Expand Up @@ -204,19 +200,20 @@ fun SelectSessionByTalkId.convertTalkUi(
)
}

fun ScheduleItemV3.convertToDb(eventId: String): Session = Session(
fun <T : Session> ScheduleItemV4.convertToDb(eventId: String, type: KClass<T>): SessionDb = SessionDb(
id = this.id,
order_ = order.toLong(),
room = this.room,
date = this.date,
start_time = this.startTime,
end_time = this.endTime,
talk_id = talkId,
session_talk_id = if (type == Session.Talk::class) sessionId else null,
session_event_id = if (type == Session.Event::class) sessionId else null,
event_id = eventId,
is_favorite = false
)

fun TalkV3.convertToDb(eventId: String): TalkSession = TalkSession(
fun Session.Talk.convertToDb(eventId: String): TalkSession = TalkSession(
id = this.id,
title = this.title,
level = this.level,
Expand All @@ -230,9 +227,16 @@ fun TalkV3.convertToDb(eventId: String): TalkSession = TalkSession(
event_id = eventId
)

fun TalkV3.convertToDb(eventId: String, speakerId: String) = TalkSessionWithSpeakers(
fun Session.Talk.convertToDb(eventId: String, speakerId: String) = TalkSessionWithSpeakers(
id = 0L,
speaker_id = speakerId,
talk_id = id,
event_id = eventId
)

fun Session.Event.convertToDb(eventId: String): EventSession = EventSession(
id = this.id,
title = this.title,
description = this.description,
event_id = eventId
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import org.gdglille.devfest.Platform
import org.gdglille.devfest.exceptions.AgendaNotModifiedException
import org.gdglille.devfest.models.AgendaV3
import org.gdglille.devfest.models.AgendaV4
import org.gdglille.devfest.models.Attendee
import org.gdglille.devfest.models.EventList
import org.gdglille.devfest.models.EventV3
Expand Down Expand Up @@ -52,10 +52,10 @@ class ConferenceApi(
client.get("$baseUrl/events/$eventId/billet-web/$barcode")
.body<Attendee>()

suspend fun fetchAgenda(eventId: String, etag: String?): Pair<String, AgendaV3> {
suspend fun fetchAgenda(eventId: String, etag: String?): Pair<String, AgendaV4> {
val response = client.get("$baseUrl/events/$eventId/agenda") {
contentType(ContentType.parse("application/json"))
accept(ContentType.parse("application/json; version=3"))
accept(ContentType.parse("application/json; version=4"))
etag?.let { ifNoneMatch(etag) }
}
if (response.status == HttpStatusCode.NotModified) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import kotlin.Boolean;

CREATE TABLE EventSession(
id TEXT NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
event_id TEXT NOT NULL
);
CREATE TABLE Session_New (
id TEXT NOT NULL PRIMARY KEY,
order_ INTEGER NOT NULL,
event_id TEXT NOT NULL,
date TEXT NOT NULL,
start_time TEXT NOT NULL,
end_time TEXT NOT NULL,
room TEXT NOT NULL,
is_favorite INTEGER AS Boolean NOT NULL DEFAULT 0,
session_event_id TEXT,
session_talk_id TEXT,
FOREIGN KEY (session_event_id) REFERENCES EventSession(id),
FOREIGN KEY (session_talk_id) REFERENCES TalkSession(id)
);
DROP TABLE Session;
ALTER TABLE Session_New RENAME TO Session;
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ start_time TEXT NOT NULL,
end_time TEXT NOT NULL,
room TEXT NOT NULL,
is_favorite INTEGER AS Boolean NOT NULL DEFAULT 0,
talk_id TEXT,
FOREIGN KEY (talk_id) REFERENCES TalkSession(id)
session_event_id TEXT,
session_talk_id TEXT,
FOREIGN KEY (session_event_id) REFERENCES EventSession(id),
FOREIGN KEY (session_talk_id) REFERENCES TalkSession(id)
);

CREATE TABLE EventSession(
id TEXT NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
event_id TEXT NOT NULL
);

CREATE TABLE TalkSession(
Expand Down Expand Up @@ -41,21 +50,26 @@ FOREIGN KEY (talk_id) REFERENCES TalkSession(id)
CREATE UNIQUE INDEX TalkSessionWithSpeakersIndex ON TalkSessionWithSpeakers(speaker_id, talk_id, event_id);

selectSessions:
SELECT Session.id, Session.order_, Session.event_id, Session.date, Session.start_time, Session.end_time, Session.room, Session.is_favorite,
Session.talk_id, TalkSession.title, TalkSession.abstract, TalkSession.level, TalkSession.language, TalkSession.slide_url, TalkSession.replay_url, TalkSession.open_feedback_url,
TalkSession.category_id, Category.name AS categoryName, Category.color AS categoryColor, Category.icon AS categoryIcon,
TalkSession.format_id, Format.name AS formatName, Format.time
SELECT Session.id, Session.order_, Session.event_id, Session.date, Session.start_time,
Session.end_time, Session.room, Session.is_favorite, Session.session_talk_id,
TalkSession.title, TalkSession.abstract, TalkSession.level, TalkSession.language,
TalkSession.slide_url, TalkSession.replay_url, TalkSession.open_feedback_url,
TalkSession.category_id, Category.name AS categoryName, Category.color AS categoryColor,
Category.icon AS categoryIcon, TalkSession.format_id, Format.name AS formatName, Format.time
FROM Session
INNER JOIN TalkSession ON Session.talk_id = TalkSession.id
INNER JOIN TalkSession ON Session.session_talk_id = TalkSession.id
INNER JOIN Category ON TalkSession.category_id = Category.id
INNER JOIN Format ON TalkSession.format_id = Format.id
WHERE Session.event_id = ?
ORDER BY start_time;

selectBreakSessions:
SELECT Session.id, Session.order_, Session.event_id, Session.date, Session.start_time, Session.end_time, Session.room, Session.is_favorite
SELECT Session.id, Session.order_, Session.event_id, Session.date, Session.start_time,
Session.end_time, Session.room, Session.session_event_id, EventSession.title,
EventSession.description
FROM Session
WHERE Session.event_id = ? AND talk_id IS NULL
INNER JOIN EventSession ON Session.session_event_id = EventSession.id
WHERE Session.event_id = ?
ORDER BY start_time;

selectDays:
Expand All @@ -70,10 +84,10 @@ TalkSession.id, TalkSession.title, TalkSession.abstract, TalkSession.level, Talk
TalkSession.category_id, Category.name AS categoryName, Category.color AS categoryColor, Category.icon AS categoryIcon,
TalkSession.format_id, Format.name AS formatName, Format.time
FROM Session
INNER JOIN TalkSession ON Session.talk_id = TalkSession.id
INNER JOIN TalkSession ON Session.session_talk_id = TalkSession.id
INNER JOIN Category ON TalkSession.category_id = Category.id
INNER JOIN Format ON TalkSession.format_id = Format.id
WHERE Session.event_id = ? AND Session.talk_id = ?;
WHERE Session.event_id = ? AND Session.session_talk_id = ?;

selectSpeakersByTalkId:
SELECT Speaker.id, Speaker.display_name, Speaker.pronouns, Speaker.bio, Speaker.job_title, Speaker.company,
Expand All @@ -94,11 +108,14 @@ INNER JOIN Format ON TalkSession.format_id = Format.id
WHERE TalkSessionWithSpeakers.event_id = ? AND TalkSessionWithSpeakers.speaker_id = ?;

upsertSession:
INSERT OR REPLACE INTO Session(id, order_, event_id, date, start_time, end_time, room, talk_id) VALUES ?;
INSERT OR REPLACE INTO Session(id, order_, event_id, date, start_time, end_time, room, session_talk_id, session_event_id) VALUES ?;

markAsFavorite:
UPDATE Session SET is_favorite = ? WHERE id == ? AND event_id == ?;

upsertEventSession:
INSERT OR REPLACE INTO EventSession VALUES ?;

upsertTalkSession:
INSERT OR REPLACE INTO TalkSession VALUES ?;

Expand All @@ -124,7 +141,7 @@ DELETE FROM TalkSessionWithSpeakers WHERE event_id == ? AND talk_id IN ?;
diffSessions:
SELECT id
FROM Session
WHERE event_id == ? AND id NOT IN ? AND talk_id != "";
WHERE event_id == ? AND id NOT IN ?;

deleteSessions:
DELETE FROM Session WHERE event_id == ? AND id IN ?;

0 comments on commit 2301f8c

Please sign in to comment.