Sometimes the desired result of a POST call is not just the execution of a bounch of SQL queries we can put in the transactions section of a json resource. Sometimes we need to implement some logic and in those cases we need the power and the freedom given by a programming language.
In order to give this opportunity have created what I call a use case. A use case is a class containing a method that takes paramters as input, perform operations and gives back output.
A use case class is a subclass of BaseUseCase and implement the needed logic in the performAction method.
The base for implementation looks like the following code.
In order to link th use case class to application we need to add this to the post section of a json resource.
As you can see we can link not one but many usecases to a resource post section, what we need to do is name the class name we want to call and pass the parameters. The parameters listed will be passed to the class in the useCaseParameters assocative array.
Let’s make an example:
We need to create a form that link an author, already in the database to a book, but befor to do that we need to be sure the author is not already linked.
This is the complete script:
The form is implemented at the start as usual, but you can see that as use case is part of the post section passing tow paremeter received as POST parameters.
The use case class will be implemented as following: