Created
November 16, 2015 12:36
-
-
Save alexbatashev/c24d47a18bfc22d0a04a to your computer and use it in GitHub Desktop.
application.conf
This file contains 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
logger.scala.slick.jdbc.JdbcBackend.statement=DEBUG | |
slick.dbs.default.driver= "slick.driver.MySQLDriver$" | |
slick.dbs.default.db.driver="com.mysql.jdbc.Driver" | |
slick.dbs.default.driver="com.mysql.jdbc.Driver" | |
slick.dbs.default.url="mysql://127.0.0.1/playdb" | |
slick.dbs.default.username=root | |
slick.dbs.default.password="123" |
This file contains 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 controllers | |
import models._ | |
import play.api.data._ | |
import play.api.data.Forms._ | |
import play.api.mvc._ | |
import play.api.Play.current | |
class Login extends Controller { | |
val usersDAO = new UsersDAO | |
val registerForm = Form( | |
mapping( | |
"name" -> nonEmptyText, | |
"middlename" -> text, | |
"surname" -> nonEmptyText, | |
"email" -> email, | |
"password" -> nonEmptyText(minLength = 6) | |
)(RegisterUser.apply)(RegisterUser.unapply)) | |
val loginForm = Form( | |
mapping( | |
"email" -> email, | |
"password" -> nonEmptyText | |
)(LoginUser.apply)(LoginUser.unapply) | |
) | |
def index = Action { | |
Ok(views.html.login()) | |
} | |
def register = Action { | |
Ok(views.html.register()) | |
} | |
def doLogin = Action { implicit request => | |
loginForm.bindFromRequest.fold( | |
formWithErrors => { | |
Redirect(routes.Login.index) | |
}, | |
userData => { | |
val user = usersDAO.getUserByEmail(userData.email) | |
Ok("") | |
} | |
) | |
} | |
def doRegister = Action { implicit request => | |
registerForm.bindFromRequest.fold( | |
formWithErrors => { | |
Ok(views.html.register()) | |
}, | |
userData => { | |
User.createUser(userData) | |
Redirect(routes.Application.index()) | |
} | |
) | |
} | |
} |
This file contains 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 models | |
import javax.inject.Inject | |
import play.api.Play | |
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfig, HasDatabaseConfigProvider} | |
import slick.driver.JdbcProfile | |
import slick.driver.MySQLDriver.api._ | |
import play.api.inject.Injector | |
import scala.concurrent.Future | |
class UsersDAO extends HasDatabaseConfig[JdbcProfile] { | |
protected val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current) | |
import driver.api._ | |
class UsersTable(tag: Tag) extends Table[User](tag, "users") { | |
def id = column[Long]("id", O.PrimaryKey, O.AutoInc) | |
def name = column[String]("name") | |
def middlename = column[String]("middlename") | |
def surname = column[String]("surname") | |
def email = column[String]("email") | |
def password = column[String]("password") | |
def salt = column[String]("salt") | |
def role = column[String]("role") | |
def * = (id, name, middlename, surname, email, password, salt, role) <> | |
((User.apply _).tupled, User.unapply) | |
def email_idx = index("users_email_idx", email, unique = true) | |
} | |
object Users extends TableQuery(new UsersTable(_)) | |
def getUserByEmail(email: String): Future[Option[User]] = { | |
dbConfig.db.run(Users.filter(u => u.email === email).result.headOption) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment