This file contains hidden or 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
| abstract class UseCaseExecutor { | |
| def execute[T](useCase: UseCase[T]): Try[T] = { | |
| Try { | |
| useCase.validate().foreach { error => throw error } | |
| val result = useCase.execute() | |
| useCase.journal.foreach(appendJournal) | |
| result | |
| } |
This file contains hidden or 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
| object UseCaseExecutor { | |
| type Presenter[T, R] = (Try[T] => R) | |
| } | |
| abstract class UseCaseExecutor { | |
| import UseCaseExecutor.Presenter | |
| def execute[T, R](useCase: UseCase[T])(presenter: Presenter[T, R]): R = { |
This file contains hidden or 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
| object InMemoryUser { | |
| def makeInMemoryUser: UserRepo = { | |
| val inMemory = new InMemoryUserReadable with InMemoryUserWritable | |
| UserRepo(inMemory, inMemory) | |
| } | |
| trait InMemoryUserBase { | |
| protected var userList: List[User] = Nil | |
| } |
This file contains hidden or 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
| case class UserRepo(read: UserReadable, write: UserWritable) | |
| trait UserReadable { | |
| def find(email: String): Option[User] | |
| } | |
| trait UserWritable { | |
| def insert(user: User): Unit | |
| } |
This file contains hidden or 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
| trait DynamicDataGenerator { | |
| def randomUUID: UUIDGenerator | |
| def currentTime: CurrentTimeGenerator | |
| } | |
| trait UUIDGenerator { | |
| def uuid: UUID | |
| } | |
| trait CurrentTimeGenerator { |
This file contains hidden or 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
| object CreateUser { | |
| case class Request(email: String, name: String) | |
| } | |
| class CreateUser(request: CreateUser.Request) | |
| (implicit val userRepo: UserRepo, implicit val generator: DynamicDataGenerator) extends UseCase[User] { | |
| private lazy val uuid = generator.randomUUID.uuid | |
| private lazy val nowTime = generator.currentTime.time | |
| private lazy val user = User(uuid, request.email, request.name, nowTime, nowTime) |
This file contains hidden or 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
| trait UseCase[T] { | |
| def execute(): T | |
| def validate(): Option[ValidationError] | |
| def journal: Option[Journal] | |
| } |
This file contains hidden or 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
| trait Entity | |
| case class Stuff( | |
| uuid: UUID, userUUID: UUID, | |
| title: String, description: String, | |
| createTime: LocalDateTime, updateTime: LocalDateTime, | |
| isTrash: Boolean = false) extends Entity | |
| case class User( | |
| uuid: UUID, email: String, name: String, |
This file contains hidden or 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
| class CreateUserSpec extends fixture.WordSpec with Matchers with OptionValues { | |
| "CreateUser" when { | |
| "validate request" should { | |
| "return an exception if email is empty" in { fixture => | |
| pending | |
| } | |
| "return an exception if email is effectively empty" in { fixture => |
This file contains hidden or 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
| package moe.brianhsu.gtd.usecase | |
| import moe.brianhsu.gtd.journal.Journal | |
| import scala.util._ | |
| object UseCaseExecutor { | |
| type Presenter[T, R] = (Try[T] => R) | |
| } |