Play Framework, REST, Routes, and Controllers

j3d

I've read some interesting tutorials about RESTful API design and the concepts behind it is quite clear... but let's put it into practice with Play now.

Suppose we want to implement a RESTful API that provides functionality for dealing with users. Let's start with the model. Here is the Address class:

case class Address(
  id: Int,
  street: String,
  zip: String,
  city: String,
  country: String
)

... here the User class:

case class User(
  id: Int,
  email: String,
  firstName: String,
  lastName: String,
  addresses: Array[Int]
  // addresses: Array[Address] would this option be better?
)

... and finally the routes:

# Creates a new user
POST   /users                     controllers.users.create

# Gets the user identified by the specified id
GET    /users/:userId             controllers.users.find(userId)

# Modifies the user identified by the specified id
PUT    /users/:userId             controllers.users.update(userId)

# Deletes the user identified by the specified id
DELETE /users/:userId             controllers.users.delete(userId)

The first question is: how do I retrieve an user by email keeping my API complaint with the REST rules? The following wouldn't work because it conflicts with GET users/:userId:

# Gets the user identified by the specified email address
GET    /users/:email              controllers.users.findByEmail(email)

The two options I've in mind so far are:

GET    /users                     controllers.users.list(Option[email])

or

GET    /users/:email/xxx          controllers.users.findByEmail(email)

where xxx should be a kind of virtual resource. Any suggestion for that?

My second and last question is: how should I manage user addresses? Should I get an User, add the new Address to User.addresses, and then update the User with PUT?

PUT    /users/:userId             controllers.users.update(userId)

... or should I create a specific controller for managing user addresses like this?

POST   /users/:userId/addresses/  controllers.addresses.create(userId)

Personally I prefer the second option... but maybe there are better ones.

Vidya

The first question is: how do I retrieve an user by email keeping my API complaint with the REST rules?

I would say simply GET /users/email/:email controllers.users.findByEmail(email) which returns a 200 if a user with the given E-mail exists and a 404 otherwise.

I can see the attraction, but I would arm wrestle @Robin on his idea. It is technically feasible, but it doesn't feel RESTful to me because of my interpretation of the Identification of Resources element of REST. Also, merging two or more possibilities for identifiers to be disambiguated on the server strikes me as brittle code that will force me to work on weekends eventually because user identifiers will come and go as requirements change--forcing me to modify the controller endpoint over and over. I may also be biased by seeing canonical method names like findByName, findByEmail, etc. but never findByYourGuessIsAsGoodAsMine.

My second and last question is: how should I manage user addresses? Should I get an User, add the new Address to User.addresses, and then update the User with PUT?... or should I create a specific controller for managing user addresses like this?

It's clear that the user is the interesting resource rather than an email (Note I am using email where you use address to distinguish from physical address in my mind). My routes would look something like users/:userId/emails/ as you have essentially and to manage them in the Users controller. I see no reason--from either software engineering perspective or a ideological REST perspective--to add the overhead of an extra class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Slim Framework: routes and controllers

From Dev

Testing Controllers In Play Framework

From Dev

Testing Controllers In Play Framework

From Dev

Play framework routes

From Dev

How to organise controllers for REST relationships? [Laravel routes]

From Dev

play framework - bind enum in routes

From Dev

Scala backticks in Play Framework Routes

From Dev

Domain routes in Play Framework 2.3

From Dev

Play Framework Error in Routes file

From Dev

Unit testing controllers with ScalaTest in Play! framework

From Dev

Unit Test controllers in Play 2 framework with Scala

From Dev

InstantiationException for controllers in Play Framework after upgrading IntelliJ

From Dev

Unit Test controllers in Play 2 framework with Scala

From Dev

Play framework many controllers (and session management)

From Dev

scala play framework reverse routing and controllers

From Dev

Non-capturing wildcards in Play Framework routes

From Dev

Using POST routes parameters in Play Framework

From Dev

Iterating all Play Framework routes in Test

From Dev

split routes in multiple files for play framework 2.2

From Dev

Long dynamic routes in play framework 2

From Dev

Play Framework @routes.Assets.at Compilation Error

From Dev

Play framework - call controller in routes with Class parameter

From Dev

Play framework routes, and scala predef values

From Dev

Play Framework 2.3.4 routes redirection to subproject

From Dev

Non-capturing wildcards in Play Framework routes

From Dev

Using POST routes parameters in Play Framework

From Dev

Play framework - call controller in routes with Class parameter

From Dev

How to "rewrite" urls or routes with Play Framework 2

From Dev

Play Framework 2.3.4 routes redirection to subproject

Related Related

  1. 1

    Slim Framework: routes and controllers

  2. 2

    Testing Controllers In Play Framework

  3. 3

    Testing Controllers In Play Framework

  4. 4

    Play framework routes

  5. 5

    How to organise controllers for REST relationships? [Laravel routes]

  6. 6

    play framework - bind enum in routes

  7. 7

    Scala backticks in Play Framework Routes

  8. 8

    Domain routes in Play Framework 2.3

  9. 9

    Play Framework Error in Routes file

  10. 10

    Unit testing controllers with ScalaTest in Play! framework

  11. 11

    Unit Test controllers in Play 2 framework with Scala

  12. 12

    InstantiationException for controllers in Play Framework after upgrading IntelliJ

  13. 13

    Unit Test controllers in Play 2 framework with Scala

  14. 14

    Play framework many controllers (and session management)

  15. 15

    scala play framework reverse routing and controllers

  16. 16

    Non-capturing wildcards in Play Framework routes

  17. 17

    Using POST routes parameters in Play Framework

  18. 18

    Iterating all Play Framework routes in Test

  19. 19

    split routes in multiple files for play framework 2.2

  20. 20

    Long dynamic routes in play framework 2

  21. 21

    Play Framework @routes.Assets.at Compilation Error

  22. 22

    Play framework - call controller in routes with Class parameter

  23. 23

    Play framework routes, and scala predef values

  24. 24

    Play Framework 2.3.4 routes redirection to subproject

  25. 25

    Non-capturing wildcards in Play Framework routes

  26. 26

    Using POST routes parameters in Play Framework

  27. 27

    Play framework - call controller in routes with Class parameter

  28. 28

    How to "rewrite" urls or routes with Play Framework 2

  29. 29

    Play Framework 2.3.4 routes redirection to subproject

HotTag

Archive