-
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.
feat(backend): support activities by partners.
- Loading branch information
1 parent
f25425a
commit 1a8f0b1
Showing
23 changed files
with
369 additions
and
56 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
42 changes: 42 additions & 0 deletions
42
backend/src/main/java/com/paligot/confily/backend/activities/ActivityDao.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 com.paligot.confily.backend.activities | ||
|
||
import com.google.cloud.firestore.Firestore | ||
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 = "activities" | ||
|
||
class ActivityDao( | ||
private val projectName: String, | ||
private val firestore: Firestore | ||
) { | ||
fun get(eventId: String, id: String): ActivityDb? = firestore | ||
.collection(projectName) | ||
.document(eventId) | ||
.collection(CollectionName) | ||
.getDocument(id) | ||
|
||
fun getAll(eventId: String): List<ActivityDb> = firestore | ||
.collection(projectName) | ||
.document(eventId) | ||
.collection(CollectionName) | ||
.getDocuments<ActivityDb>() | ||
|
||
fun createOrUpdate(eventId: String, item: ActivityDb) { | ||
if (item.id == null) { | ||
firestore | ||
.collection(projectName) | ||
.document(eventId) | ||
.collection(CollectionName) | ||
.insert { item.copy(id = it) } | ||
} else { | ||
firestore | ||
.collection(projectName) | ||
.document(eventId) | ||
.collection(CollectionName) | ||
.update(item.id, item) | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
backend/src/main/java/com/paligot/confily/backend/activities/ActivityDb.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,9 @@ | ||
package com.paligot.confily.backend.activities | ||
|
||
data class ActivityDb( | ||
val id: String? = null, | ||
val name: String = "", | ||
val startTime: String = "", | ||
val endTime: String = "", | ||
val partnerId: String = "" | ||
) |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/paligot/confily/backend/activities/ActivityMappers.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,20 @@ | ||
package com.paligot.confily.backend.activities | ||
|
||
import com.paligot.confily.models.Activity | ||
import com.paligot.confily.models.inputs.ActivityInput | ||
|
||
fun ActivityDb.convertToModel() = Activity( | ||
id = id ?: "", | ||
name = name, | ||
startTime = startTime, | ||
endTime = endTime, | ||
partnerId = partnerId | ||
) | ||
|
||
fun ActivityInput.convertToDb(id: String? = null) = ActivityDb( | ||
id = id, | ||
name = name, | ||
startTime = startTime, | ||
endTime = endTime, | ||
partnerId = partnerId | ||
) |
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/com/paligot/confily/backend/activities/ActivityModule.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,13 @@ | ||
package com.paligot.confily.backend.activities | ||
|
||
import com.paligot.confily.backend.events.EventModule.eventDao | ||
import com.paligot.confily.backend.internals.GoogleServicesModule.cloudFirestore | ||
import com.paligot.confily.backend.internals.SystemEnv.projectName | ||
import com.paligot.confily.backend.partners.PartnerModule.partnerDao | ||
|
||
object ActivityModule { | ||
val activityDao = lazy { ActivityDao(projectName, cloudFirestore.value) } | ||
val activityRepository = lazy { | ||
ActivityRepository(eventDao.value, partnerDao.value, activityDao.value) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/com/paligot/confily/backend/activities/ActivityRepository.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,35 @@ | ||
package com.paligot.confily.backend.activities | ||
|
||
import com.paligot.confily.backend.NotFoundException | ||
import com.paligot.confily.backend.events.EventDao | ||
import com.paligot.confily.backend.partners.PartnerDao | ||
import com.paligot.confily.models.inputs.ActivityInput | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.datetime.LocalDateTime | ||
|
||
class ActivityRepository( | ||
private val eventDao: EventDao, | ||
private val partnerDao: PartnerDao, | ||
private val activityDao: ActivityDao | ||
) { | ||
suspend fun create(eventId: String, apiKey: String, activity: ActivityInput) = coroutineScope { | ||
val event = eventDao.getVerified(eventId, apiKey) | ||
val partnerExist = partnerDao.exists(eventId, activity.partnerId) | ||
if (!partnerExist) { | ||
throw NotFoundException("Partner ${activity.partnerId} Not Found") | ||
} | ||
val eventStartDate = LocalDateTime.parse(event.startDate.dropLast(1)) | ||
val activityStartDate = LocalDateTime.parse(activity.startTime) | ||
if (activityStartDate < eventStartDate) { | ||
throw IllegalArgumentException("Activity start date must be after event start date") | ||
} | ||
val eventEndDate = LocalDateTime.parse(event.endDate.dropLast(1)) | ||
val activityEndDate = LocalDateTime.parse(activity.endTime) | ||
if (activityEndDate > eventEndDate) { | ||
throw IllegalArgumentException("Activity end date must be before event end date") | ||
} | ||
activityDao.createOrUpdate(eventId, activity.convertToDb()) | ||
eventDao.updateUpdatedAt(event) | ||
return@coroutineScope eventId | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/paligot/confily/backend/activities/ActivityRouting.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,20 @@ | ||
package com.paligot.confily.backend.activities | ||
|
||
import com.paligot.confily.backend.activities.ActivityModule.activityRepository | ||
import com.paligot.confily.backend.receiveValidated | ||
import com.paligot.confily.models.inputs.ActivityInput | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.response.respond | ||
import io.ktor.server.routing.Routing | ||
import io.ktor.server.routing.post | ||
|
||
fun Routing.registerActivitiesRoutes() { | ||
val repository by activityRepository | ||
|
||
post("/activities") { | ||
val eventId = call.parameters["eventId"]!! | ||
val apiKey = call.request.headers["api_key"]!! | ||
val activity = call.receiveValidated<ActivityInput>() | ||
call.respond(HttpStatusCode.Created, repository.create(eventId, apiKey, activity)) | ||
} | ||
} |
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
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
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.