Skip to content

Instantly share code, notes, and snippets.

@iref
Created December 12, 2016 13:42
Show Gist options
  • Save iref/63f787febc57a77bb23bca1efb9f56ce to your computer and use it in GitHub Desktop.
Save iref/63f787febc57a77bb23bca1efb9f56ce to your computer and use it in GitHub Desktop.
final case class UserId(id: Long) extends AnyVal
final case class User(email: String, name: String, passwordDigest: String, createdAt: Instant, updatedAt: Instant)
final case class UserAndId(user: User, id: UserId)
sealed abstract class SignupError extends Product with Serializable
case object EmailAlreadyUsed
final case class Authenticated(email: String, token: Token)
final case class Token(get: UUID = UUID.randomUUID())
object UserRepository {
def save(user: User): ConnectionIO[UserId] = ???
def findByEmail(email: String): ConnectionIO[Option[UserAndId]] = ???
}
object SignupService {
import UserRepository._
def signup(signupForm: SignupForm): ConnectionIO[Either[SignupError, Authenticated]] = {
OptionT(findByEmail(signupForm.email)).map { _ =>
Either.left(EmailAlreadyUsed)
}.getOrElseF {
val newUser = createUser(signupForm)
saveUser(newUser).map { _ =>
Either.right(Authenticated(user.email, Token()))
}
}
}
def createUser(signupForm: SignupForm): User = ???
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment