package controllers import play.api._ import play.api.data._ import play.api.data.Forms._ import play.api.mvc._ import play.api.db.slick._ import play.api.db.slick.Config.driver.simple._ import play.api.Play.current import scala.concurrent.Future import models._ case class SignupData(username: String, email: String, password: String, passwordCheck: String, firstName: String, lastName: String, country: String, postalCode: String, address: String, phone: String, birthdate: java.sql.Date) case class ProfileData(email: String, password: String, passwordCheck: String, country: String, postalCode: String, address: String, phone: String) object Profile extends Controller { val signupForm = Form( mapping( "username" -> nonEmptyText(minLength = 3, maxLength = 20), "email" -> email, "password" -> nonEmptyText(minLength = 5, maxLength = 255), "passwordCheck" -> nonEmptyText(minLength = 5, maxLength = 255), "firstName" -> nonEmptyText(minLength = 1, maxLength = 255), "lastName" -> nonEmptyText(minLength = 1, maxLength = 255), "country" -> nonEmptyText(minLength = 2, maxLength = 2), "postalCode" -> nonEmptyText(minLength = 4, maxLength = 20), "address" -> nonEmptyText(minLength = 5, maxLength = 255), "phone" -> nonEmptyText(minLength = 5, maxLength = 20), "birthdate" -> sqlDate )(SignupData.apply)(SignupData.unapply) .verifying("Password mismatch", fields => fields match { case profileData => checkPasswordCoherence(profileData.password, profileData.passwordCheck) }) .verifying("Username already registered", fields => fields match { case profileData => !checkUsernameUse(profileData.username) }) .verifying("Email address already in use", fields => fields match { case profileData => !checkEmailUse(profileData.email) }) ) val profileForm = Form( mapping( "email" -> email, "password" -> text(maxLength = 255), "passwordCheck" -> text(maxLength = 255), "country" -> nonEmptyText(minLength = 2, maxLength = 2), "postalCode" -> nonEmptyText(minLength = 4, maxLength = 20), "address" -> nonEmptyText(minLength = 5, maxLength = 255), "phone" -> nonEmptyText(minLength = 5, maxLength = 20) )(ProfileData.apply)(ProfileData.unapply) .verifying("Password mismatch", fields => fields match { case profileData => checkPasswordCoherence(profileData.password, profileData.passwordCheck) }) .verifying("Email address already in use", fields => fields match { case profileData => !checkEmailUse(profileData.email) }) ) def checkPasswordCoherence(p1: String, p2: String) = { p1 == p2 } def checkUsernameUse(username: String) = DB.withSession { implicit session => Tables.Users.filter(_.username === username).length.run > 0 } def checkEmailUse(email: String) = DB.withSession { implicit session => Tables.Users.filter(_.email === email).length.run > 0 } def signup = Auth { implicit request => if (request.account.nonEmpty) { Redirect(routes.Application.index()) } else { Ok(views.html.pages.auth.signupForm(signupForm)) } } def signupSubmit = Auth { implicit request => if (request.account.nonEmpty) { Redirect(routes.Application.index()) } else { DB.withSession { implicit session => signupForm.bindFromRequest.fold( formWithErrors => { BadRequest(views.html.pages.auth.signupForm(formWithErrors)) }, validForm => { val users = Tables.Users returning Tables.Users.map(_.uuid) val uuid = users += Tables.User( username = validForm.username, email = validForm.email, userPassword = validForm.password, creationDate = new java.sql.Timestamp(new java.util.Date().getTime), firstName = validForm.firstName, lastName = validForm.lastName, countryCode = validForm.country, postalCode = validForm.postalCode, address = validForm.address, phone = validForm.phone, birthdate = new java.sql.Timestamp(validForm.birthdate.getTime) ) Redirect(routes.Application.index()) .withSession(Security.username -> uuid) .flashing("success" -> "You are now registered. Welcome! You may now spend your money.") } ) } } } def viewProfile(userUuid: String) = TODO def editProfile = TODO }