Skip to content

Commit

Permalink
refactor(backend): move Firestore implementation to dao classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Oct 24, 2024
1 parent 5d4bfbb commit f25425a
Show file tree
Hide file tree
Showing 28 changed files with 544 additions and 705 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
package com.paligot.confily.backend.categories

import com.paligot.confily.backend.internals.helpers.database.Database
import com.paligot.confily.backend.internals.helpers.database.get
import com.paligot.confily.backend.internals.helpers.database.getAll
import com.google.cloud.firestore.Firestore
import com.paligot.confily.backend.internals.helpers.database.batchDelete
import com.paligot.confily.backend.internals.helpers.database.diffRefs
import com.paligot.confily.backend.internals.helpers.database.getDocument
import com.paligot.confily.backend.internals.helpers.database.getDocuments
import com.paligot.confily.backend.internals.helpers.database.insert
import com.paligot.confily.backend.internals.helpers.database.update

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
)
class CategoryDao(
private val projectName: String,
private val firestore: Firestore
) {
fun get(eventId: String, id: String): CategoryDb? = firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.getDocument(id)

suspend fun getAll(eventId: String): List<CategoryDb> = database.getAll(
eventId = eventId,
collectionName = CollectionName
)
fun getAll(eventId: String): List<CategoryDb> = firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.getDocuments<CategoryDb>()

suspend fun createOrUpdate(eventId: String, item: CategoryDb) {
fun createOrUpdate(eventId: String, item: CategoryDb) {
if (item.id == null) {
database.insert(
eventId = eventId,
collectionName = CollectionName
) { item.copy(id = it) }
firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.insert { item.copy(id = it) }
} else {
database.update(
eventId = eventId,
collectionName = CollectionName,
id = item.id,
item = item
)
firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.update(item.id, item)
}
}

suspend fun deleteDiff(eventId: String, ids: List<String>) {
val diff = database.diff(eventId, CollectionName, ids)
database.deleteAll(eventId, CollectionName, diff)
fun deleteDiff(eventId: String, ids: List<String>) {
val diff = firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.diffRefs(ids)
firestore.batchDelete(diff)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.paligot.confily.backend.categories

import com.paligot.confily.backend.events.EventModule.eventDao
import com.paligot.confily.backend.internals.InternalModule.database
import com.paligot.confily.backend.internals.GoogleServicesModule.cloudFirestore
import com.paligot.confily.backend.internals.SystemEnv.projectName

object CategoryModule {
val categoryDao = lazy { CategoryDao(database.value) }
val categoryDao = lazy { CategoryDao(projectName, cloudFirestore.value) }
val categoryRepository = lazy { CategoryRepository(eventDao.value, categoryDao.value) }
}
Original file line number Diff line number Diff line change
@@ -1,83 +1,76 @@
package com.paligot.confily.backend.events

import com.google.cloud.firestore.Firestore
import com.paligot.confily.backend.NotAuthorized
import com.paligot.confily.backend.NotFoundException
import com.paligot.confily.backend.internals.helpers.database.BasicDatabase
import com.paligot.confily.backend.internals.helpers.database.get
import com.paligot.confily.backend.internals.helpers.database.getAll
import com.paligot.confily.backend.internals.helpers.database.getDocument
import com.paligot.confily.backend.internals.helpers.database.getDocuments
import com.paligot.confily.backend.internals.helpers.database.update
import com.paligot.confily.backend.internals.helpers.database.upsert

class EventDao(
private val projectName: String,
private val database: BasicDatabase
private val firestore: Firestore
) {
suspend fun list(): List<EventDb> = database
.getAll<EventDb>(projectName)
fun list(): List<EventDb> = firestore
.collection(projectName)
.getDocuments<EventDb>()
.filter { it.published }

suspend fun get(id: String): EventDb? = database.get(projectName, id)
fun get(id: String): EventDb? = firestore
.collection(projectName)
.getDocument(id)

suspend fun getVerified(id: String, apiKey: String?): EventDb {
val eventDb = database.get<EventDb>(projectName, id)
fun getVerified(id: String, apiKey: String?): EventDb {
val eventDb = firestore
.collection(projectName)
.getDocument<EventDb>(id)
?: throw NotFoundException("Event $id Not Found")
return if (eventDb.apiKey == apiKey) eventDb else throw NotAuthorized
}

suspend fun createOrUpdate(event: EventDb) {
val existing = database.get<EventDb>(projectName, event.slugId)
if (existing == null) {
database.insert(projectName, event.slugId, event)
} else {
database.update(
projectName,
event.slugId,
event.copy(updatedAt = System.currentTimeMillis())
)
}
fun createOrUpdate(event: EventDb) {
firestore
.collection(projectName)
.upsert(event.slugId, event.copy(updatedAt = System.currentTimeMillis()))
}

suspend fun updateMenus(eventId: String, apiKey: String, menus: List<LunchMenuDb>) {
fun updateMenus(eventId: String, apiKey: String, menus: List<LunchMenuDb>) {
val existing = getVerified(eventId, apiKey)
database.update(
projectName,
eventId,
existing.copy(menus = menus, updatedAt = System.currentTimeMillis())
)
firestore
.collection(projectName)
.update(eventId, existing.copy(menus = menus, updatedAt = System.currentTimeMillis()))
}

suspend fun updateCoc(eventId: String, apiKey: String, coc: String) {
fun updateCoc(eventId: String, apiKey: String, coc: String) {
val existing = getVerified(eventId, apiKey)
database.update(
projectName,
eventId,
existing.copy(coc = coc, updatedAt = System.currentTimeMillis())
)
firestore
.collection(projectName)
.update(eventId, existing.copy(coc = coc, updatedAt = System.currentTimeMillis()))
}

suspend fun updateFeatures(eventId: String, apiKey: String, hasNetworking: Boolean) {
fun updateFeatures(eventId: String, apiKey: String, hasNetworking: Boolean) {
val existing = getVerified(eventId, apiKey)
database.update(
projectName,
eventId,
existing.copy(
features = FeaturesActivatedDb(hasNetworking = hasNetworking),
updatedAt = System.currentTimeMillis()
firestore
.collection(projectName)
.update(
eventId,
existing.copy(
features = FeaturesActivatedDb(hasNetworking = hasNetworking),
updatedAt = System.currentTimeMillis()
)
)
)
}

suspend fun updateUpdatedAt(event: EventDb) {
database.update(
projectName,
event.slugId,
event.copy(updatedAt = System.currentTimeMillis())
)
fun updateUpdatedAt(event: EventDb) {
firestore
.collection(projectName)
.update(event.slugId, event.copy(updatedAt = System.currentTimeMillis()))
}

suspend fun updateAgendaUpdatedAt(event: EventDb) {
database.update(
projectName,
event.slugId,
event.copy(agendaUpdatedAt = System.currentTimeMillis())
)
fun updateAgendaUpdatedAt(event: EventDb) {
firestore
.collection(projectName)
.update(event.slugId, event.copy(agendaUpdatedAt = System.currentTimeMillis()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.paligot.confily.backend.events

import com.paligot.confily.backend.categories.CategoryModule.categoryDao
import com.paligot.confily.backend.formats.FormatModule.formatDao
import com.paligot.confily.backend.internals.InternalModule.basicDatabase
import com.paligot.confily.backend.internals.GoogleServicesModule.cloudFirestore
import com.paligot.confily.backend.internals.SystemEnv.projectName
import com.paligot.confily.backend.partners.PartnerModule.partnerDao
import com.paligot.confily.backend.qanda.QAndAModule.qAndADao
Expand All @@ -12,7 +12,7 @@ import com.paligot.confily.backend.speakers.SpeakerModule.speakerDao
import com.paligot.confily.backend.third.parties.geocode.GeocodeModule.geocodeApi

object EventModule {
val eventDao = lazy { EventDao(projectName, basicDatabase.value) }
val eventDao = lazy { EventDao(projectName, cloudFirestore.value) }
val eventRepository = lazy {
EventRepository(
geocodeApi.value,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
package com.paligot.confily.backend.formats

import com.paligot.confily.backend.internals.helpers.database.Database
import com.paligot.confily.backend.internals.helpers.database.get
import com.paligot.confily.backend.internals.helpers.database.getAll
import com.google.cloud.firestore.Firestore
import com.paligot.confily.backend.internals.helpers.database.batchDelete
import com.paligot.confily.backend.internals.helpers.database.diffRefs
import com.paligot.confily.backend.internals.helpers.database.getDocument
import com.paligot.confily.backend.internals.helpers.database.getDocuments
import com.paligot.confily.backend.internals.helpers.database.insert
import com.paligot.confily.backend.internals.helpers.database.update

private const val CollectionName = "formats"

class FormatDao(private val database: Database) {
suspend fun get(eventId: String, id: String): FormatDb? = database.get(
eventId = eventId,
collectionName = CollectionName,
id = id
)
class FormatDao(
private val projectName: String,
private val firestore: Firestore
) {
fun get(eventId: String, id: String): FormatDb? = firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.getDocument(id)

suspend fun getAll(eventId: String): List<FormatDb> = database.getAll(
eventId = eventId,
collectionName = CollectionName
)
fun getAll(eventId: String): List<FormatDb> = firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.getDocuments<FormatDb>()

suspend fun createOrUpdate(eventId: String, item: FormatDb) {
fun createOrUpdate(eventId: String, item: FormatDb) {
if (item.id == null) {
database.insert(
eventId = eventId,
collectionName = CollectionName
) { item.copy(id = it) }
firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.insert { item.copy(id = it) }
} else {
database.update(
eventId = eventId,
collectionName = CollectionName,
id = item.id,
item = item
)
firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.update(item.id, item)
}
}

suspend fun deleteDiff(eventId: String, ids: List<String>) {
val diff = database.diff(eventId, CollectionName, ids)
database.deleteAll(eventId, CollectionName, diff)
fun deleteDiff(eventId: String, ids: List<String>) {
val diff = firestore
.collection(projectName)
.document(eventId)
.collection(CollectionName)
.diffRefs(ids)
firestore.batchDelete(diff)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.paligot.confily.backend.formats

import com.paligot.confily.backend.events.EventModule.eventDao
import com.paligot.confily.backend.internals.InternalModule.database
import com.paligot.confily.backend.internals.GoogleServicesModule.cloudFirestore
import com.paligot.confily.backend.internals.SystemEnv.projectName

object FormatModule {
val formatDao = lazy { FormatDao(database.value) }
val formatDao = lazy { FormatDao(projectName, cloudFirestore.value) }
val formatRepository = lazy { FormatRepository(eventDao.value, formatDao.value) }
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package com.paligot.confily.backend.internals

import com.paligot.confily.backend.internals.GoogleServicesModule.cloudFirestore
import com.paligot.confily.backend.internals.GoogleServicesModule.cloudStorage
import com.paligot.confily.backend.internals.GoogleServicesModule.drive
import com.paligot.confily.backend.internals.GoogleServicesModule.secretManager
import com.paligot.confily.backend.internals.SystemEnv.gcpProjectId
import com.paligot.confily.backend.internals.SystemEnv.isCloud
import com.paligot.confily.backend.internals.SystemEnv.projectName
import com.paligot.confily.backend.internals.helpers.database.BasicDatabase
import com.paligot.confily.backend.internals.helpers.database.Database
import com.paligot.confily.backend.internals.helpers.drive.DriveDataSource
import com.paligot.confily.backend.internals.helpers.image.TranscoderImage
import com.paligot.confily.backend.internals.helpers.secret.Secret
import com.paligot.confily.backend.internals.helpers.storage.Storage

object InternalModule {
val database: Lazy<Database> = lazy {
Database.Factory.create(cloudFirestore.value, projectName)
}
val basicDatabase: Lazy<BasicDatabase> = lazy {
BasicDatabase.Factory.create(cloudFirestore.value)
}
val driveDataSource: Lazy<DriveDataSource> = lazy {
DriveDataSource.Factory.create(drive.value)
}
Expand Down

This file was deleted.

Loading

0 comments on commit f25425a

Please sign in to comment.