Sailsjs overwrite post action in controller - sails.js

Short question, tried finding an IRC for sails for such a quick one but got lost so here goes. I have a controller with a route of '/userposts'. I know sails offers some default REST-like functionality without backend code needed but what if I want to overwrite the default POST action what would I do?
I'm forced to write a POST route such as post /userposts/create or I can overwrite the default action and post straight to /userposts which will identify my overwriting and execute it.
I hope I'm making sense. I basically want to create a custom POST route and be able to
socket.post('/userposts', {title: "Foo", content: "Bar"}, function(response){});
I tried with create but it doesn't get executed on a post to /userposts

Sails.js provides blueprints out of the box.
These blueprints provide you with the following CRUD routes that are enabled by default
/:controller/find/:id?
/:controller/create
/:controller/update/:id
/:controller/destroy/:id
To modify the default functionality for your controllers, look at the settings in config/controllers.js
Your routes are defined within config/routes.js, in your case you have a model UserPosts and a corresponding controller named UserPostsController.
In your UserPosts controller, create a function createPost and specify the route(s) to this method
'POST /userposts/create': 'UserPostsController.createPost'
which is shorthand for
'POST /userposts/create': {
controller: 'userposts',
action: 'createPost'
}
you can also override the /:controller route
'POST /userposts': 'UserPostsController.createPost'
These routes will map any POST requests made to the createPost function.
For more information, be sure to check out the Sails.js documentation

Related

How to get user related data with Generic Interface in OTRS 6?

I wonder how to get User Data with Generic Interface. It seems that there is no Controller to get User Data...
these are the only controllers:
So how can I add user methods to my webservices?
I tried adding a User section in my webservice.yml and reimported it:
User:
Description: Search for User Data
MappingInbound: {}
MappingOutbound: {}
Type: Kernel::System::User
but that didn't work. OTRS says that there is no Controller for Kernel::System::User. I also tried only to set Type to User. Same error.
Since the generic interface replaces the deprecated RPC.pl API, it should have at least the same set of methods.
Otherwise this would'nt be an improvement of the API right?
You can create controller by youself, it's not that hard.
Take a look at existing services in Kernel/GenericInterface/Operation/*
Register new operation with XML, look for examples here: Kernel/Config/Files/XML/GenericInterface.xml
Don't forget to call:
/opt/otrs/bin/otrs.SetPermissions.pl (as root)
/opt/otrs/bin/otrs.Console.pl Maint::Config::Rebuild
after creation of new files

rest routes deciding update vs add

I'm writing a web app with node/express and I'm trying to set up some restful routes. Basically I have some generic items and I have a page that has a list of these items. so I've set up the following route:
router.get('/items')...
I'm currently setting up add/update items as well, but I'm not sure if I should set up a PUT for add and POST for update, or use POST for both? I've read that POST is acceptable for both add/update, but if I use post for add and update, then I have to use the same route, is this correct? Which would mean I have to pass back some sort of 'action' parameter to tell the route what action to take.
is this a situation where I should use PUT and POST separately?
You can use post to do both the insert and update with a url pattern like this
POST -> items/ -- add an item
POST -> items/{itemId} -- updates the given item with the id itemId
Refer this for a more detailed description
https://stackoverflow.com/a/630475/381407

Where to put custom advanced queries in a sailsjs mongodb stack

I want to proceed with more advanced queries in a sailsjs and mongodb stack where sailsjs is setup to serve as an api with data against a front end client. I've been able to fetch data with some basic queries but now looking into on how to proceed with more advanced ones. For example, I want to query the database for entries where the string match either the title or the text, something like this,
db.mycol.find({$or:[{"text":/.*test.*/},{"title": /.*test.*/}]})
My question, where do I put this logic? Any hints, links, tutorial that could point me into the right direction for this would be appreciated.
If you generate an api, for example:
sails generate api Customer
Sails will create a CustomerController for you in api>controllers.
You can add whatever custom endpoints you want to that.
If I put in CustomerController
blah: function(req, res) {
res.json(200, 'You are at blah');
}
and I navigate to customer/blah, it will hit that endpoint. So you can add whatever custom endpoints you want to for that controller. Go crazy.
Do not forget that sails has policy enforcement that you need to set up in the config/policies.js. This lets you expose, block, or add whatever middleware you need to in order to keep your back end as secure or open as it needs to be.

Can grails application have RestController and normal controller (for GSP) for same domain class

Recently I need to create REST API for existing grails application.
I am thinking that is it really possible to have both of the controllers (Normal and Restful) for same domain class in one single grails application?
Which controller will handle the request if make a HTTP GET request?
Please discuss your thoughts or if it is possible at all please guide me how.
We can define a new Controller to handle to REST API calls. e.g. In my app I have User as Domain Class and have UserController which return the data to GSP pages. I wanted to add REST API support (unfortunately) in the same app and I don't wanted to deal with mess it is already there in UserController. So I added new Controller UserRestController which will specifically handle the REST API calls and following mapping in UrlMappings.groovy which now works fine. Thanks to #codehx for helping me on this.
"/api/users"(controller: "userRest", parseRequest: true) {
action = [GET: "list", POST: "save" }
"/api/users/$id"(controller: "usersRest", parseRequest: true) {
action = [GET: "show", PUT: "update", DELETE: "delete"] }
Which controller will handle the request if make a HTTP GET request?
As far as it is not possible to have two controllers with same name in grails app this will not be confusing.
Just use two different names for Normal controller and for your RESTFUL controller, so obviously the URL for accessing the two urls will be different.

How to configure Sails Controller to use only 'Post' method

I saw that all controllers methods are free for GET and POST. How to ensure to permits only POST for some methods?
If you are using action blueprints to automatically route URLs to custom controller action, then those actions will respond to GET, PUT, POST, DELETE and PATCH methods by default. If you'd rather control which methods are allowed, you have a few choices:
Disable certain methods using custom routes in your config/routes.js file. For example, if you have a foo action in UserController.js that you don't want to allow GET requests for, you can add the following custom route:
"GET /user/foo": {response: 'forbidden'}
to automatically route it to the "forbidden" response (same as doing res.forbidden() in a controller)
Test req.method within the action itself, and return early for methods you don't want to process:
if (req.method.toUpperCase() == 'GET') {return res.forbidden();}
Disable action routes by setting actions to false in your config/blueprints.js file. You'll then have to set up all your routes manually in your config/routes.js file.