aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/Profile.scala
blob: 0009628c091bb33cfcbaf1e9f12ca9cc6ba35be3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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

}