Updating a particular user in play 2.6.x using slick

in #utopian-io6 years ago

Screenshot (67).png

Repository

https://github.com/playframework/playframework

What Will I Learn?

In this tutorial you will learn the following

  • How to update a user in the database
  • How to create implicit requests
  • adding parameters to functions in routes

Requirements

The following are required in order to properly follow along this tutorial

  • Intellij IDEA
  • sbt
  • playframework with slick installed
  • Web browser
  • Basic knowledge of Scala programming language

Difficulty

  • Intermediate

Tutorial Contents

In today's tutorial we will be looking at how to update a particular user in play 2.6.x using slick. In the previous tutorial I looked how to retrieve a particular user in play using slick. You might be wondering what slick is, slick is simply a relational mapper for scala, it's different from SQL but can still be used in combination with SQL

So let's begin

Creating update queries in our model

We will attempt to create a query that is similar to the following in SQL: UPDATE some_table SET column = some_value WHERE condition. So we will open the UserRepository file found in the app/models package which we created in IntelliJ IDEA in our previous tutorial and type the following code.

 def update (id: Long, name: String) : Future[Int] = db.run {
    people.filter(_.id === id).map(p => (p.name)).update((name))
  }

In the above, we have simply defined a function called update that takes in 2 parameters: the id of the row, while the second parameter is the value of the WHERE clause. When this function is called we will simply insert the id of the row we want to update as the first parameter and the value we want to replace it with as the second parameter. filter works similar to select it selects a particular column and the update function replaces an item in a row with a new one.

Controller Actions with implicit requests

Since we are done with the queries, we can now implement our logic in the controller. In the controller we will attempt to call the update method which defined in our models package and insert the 2 parametersid and name.

def updateUser (id: Int, name: String) = Action.async {
    implicit request =>
      repo.update(id, name).map { _ =>
        Redirect (routes.UserController.index())
      }
  }

In the above code, we have created an implicit request to update the user repo.update(id, name) and redirect Redirect (routes.UserController.index()) back to the index page once the user has been updated in the database. The index page is controlled by theindex method.

Updating values through routes

In the routes we can create GET requests that would allow us to update users in the database by entering values in the web browser. We will enter values like this localhost:9000/updateperson/1/jade, updateperson is the address that calls the updateUser function in the UserController controller, 1 is the id of the row and jade is our replacement value. So the complete code in our routes file will look like this

GET     /updateperson/:id/:name           controllers.UserController.updateUser(id:Int,name:String)

The :id and :name are parameters that we intend to pass into the browser, and theupdateUser() function has to take in 2 parameters here, since they have been defined that way in the controller.

So let's run our code and to do that we will open up command prompt (CMD) in windows, then type cd followed by the file path of your play application. Then type sbt and after it has completed updating the application, type run and press enter, you will get something similar to the screenshot below

Screenshot (80).png

Some users have already been entered before now, let's view the screenshot to view list of users.

Screenshot (81).png

we will now attempt to update a user in a database, I have randomly selected user with id 3, which I will update from bob to Dylan. So to achieve that will type the following address in the web browser localhost:9000/updateperson/3/Dylan, so let's view the changes

Screenshot (82).png

As you can see the name of the user with id of 3 has been changed from Bob to Dylan

Curriculum

Include a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.

Proof of Work Done

The code used for this tutorial can be found here

Sort:  

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • In the images where you have code, put the most visible code. In your images it is difficult to see the code.
  • There are parts of the code that have little explanation, try to explain as much as possible.
  • Put more resources.
  • Enter the professional print screens.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @portugalcoin
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Nice tutorial,@leczy i will appreciate your work here is a some advise from me that you have used pics of coding which will be might difficult for user to understandable, so use coding instead of pics.

using the signature of a utopian moderator is not acceptable!

Hey @leczy
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.16
TRX 0.17
JST 0.029
BTC 69505.86
ETH 2493.71
USDT 1.00
SBD 2.54