Skip to content

Commit

Permalink
refactor(backend): create dedicated backend for categories.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Sep 30, 2023
1 parent 9babe12 commit ed49dcd
Show file tree
Hide file tree
Showing 28 changed files with 314 additions and 139 deletions.
33 changes: 25 additions & 8 deletions backend/src/main/java/org/gdglille/devfest/backend/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.route
import io.ktor.server.routing.routing
import org.gdglille.devfest.backend.third.parties.billetweb.registerBilletWebRoutes
import org.gdglille.devfest.backend.third.parties.conferencehall.registerConferenceHallRoutes
import org.gdglille.devfest.backend.categories.CategoryDao
import org.gdglille.devfest.backend.categories.registerCategoriesRoutes
import org.gdglille.devfest.backend.events.EventDao
import org.gdglille.devfest.backend.events.registerEventRoutes
import org.gdglille.devfest.backend.internals.helpers.database.BasicDatabase
import org.gdglille.devfest.backend.internals.helpers.database.Database
import org.gdglille.devfest.backend.internals.helpers.image.TranscoderImage
import org.gdglille.devfest.backend.internals.helpers.secret.Secret
import org.gdglille.devfest.backend.internals.helpers.storage.Storage
import org.gdglille.devfest.backend.third.parties.conferencehall.ConferenceHallApi
import org.gdglille.devfest.backend.third.parties.geocode.GeocodeApi
import org.gdglille.devfest.backend.third.parties.welovedevs.WeLoveDevsApi
import org.gdglille.devfest.backend.jobs.JobDao
import org.gdglille.devfest.backend.partners.PartnerDao
import org.gdglille.devfest.backend.partners.registerPartnersRoutes
Expand All @@ -43,6 +40,11 @@ import org.gdglille.devfest.backend.speakers.SpeakerDao
import org.gdglille.devfest.backend.speakers.registerSpeakersRoutes
import org.gdglille.devfest.backend.talks.TalkDao
import org.gdglille.devfest.backend.talks.registerTalksRoutes
import org.gdglille.devfest.backend.third.parties.billetweb.registerBilletWebRoutes
import org.gdglille.devfest.backend.third.parties.conferencehall.ConferenceHallApi
import org.gdglille.devfest.backend.third.parties.conferencehall.registerConferenceHallRoutes
import org.gdglille.devfest.backend.third.parties.geocode.GeocodeApi
import org.gdglille.devfest.backend.third.parties.welovedevs.WeLoveDevsApi
import org.gdglille.devfest.backend.third.parties.welovedevs.registerWLDRoutes
import org.gdglille.devfest.models.inputs.Validator
import org.gdglille.devfest.models.inputs.ValidatorException
Expand Down Expand Up @@ -85,6 +87,7 @@ fun main() {
)
val speakerDao = SpeakerDao(database, storage)
val talkDao = TalkDao(database)
val categoryDao = CategoryDao(database)
val scheduleItemDao = ScheduleItemDao(database)
val eventDao = EventDao(projectName, basicDatabase)
val partnerDao = PartnerDao(database, storage)
Expand Down Expand Up @@ -126,18 +129,32 @@ fun main() {
speakerDao,
qAndADao,
talkDao,
categoryDao,
scheduleItemDao,
partnerDao
)
route("/events/{eventId}") {
registerQAndAsRoutes(eventDao, qAndADao)
registerSpeakersRoutes(eventDao, speakerDao)
registerTalksRoutes(eventDao, speakerDao, talkDao)
registerSchedulersRoutes(eventDao, talkDao, speakerDao, scheduleItemDao)
registerTalksRoutes(eventDao, speakerDao, talkDao, categoryDao)
registerCategoriesRoutes(eventDao, categoryDao)
registerSchedulersRoutes(
eventDao,
talkDao,
categoryDao,
speakerDao,
scheduleItemDao
)
registerPartnersRoutes(geocodeApi, eventDao, partnerDao, jobDao, imageTranscoder)
// Third parties
registerBilletWebRoutes(eventDao)
registerConferenceHallRoutes(conferenceHallApi, eventDao, speakerDao, talkDao)
registerConferenceHallRoutes(
conferenceHallApi,
eventDao,
speakerDao,
talkDao,
categoryDao
)
registerWLDRoutes(wldApi, eventDao, partnerDao, jobDao)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.gdglille.devfest.backend.categories

import org.gdglille.devfest.backend.internals.helpers.database.Database
import org.gdglille.devfest.backend.internals.helpers.database.get
import org.gdglille.devfest.backend.internals.helpers.database.getAll

private const val CollectionName = "categories"

class CategoryDao(private val database: Database) {
suspend fun get(eventId: String, id: String): CategoryDb? = database.get(
eventId = eventId,
collectionName = CollectionName,
id = id
)

suspend fun getAll(eventId: String): List<CategoryDb> = database.getAll(
eventId = eventId,
collectionName = CollectionName
)

suspend fun createOrUpdate(eventId: String, item: CategoryDb) {
if (item.id == null) database.insert(
eventId = eventId,
collectionName = CollectionName
) { item.copy(id = it) }
else database.update(
eventId = eventId,
collectionName = CollectionName,
id = item.id,
item = item
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.gdglille.devfest.backend.categories

data class CategoryDb(
val id: String? = null,
val name: String = "",
val color: String = "",
val icon: String = ""
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.gdglille.devfest.backend.categories

import org.gdglille.devfest.models.Category
import org.gdglille.devfest.models.inputs.CategoryInput

fun CategoryDb.convertToModel() = Category(
id = id ?: "",
name = name,
color = color,
icon = icon
)

fun CategoryInput.convertToDb(id: String? = null) = CategoryDb(
id = id,
name = name,
color = color,
icon = icon
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.gdglille.devfest.backend.categories

import kotlinx.coroutines.coroutineScope
import org.gdglille.devfest.backend.NotFoundException
import org.gdglille.devfest.backend.events.EventDao
import org.gdglille.devfest.models.inputs.CategoryInput

class CategoryRepository(
private val eventDao: EventDao,
private val categoryDao: CategoryDao
) {
suspend fun list(eventId: String) = coroutineScope {
return@coroutineScope categoryDao.getAll(eventId)
.map { it.convertToModel() }
}

suspend fun get(eventId: String, categoryId: String) = coroutineScope {
return@coroutineScope categoryDao.get(eventId, categoryId)?.convertToModel()
?: throw NotFoundException("Category $categoryId Not Found")
}

suspend fun create(eventId: String, apiKey: String, category: CategoryInput) = coroutineScope {
val event = eventDao.getVerified(eventId, apiKey)
categoryDao.createOrUpdate(eventId, category.convertToDb())
eventDao.updateUpdatedAt(event)
return@coroutineScope eventId
}

suspend fun update(
eventId: String,
apiKey: String,
categoryId: String,
categoryInput: CategoryInput
) = coroutineScope {
val event = eventDao.getVerified(eventId, apiKey)
categoryDao.createOrUpdate(eventId, categoryInput.convertToDb(categoryId))
eventDao.updateUpdatedAt(event)
return@coroutineScope eventId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.gdglille.devfest.backend.categories

import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.put
import org.gdglille.devfest.backend.events.EventDao
import org.gdglille.devfest.backend.receiveValidated
import org.gdglille.devfest.models.inputs.CategoryInput

fun Route.registerCategoriesRoutes(
eventDao: EventDao,
categoryDao: CategoryDao
) {
val repository = CategoryRepository(eventDao, categoryDao)

get("/categories") {
val eventId = call.parameters["eventId"]!!
call.respond(HttpStatusCode.OK, repository.list(eventId))
}
get("/categories/{id}") {
val eventId = call.parameters["eventId"]!!
val catId = call.parameters["id"]!!
call.respond(HttpStatusCode.OK, repository.get(eventId, catId))
}
post("/categories") {
val eventId = call.parameters["eventId"]!!
val apiKey = call.request.headers["api_key"]!!
val catInput = call.receiveValidated<CategoryInput>()
call.respond(HttpStatusCode.Created, repository.create(eventId, apiKey, catInput))
}
put("/categories/{id}") {
val eventId = call.parameters["eventId"]!!
val apiKey = call.request.headers["api_key"]!!
val catId = call.parameters["id"]!!
val catInput = call.receiveValidated<CategoryInput>()
call.respond(HttpStatusCode.OK, repository.update(eventId, apiKey, catId, catInput))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ class EventDao(
)
}

suspend fun updateCategories(eventId: String, apiKey: String, categories: List<CategoryDb>) {
val existing = getVerified(eventId, apiKey)
database.update(
projectName,
eventId,
existing.copy(categories = categories, updatedAt = System.currentTimeMillis())
)
}

suspend fun updateFeatures(eventId: String, apiKey: String, hasNetworking: Boolean) {
val existing = getVerified(eventId, apiKey)
database.update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.gdglille.devfest.backend.events

import java.text.DecimalFormat
import java.time.LocalDateTime
import org.gdglille.devfest.backend.qanda.QAndADb

data class AddressDb(
val formatted: List<String> = emptyList(),
Expand All @@ -21,12 +20,6 @@ data class LunchMenuDb(
val dessert: String = ""
)

data class CategoryDb(
val name: String = "",
val color: String = "",
val icon: String = ""
)

data class FeaturesActivatedDb(
val hasNetworking: Boolean = false
)
Expand Down Expand Up @@ -64,7 +57,6 @@ data class EventDb(
val formats: Map<String, Int> = emptyMap(),
val menus: List<LunchMenuDb> = emptyList(),
val coc: String = "",
val categories: List<CategoryDb> = emptyList(),
val features: FeaturesActivatedDb = FeaturesActivatedDb(),
val contactPhone: String? = null,
val contactEmail: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package org.gdglille.devfest.backend.events

import java.util.UUID
import org.gdglille.devfest.backend.internals.slug
import org.gdglille.devfest.backend.qanda.QAndADb
import org.gdglille.devfest.backend.qanda.convertToModel
Expand All @@ -14,12 +13,12 @@ import org.gdglille.devfest.models.EventV2
import org.gdglille.devfest.models.EventV3
import org.gdglille.devfest.models.FeaturesActivated
import org.gdglille.devfest.models.inputs.BilletWebConfigInput
import org.gdglille.devfest.models.inputs.CategoryInput
import org.gdglille.devfest.models.inputs.ConferenceHallConfigInput
import org.gdglille.devfest.models.inputs.CreatingEventInput
import org.gdglille.devfest.models.inputs.EventInput
import org.gdglille.devfest.models.inputs.LunchMenuInput
import org.gdglille.devfest.models.inputs.WldConfigInput
import java.util.UUID

fun LunchMenuDb.convertToModel() = EventLunchMenu(
name = name,
Expand Down Expand Up @@ -124,12 +123,6 @@ fun LunchMenuInput.convertToDb() = LunchMenuDb(
dessert = dessert
)

fun CategoryInput.convertToDb() = CategoryDb(
name = name,
color = color,
icon = icon
)

fun ConferenceHallConfigInput.convertToDb() = ConferenceHallConfigurationDb(
eventId = eventId,
apiKey = apiKey
Expand Down
Loading

0 comments on commit ed49dcd

Please sign in to comment.