Controller-based Associations/Relations in SailsJS - node.js

Say I have a ShoppingCart model with a one-to-many relation to an Item model (i.e. A shopping cart can be full of different items).
I can obviously create a new shopping cart like so:
POST /api/shoppingcart
What I'd like to create is a RESTful API where an item can be added (or updated) to that shopping cart like this:
POST /api/shoppingcart/:shoppingCartId/item
PUT /api/shoppingcart/:shoppingCartId/item/:itemId
From the research I've done so far this doesn't seem built-in to SailsJS. I'd prefer to stay away from manually setting these routes. Is there a way I can automate the route with the ShoppingCartController?

Never mind, found out Sails does it as long as you have the config rest option enabled.
It works with GET, POST, and DELETE methods.
[parentModel]/:parentid/[childModel]/:id?
Check the source: https://github.com/balderdashy/sails/blob/master/lib/hooks/blueprints/index.js#L319

Related

What is the best way to create cart in nodejs,expressjs with hbs?

My question meaning is that what should we use to create cart in e-commerce site in nodejs. Should we direct add the data in mongodb or first use to create session from collected items. What should we do. If the session is the best way to create cart then please suggest me a best way to create cart in sessions thanks
Creating an entry in db would be great because you may need to access user cart at any point of time so instead of managing session, managing db would be good idea.
one more suggestion I would like to give is that when user is checking out the cart then before checkout validate the cart.
suppose, user has added any product to the cart and then does not checkout the cart but after some days, user is checking out the cart and the product in the user cart is out of stock then you need to remove that product from user cart or may be the price of that product is changed then also you need to notify user for this before checkout. This type of validations are included in validate cart.
Hope this helps!
Thank you

Update Mongoose update records with ejs templating

I have an web app with node.js and express with mongoose. I have a candidate profile that has a lot of inputs like user personal (name address etc.), info, parents info and next of kin. I view for the candidate profile.
My question is, what's the best approach to edit the user data?
Do I go the approach of rendering the whole candidate profile and making changes and updating, or I make the option to edit fields individually which will make me have a lot of code for each field, or are there better suggestions.

Programmatically create order in shopware plugin

I need to create ShopWare 6 plugin that creates new orders from products that are received via AJAX POST request. I don't know how to do that. I googled and checked documentation but I couldn't find any info on how can it be done.
Does someone know how can it be done?
In my opinion, the best solution would be to create Cart object from products that you get from request probably you know context token so you can use the current customer's cart and add products to it, but also you can create a new cart and then call \Shopware\Core\Checkout\Cart\Order\OrderPersister::persist() method to persist order from your cart.
Take a look here how to load/create a Cart
\Shopware\Core\Checkout\Cart\SalesChannel\CartLoadRoute::load()

Spring Data Rest Frontend deep linking

So i have been struggling with this one question some time now:
How to handle details Page or deep linking on the Frontend.
So, say, we got a paged collection endpoint with user entities in it and a React App consuming the endpoint.
The flow would be, user authenticates, gets collections, clicks on an item and is either:
Redirected to a new Url say: webapp.com/users/userid
A modal opens with the user details.
Say we got a scenario were two people working with the webapp, Person 1 wants to share a link with Person 2. Person 2 should do some updates on a specific user, which is identified by the link.
The link should be something like : https://www.webapp.com/users/{slug or id}
With Option 2 this functionality is not mappable.
With Option 1 we got to expose the ids in the response to identify the resource, which may work, but we would still need to hardcode the url, as the findById method is not exported as a Uri Template.
So, my Solution would be to add a slug for the resources, implement a search method by the slug, and then get the user, if found, by its self-link.
Sounds like a good solution for me, but on the other hand, I would have to add an extra frontend id(the slug here) which would need to be also unique, to the database model.
So how do you guys handle a problem like this, or is there anybody using spring data rest in this way or in production mode where you have the handle situations like this?
Should mention that this isn’t a primary problem with spring data rest but rather with hateoas itself.
thanks in advance
Florian
You don't need to hardcode URL template. Spring data rest will generate links for each resource.
You can refer to it from front end by some format like: {your_user_object}._links.self.href

How to provide Detail and Summary objects for my REST api with Node.js and Mongoose?

I want to provide some venue information over my node.js REST API build by mongoose and express.
In my model, I should take care of every details for a venue.
But when requested for summary, I should show only some of them.
And If requested for 1 venue in detail, I should show more.
What is the best way to do it?
If you want to achieve something like this why not create some custom queries for your detail and summary actions?
Basically what you need to do.
Create your custom queries that received the custom data.Do this in your model. (e.g the data for summary or details)
Create specific actions for these speficic type of API calls in your controllers and server it as you wish. (E.g create /getVenueDetail/venueId=213235 )

Resources