As you know, service object is used to create data for multiple domains, or connect 3rd party APIs in one procedure.
So in this case, resource interface and domain model are not exactly matched.
For example, you want to create Subscription.
The procedure is following,
create User
create choose the Plan
create Subscription (connecting third party API)
(If something fails, the rollback occurs.)
The business logic is across more than one domain.
So I could do POST with those JSON data.
{"plan_id": 1,
"user_info":{"uuid": "644e1dd7-2a7f-18fb-b8ed-ed78c3f92c2b",
"name": "John Doe",
"email": "john#gmailcom"},
"user_card_info": {"object":"card",
"exp_month": 12,
"exp_year": 2020,
"number": 4242424242424242}
}
In this case, how should I handle this situation in JSON API.
Maybe Should I create abstract resource?
Also How can I handle this within Controller in JSON API?
To do this in the front end, you would essentially need to duplicate rails validations for every single field, however it is much simpler to do this in rails within the controller action.
Firstly, all relevant models need to have the proper validations (generic and custom if necessary). This way, when you create an invalid object, you can catch the issue. Then, there's a class method called #transactions. I've listed the documentation below. This allows you to insert multiple records at once and rollback if any of them fail which should solve your issue. Be sure to use the #save! method over #save because the latter fails silently. Hopefully this was helpful :)
http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
Related
Would someone be able to direct me to the correct API that I could use to mark a Student Subject and Course to ‘Complete’?
I found out ‘Grade.CourseCompletion’ API could serve the purpose, but not sure if that's correct understanding.
Also, where could I find below highlighted fields and how I can change their value?
"OrgUnitId": <number:D2LID>,
**"CompletionId": <number:D2LID>,**
"UserId": <number:D2LID>,
**"CompletedDate": <string:UTCDateTime>,**
"ExpiryDate": <string:UTCDateTime>|null
Thanks
Vivek
You cannot change a CompletionId; it's an entity identifier for the completion record that gets created by Brightspace when the completion record gets created. (I believe you could, however, delete a completion record and create a new one.)
You use the POST and PUT routes for course completion to create new (or update existing) course completion records. The JSON structure you provide when you do a create or update operation allows you to specify a CompletedDate.
I would also point out that D2L has a developer-specific community to support clients and partners and you may find that answers to your questions are more timely there.
If I create models for request and response parameters for every http request in iOS project, it is easier for me to deal with models rather than dictionaries.
However, it will create too many models.
Is this a good approach or not?
And during the development, I found if there is only one model, for example, in an online shopping system, I only have one store model. But I use it in store module, cart model and order model. Actually in each module, the different attributes of the store model has been used. So this giant model always has some extra attributes for usage in each module.
Is this a good approach to manage the models in iOS projects? Or should I create CartStore model, OrderStore model?
To me, you should create models for different flows/modules to adapt Single Responsibility Principle.
From your shoes, the Store model should only contain the attributes of the store, if Cart or Order module need any further attributes then you should create another model.
Hope this helps.
The model approach will save a ton of your time when come back to fix or add on a new feature. Sure, They will take quite time but today Apple has already provide code codable/decodable protocol which will help you to handle parsing between JSON and Object Model. Also writing a unit test or even integration test it will be much easier for you.
Base on which data response of request, if have the same data structure should only create 1.
Ex: api get all stores and api get store with condition (distance, rating, ...) return storeModel, then we just using only 1 storeModel. And we have api get store detail (this api return more info than api get list stores above) just add more property in storeModel and using it again. So that with 3 apis, we only using storeModel
With your example, I think should create StoreModel, CartModel and OrderModel inherits from BaseModel. In this BaseModel, we'll have the all property which StoreModel, CartModel and OrderModel have, and in each children model, we will have only specific property.
Hope this help!
I am developing a restful API. My create Reservation method requires parameters like data, location etc. It also requires a list of People. Those people have name, birthdate etc.
So to pass the people to the API I need to send a nested object to the API. This is something that I usually don't see out there in the API world.
So my question is: Are there any problems with this and should I somehow flatten the parameter object or is it no problem at all to accept a nested object as parameter?
There will be no problems with this but usually its not preferable because consumer of API need to understand the nested JSON and construct while calling. API having simple request JSON and syntax is always preferable.
This type of strategy is totally linked to modeling and coupling with the information-producing system.
In my opnion, if the resources can be structured sequentially as child resources, in this way the navigation and resuabilidad becomes more interesting. To minimize the number of calls a design pattern API Gateway with agragation is being pretty much in today by corporations.
Example:
POST http://api.teste.com/clients/ --> This is going to create a new client resource and returning the new client identification
POST http://api.teste.com/clients/1/addresses
In a rails 3 app, I have a model with a boolean attribute called "archived". This attribute is accessible in the cms, where I can go in and edit individual objects, and a checkbox where I can check if an object is archived or not. However, if this object is archived, I don't want this object to be accessible on the api level. That includes the object itself, and its associated objects. At the same time I would like the object to be accessible in the cms, to edit other details and set the archived value back to false, so that I can access the object again in the api and change its behaviour back to default.
I would really appreciate suggestions on how to approach this from a design/high level perspective. Thank you for your help.
When you say "api" do you mean:
Public RESTful API
Access to those models in your own code. Like finder methods on your model?
For the first case, simply get only objects in required state (Model.where(archived: false)).
In the second case, taking into account that Ruby is very "open" language, it would be impossible to limit access to any of object property or method. However, to build a public API for other team members to use, you can add methods that correctly filter out your models (look into scope).
As a second alternative to Model.where(archived: false)
If you used a status enumerator instead of a boolean, you would be able to restrict the list using Model.archived, or Model.published... All you need is an integer field for the model's db table, and something like this in the model:
enum status: { dev:0, published: 1, archived: 2}
Then in your API, you define what status segment you want to restrict it to as shown above. The enumerator system in RoR is a little tricky to get the hang of at first, but very powerful once you get it.
I followed this examples http://framework.zend.com/manual/2.2/en/user-guide/database-and-models.html to create a model and a way to save it to a database.
But I don't like the idea of using the AlubumTable-class in my controller as I think this creates too much dependencies. I just want to add the save(), fetchAll(), etc. methods to my model so that I don't have to care about how to save my models inside my controller.
If I want to change the way my models get stored e.g. from a database to a REST-service I would have to rewrite every part of my controller where I get or store models instead of just changing the save() etc. methods in my model.
Are there any tutorials for my way or is this just a stupid idea? :)
The concern you have is actually OK, but you have to realize that the AlbumTable is nothing but a layer between your Controller and the Database. The AlbumTable actually is the one thing with the dependency, not the controller.
The Controller will always need some sort of "Service" or "Gateway" (which would be AlbumTable) to get access to the Data from the DB.
Also i do not understand what you mean by "i want to change the way my models get stored" - You should always store the MODEL into your Service. In the given example the Model is Album and the Service is AlbumTable. No matter where the data comes from - REST, RPC, "normal HTTP", you would always store the Album and not some ArrayData or whatnot. You'd rather try to implement a function inside your model like exchangeArray(), exchangeJson().
You may want to make your "problem" more clear to us...