-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backend): move Firestore implementation to dao classes.
- Loading branch information
1 parent
5d4bfbb
commit f25425a
Showing
28 changed files
with
544 additions
and
705 deletions.
There are no files selected for viewing
66 changes: 39 additions & 27 deletions
66
backend/src/main/java/com/paligot/confily/backend/categories/CategoryDao.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 |
---|---|---|
@@ -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) | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
backend/src/main/java/com/paligot/confily/backend/categories/CategoryModule.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 |
---|---|---|
@@ -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) } | ||
} |
97 changes: 45 additions & 52 deletions
97
backend/src/main/java/com/paligot/confily/backend/events/EventDao.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 |
---|---|---|
@@ -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())) | ||
} | ||
} |
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
66 changes: 39 additions & 27 deletions
66
backend/src/main/java/com/paligot/confily/backend/formats/FormatDao.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 |
---|---|---|
@@ -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) | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
backend/src/main/java/com/paligot/confily/backend/formats/FormatModule.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 |
---|---|---|
@@ -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) } | ||
} |
9 changes: 0 additions & 9 deletions
9
backend/src/main/java/com/paligot/confily/backend/internals/InternalModule.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
27 changes: 0 additions & 27 deletions
27
...end/src/main/java/com/paligot/confily/backend/internals/helpers/database/BasicDatabase.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.