Created
April 8, 2018 02:33
-
-
Save brianhsu/8a7d8d476cbfb7332882f8a98821e690 to your computer and use it in GitHub Desktop.
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) | |
override def execute(): User = { | |
userRepo.write.insert(user) | |
user | |
} | |
override def journal = Some(InsertLog("user", user.uuid, user, nowTime)) | |
override def validate(): Option[ValidationError] = { | |
import ParamValidator._ | |
val isEmailDuplicated = (email: String) => userRepo.read.find(email).map(_ => IsDuplicated) | |
checkParams( | |
forField("email", request.email, NonEmptyString, EmailValidator, isEmailDuplicated), | |
forField("name", request.name, ParamValidator.NonEmptyString) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment