简体   繁体   中英

How do I add a new controller in front of a nested controller so that users don't have to issue two requests to API?

I have created a Rails 5 API with two controllers, one nested within the other one, such that in order to create a new Deployment, you have to create a Template first, and get it's ID. ie POST /templates//deployments. I did this this way because a Template is a static object that should never change over many months, but a deployment is a changing thing that is potentially new every time. A deployment is something you do with a template, therefore you shouldn't be able to create a deployment unless a template already exists.

I spoke with the developers who will use this service, and they complained about having to use two requests to make a Deployment. I argued for my design, but I failed to convince them. I am a junior in the organization, and I don't have the power to make them do what I want.

Therefore, I need to create a third controller, called QuickDeployment, that accepts a params hash containing params for both the template and the deployment. If the template already exists, it should use it, otherwise it should create an new one. It should create a new deployment regardless. A deployment is an object with a state machine and workflow - users should be able to get the current state of the deployment with a GET /quick_deployments/.

I'm wondering if that third controller needs to have a model which would have references to the template and deployment is created, or not? I want to avoid if I can repeating myself with respect to the parameters and behaviors of the templates and deployments associated with the QuickDeployment.

So do I create a QuickDeployment model, or not? Can I accomplish the same thing without creating the model, using routing or something?

I'm wondering if that third controller needs to have a model which would have references to the template and deployment is created, or not?

No. Your new controller would basically extract your template parameters from the POST request, create the template record, then create a deployment record which belongs to the newly created template with the deployment parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM