My controller - kplian/pxp-nd GitHub Wiki
Controller
A controller is a class that extends from a controller we import from the pxp-nd/core module.
Receive a configuration through decorators for example @Route('/product') which is also in pxp-nd/core`
When we extend from the Controller class, we inherit four basic methods: list, add, edit, delete.
For these default methods to work for you, you have to tell it what your model is, for example @Model('shop/Product'), if you don't use your model you can also create your own methods.
import { Controller, Model, Route} from "@pxp-nd/core";
@Route('/products')
@Model ('shop/Product')
class Product extends Controller{
}
export default Product;
Now we are going to test the methods that are created by default in postman:
-
add
Inside postman, we introduce the following path POST
http://localhost:3200/api/shop/products/addthis would be a POST method, and we send a Json in this way.
{
"name": "honey",
"price":"30",
"description": "honey"
}
And the result we get is the following.
{
"name": "honey",
"price": "30",
"description": "honey",
"createdBy": "admin",
"productId": 5
}
-
list
GET http://localhost:3200/api/shop/products/list?start=0&limit=10&sort=name&dir=ASC
We send parameters so that it orders us by name in ascending order.

And the result we get is the following.
{
"data": [
{
"createdBy": "admin",
"userIdAi": null,
"userAi": null,
"modifiedBy": null,
"createdAt": "2021-07-05T15:53:35.120Z",
"modifiedAt": "2021-07-05T15:53:35.120Z",
"isActive": true,
"productId": 2,
"name": "bread",
"description": "bread"
},
{
"createdBy": "admin",
"userIdAi": null,
"userAi": null,
"modifiedBy": null,
"createdAt": "2021-07-05T15:53:35.120Z",
"modifiedAt": "2021-07-05T15:53:35.120Z",
"isActive": true,
"productId": 4,
"name": "cheese",
"description": "cheese"
},
{
"createdBy": "admin",
"userIdAi": null,
"userAi": null,
"modifiedBy": null,
"createdAt": "2021-07-05T15:53:35.120Z",
"modifiedAt": "2021-07-05T15:53:35.120Z",
"isActive": true,
"productId": 3,
"name": "coffee",
"description": "coffee"
},
{
"createdBy": "admin",
"userIdAi": null,
"userAi": null,
"modifiedBy": null,
"createdAt": "2021-07-05T15:53:35.120Z",
"modifiedAt": "2021-07-05T15:53:35.120Z",
"isActive": true,
"productId": 5,
"name": "honey",
"description": "honey"
},
{
"createdBy": "admin",
"userIdAi": null,
"userAi": null,
"modifiedBy": null,
"createdAt": "2021-07-05T15:53:35.120Z",
"modifiedAt": "2021-07-05T15:53:35.120Z",
"isActive": true,
"productId": 1,
"name": "milk",
"description": "milk"
}
],
"count": 5
}
-
edit
PATCH http://localhost:3200/api/shop/products/edit/1 as you can see at the end url we put the productId that we want to edit, In our example the productId = 1 its name is milk and now we will change it to juice.
{
"name": "juice",
}
Now we can see that the productId = 1 has the name of juice.
{
"createdBy": "admin",
"userIdAi": null,
"userAi": null,
"modifiedBy": "admin",
"createdAt": "2021-07-05T15:53:35.120Z",
"modifiedAt": "2021-07-05T21:51:08.330Z",
"isActive": true,
"productId": 1,
"name": "juice",
"description": "milk",
"id": "1"
}
-
delete
DELETE http://localhost:3200/api/shop/products/delete/3
In this example we are going to eliminate the productId=3, clicking on the SEND button will show us in the body what has been eliminated.
{
"createdBy": "admin",
"userIdAi": null,
"userAi": null,
"modifiedBy": null,
"createdAt": "2021-07-05T15:53:35.120Z",
"modifiedAt": "2021-07-05T15:53:35.120Z",
"isActive": true,
"name": "coffee",
"description": "coffee"
}
How to create a controller using pxp-cli
To create a controller using pxp-cli: 1.- have an entity created 2.- create a folder with the name of controllers is where we will save our controllers 3.- With this command we can see all the options that pxp-cli has:
pxp --help
4.- To create a controller we will use the following command.
pxp --controller
To create our controller, it will ask us for the following information:
- Controller name
- Create a default route
- We will select our modules
- Configure default model Entity (yes)
- And finally, we select our entity