Skip to content

Commit

Permalink
SNAPSHOT Migrate Club entity in the domain and presentation layer
Browse files Browse the repository at this point in the history
  • Loading branch information
carltonwhitehead committed Nov 19, 2024
1 parent eb7e6fa commit 108083b
Show file tree
Hide file tree
Showing 29 changed files with 232 additions and 167 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.coner.trailer.app.admin.util

import tech.coner.trailer.Club
import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.Event
import tech.coner.trailer.Participant
import tech.coner.trailer.Person
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tech.coner.trailer.eventresults.EventResultsType
import java.nio.file.Path
import java.time.LocalDate
import java.util.*
import tech.coner.trailer.domain.entity.Club

class SubcommandArgumentsFactory(
private val snoozleDir: Path,
Expand Down
2 changes: 2 additions & 0 deletions core-test/src/main/kotlin/tech/coner/trailer/TestClubs.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.coner.trailer

import tech.coner.trailer.domain.entity.Club

object TestClubs {
val lscc = Club(
name = "Local Sports Car Club"
Expand Down
8 changes: 8 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>core</artifactId>

<dependencies>
<dependency>
<groupId>tech.coner.trailer</groupId>
<artifactId>toolkit-validation</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
1 change: 1 addition & 0 deletions core/src/main/kotlin/tech/coner/trailer/Policy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import tech.coner.trailer.eventresults.EventResultsType
import tech.coner.trailer.eventresults.FinalScoreStyle
import tech.coner.trailer.eventresults.PaxTimeStyle
import java.util.*
import tech.coner.trailer.domain.entity.Club

data class Policy(
val id: UUID,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.coner.trailer
package tech.coner.trailer.domain.entity

data class Club(
val name: String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.coner.trailer.domain.validation

import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.toolkit.validation.Feedback
import tech.coner.trailer.toolkit.validation.Severity

sealed class ClubFeedback : Feedback<Club> {

override val severity = Severity.Error

data object NameMustNotBeBlank : ClubFeedback() {
override val property = Club::name
}

data object NameMustNotExceedMaxLength : ClubFeedback() {
override val property = Club::name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.coner.trailer.domain.validation

import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.domain.validation.ClubFeedback.NameMustNotBeBlank
import tech.coner.trailer.domain.validation.ClubFeedback.NameMustNotExceedMaxLength
import tech.coner.trailer.toolkit.validation.Validator

typealias ClubValidator = Validator<Unit, Club, ClubFeedback>

fun ClubValidator() = Validator<Unit, Club, ClubFeedback> {
Club::name { name -> NameMustNotBeBlank.takeUnless { name.isNotBlank() } }
Club::name { name -> NameMustNotExceedMaxLength.takeUnless { name.length <= Club.NAME_MAX_LENGTH } }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.coner.trailer.io.constraint

import tech.coner.trailer.Club
import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.toolkit.konstraints.CompositeConstraint

class ClubPersistConstraints : CompositeConstraint<Club>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tech.coner.trailer.io.mapper

import tech.coner.trailer.Club
import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.datasource.snoozle.entity.ClubEntity

class ClubMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PolicyMapper(
fun toCore(snoozle: PolicyEntity): Policy {
return Policy(
id = snoozle.id,
club = clubService.get(),
club = clubService.get().getOrThrow().getOrNull()!!,
name = snoozle.name,
conePenaltySeconds = snoozle.conePenaltySeconds,
paxTimeStyle = PaxTimeStyle.valueOf(snoozle.paxTimeStyle),
Expand Down
22 changes: 14 additions & 8 deletions io/src/main/kotlin/tech/coner/trailer/io/service/ClubService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tech.coner.trailer.io.service

import arrow.core.Either
import arrow.core.raise.either
import tech.coner.snoozle.db.entity.EntityIoException
import tech.coner.trailer.Club
import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.datasource.snoozle.ClubResource
import tech.coner.trailer.datasource.snoozle.entity.ClubEntity
import tech.coner.trailer.io.constraint.ClubPersistConstraints
Expand All @@ -13,16 +15,20 @@ class ClubService(
private val mapper: ClubMapper
) {

fun get(): Club {
return try {
mapper.toCore(resource.read(ClubEntity.Key))
} catch (notFoundException: EntityIoException.NotFound) {
throw NotFoundException(message = "Club not found.", cause = notFoundException)
} catch (readFailure: EntityIoException.ReadFailure) {
throw ReadException(message = "Failed to read contents of Club from storage.", cause = readFailure)
fun get(): Result<Either<GetFailure, Club>> = runCatching {
either {
try {
mapper.toCore(resource.read(ClubEntity.Key))
} catch (nFE: NotFoundException) {
raise(GetFailure.NotFound)
}
}
}

sealed interface GetFailure {
data object NotFound : GetFailure
}

fun createOrUpdate(
name: String
): Result<Club> = runCatching {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tech.coner.trailer.presentation.di.adapter
import org.kodein.di.*
import tech.coner.trailer.*
import tech.coner.trailer.di.DataSessionScope
import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.io.model.PolicyCollection
import tech.coner.trailer.presentation.adapter.*
import tech.coner.trailer.presentation.adapter.eventresults.*
Expand Down Expand Up @@ -31,11 +32,6 @@ val presentationAdapterModule = DI.Module("tech.coner.trailer.presentation.adapt
bindSingleton { new(::ClassParentModelAdapter) }
bindSingleton<tech.coner.trailer.presentation.library.adapter.Adapter<Class.Parent, ClassParentModel>> { instance<ClassParentModelAdapter>() }

// Club
bind { scoped(DataSessionScope).singleton { new(::ClubModelAdapter) } }
bind<tech.coner.trailer.presentation.library.adapter.Adapter<Club, ClubModel>> { scoped(DataSessionScope).singleton { instance<ClubModelAdapter>() } }
bindSingleton { new(::ClubNameStringFieldAdapter) }

// Event
bindSingleton { new(::EventIdStringFieldAdapter) }
bindSingleton { new(::EventNameStringFieldAdapter) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
package tech.coner.trailer.presentation.di.presenter

import org.kodein.di.*
import tech.coner.trailer.Club
import org.kodein.di.DI
import org.kodein.di.bind
import org.kodein.di.instance
import org.kodein.di.multiton
import org.kodein.di.scoped
import org.kodein.di.singleton
import tech.coner.trailer.di.DataSessionScope
import tech.coner.trailer.presentation.di.constraint.presentationConstraintModule
import tech.coner.trailer.presentation.library.adapter.LoadableItemAdapter
import tech.coner.trailer.presentation.library.presenter.Presenter
import tech.coner.trailer.presentation.library.presenter.PresenterCoroutineScope
import tech.coner.trailer.presentation.library.state.LoadableItem
import tech.coner.trailer.presentation.presenter.club.ClubPresenter
import tech.coner.trailer.presentation.presenter.club.SecondDraftClubPresenter
import tech.coner.trailer.presentation.presenter.club.ClubDetailPresenter
import tech.coner.trailer.presentation.presenter.person.PersonDetailPresenter
import tech.coner.trailer.presentation.presenter.run.EventRunLatestPresenter
import tech.coner.trailer.presentation.state.ClubPresenterState
import tech.coner.trailer.toolkit.presentation.presenter.PresenterCoroutineScope

val presenterModule = DI.Module("tech.coner.trailer.presentation.presenter") {
importOnce(presentationConstraintModule)

// Club
bind {
scoped(DataSessionScope).multiton { arg: Presenter.Argument.Nothing ->
ClubPresenter(arg, instance(), instance(), instance())
}
}
bind {
scoped(DataSessionScope).multiton { initialState: ClubPresenterState ->
SecondDraftClubPresenter(
initialState = initialState,
adapter = tech.coner.trailer.presentation.library.adapter.LoadableItemAdapter(instance())
)
scoped(DataSessionScope).singleton {
ClubDetailPresenter()
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tech.coner.trailer.presentation.model.club

import tech.coner.trailer.domain.validation.ClubValidator
import tech.coner.trailer.toolkit.presentation.model.BaseItemModel
import tech.coner.trailer.toolkit.validation.Validator

class ClubDetailItemModel(
override val initialItem: ClubDetailModel,
private val adapter: ClubEntityModelAdapter = ClubEntityModelAdapter()
) : BaseItemModel<ClubDetailModel, Unit, ClubDetailModelFeedback>() {

override val validator: Validator<Unit, ClubDetailModel, ClubDetailModelFeedback> = Validator {
input(
otherTypeValidator = ClubValidator(),
mapContextFn = {},
mapInputFn = { adapter.modelToEntityAdapter(it) },
mapFeedbackObjectFn = { ClubDetailModelFeedback(it) }
)
}
override val validatorContext = Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tech.coner.trailer.presentation.model.club

import tech.coner.trailer.toolkit.presentation.model.Model

data class ClubDetailModel(
val name: String
) : Model
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tech.coner.trailer.presentation.model.club

import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.domain.validation.ClubFeedback
import tech.coner.trailer.toolkit.validation.Feedback
import tech.coner.trailer.toolkit.validation.FeedbackDelegate
import tech.coner.trailer.toolkit.validation.adapter.propertyAdapterOf

data class ClubDetailModelFeedback(
val source: ClubFeedback
) : Feedback<ClubDetailModel> by FeedbackDelegate(
feedback = source,
propertyAdapter = propertyAdapterOf(
Club::name to ClubDetailModel::name
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tech.coner.trailer.presentation.model.club

import tech.coner.trailer.domain.entity.Club
import tech.coner.trailer.toolkit.presentation.adapter.EntityModelAdapter

class ClubEntityModelAdapter : EntityModelAdapter<Club, ClubDetailModel>() {
override val entityToModelAdapter: (Club) -> ClubDetailModel = {
ClubDetailModel(
name = it.name
)
}
override val modelToEntityAdapter: (ClubDetailModel) -> Club = {
Club(
name = it.name
)
}
}
Loading

0 comments on commit 108083b

Please sign in to comment.