Step 1 : Design your schemaLet us decide upon the user schema first . A Simple User schema would look like this
We will now make use of evolutions to create the table for the above schema
Create a new package evolutions.default under the conf directory. Now add a new file with name “1.sql” that contains the evolution script as below
Step 2 : Define a User Model for the above schema design
Create model package under app folder .
Now create a User.scala class in the model package (app>model>User) and define a case class like below
Step 3 : Implement the data access layer(app.dao)
Add a new package dao inside the app folder.
Add UserDAO trait inside the dao package , with all the required User CRUDs
Implement the UserDAO trait using UserDAOImpl class .
Play uses Google Guice DI framework to handle Dependency Injection . Guice provides an out-of-box implementation that helps to automatically inject required objects at run time .
Since the database is configured using PlaySlick , we can obtain the database configuration inside a dao class using DatabaseConfigProvider trait provided by the PlaySlick module .
Now describe a database schema with Table row classes for the tables and implement the abstract CRUD methods
Now bind the trait to its implementation using Guice . The simplest way to do this using guice is by adding @ImplementedBy annotation to the trait. So your final dao trait should look like
So , to provide an implicit execution , you can do it in two ways :Import the implicit context using the import statement
OR Pass an additional parameter to your class definition
Step 4 : Implement Service Layer( app.service)
Add a new package service inside the app folder.
Add UserService trait inside the service package , with all the required User Service Methods
Implement the UserService trait using UserServiceImpl class .
Now again bind the trait to its implementation .
Step 5 : Implement the View Layer(app.views)
In HTML, the data is usually submitted through html forms . So we need to define a form structure for our User case class to help play handle to the form data in an easy and well-defined manner, with the help of play-forms
Create a userForm for the corresponding User case class that we define above
Create a html file (user.scala.html) inside the views package. Pass the empty userform to be displayed on the html page . The implicit Request header and messages are provided to support Internationalization
Add a new file messages in the conf package. And add these lines into the file
Step 6 : Implement the Controller Layer (app.controller)
Add the UserController inside the controller package . It should look like below.
Now the final step will be to add the corresponding paths in routes(conf.routes)
Thats it !! We are done with the implementation of User Registration. Run the application to add and manage users
Open localhost:9000 in your browser
Click “Apply this script now !” button and that should create your required table in the given database