-
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): create dedicated backend for categories.
- Loading branch information
1 parent
9babe12
commit ed49dcd
Showing
28 changed files
with
314 additions
and
139 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
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/org/gdglille/devfest/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 |
---|---|---|
@@ -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 | ||
) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/org/gdglille/devfest/backend/categories/CategoryDb.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,8 @@ | ||
package org.gdglille.devfest.backend.categories | ||
|
||
data class CategoryDb( | ||
val id: String? = null, | ||
val name: String = "", | ||
val color: String = "", | ||
val icon: String = "" | ||
) |
18 changes: 18 additions & 0 deletions
18
backend/src/main/java/org/gdglille/devfest/backend/categories/CategoryMappers.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,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 | ||
) |
40 changes: 40 additions & 0 deletions
40
backend/src/main/java/org/gdglille/devfest/backend/categories/CategoryRepository.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,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 | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
backend/src/main/java/org/gdglille/devfest/backend/categories/CategoryRouting.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,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)) | ||
} | ||
} |
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.