What is the proper way to handle unique id in a REST architecture? - api

My application is two parts :
an android app that lets the user creates comments on stuff. Each comment has a unique id (a string)
a server with a REST api (json based) that gathers the data. The server generates the unique id.
The android app creates a new comment by POSTing the comment data to the server, minus the unique id (it gets generated by the server upon the comment creation).
The unique id will be used in the subsequent requests (For example : <myserver>/api/v1/comment/<id>)
The question is after the POST, the server needs a way to tell the android app what the id of the newly created comment is.
What is the REST way to do get the new id ?
Note : I would like to avoid to create an extra call to get a new identifier and then to post my comment.

https://www.rfc-editor.org/rfc/rfc2616#section-10.2.2
You respond with a 201 Created status and a Location header containing the URI to the created resource.

just reply to the POST with the id

Related

reactjs: material-table: how to do serverside validation when adding or editing

I am planning to use material-table. Its a simple table with a column name
I have seen some tutorials how to do CRUD operation using material-table.
But i want to check the name is unique from server side, not on client side.
and pass the error if the name is not unique.
Can we do these things with material table

How to block other data in ASP.NET Core 5.0

I have a project which contains Company and Agency users.
I have a problem while I'm trying to show their data. For example: the agency can see other agency's data if they change's the value on the web browser searchbar. I want to block that vulnerability but I don't know how to do that.
I will be doing that first time so, thanks for any suggestions!
As I Understood from you in the comments the Term SearchBar
you mean by it , the URL place in the browser.
As my best answer would be to add a Guid in the Company Model and map it to a uniqueidentifier in the sql server database, and when the value is inserted you generate a uniqueidentifier for this company.
then you start getting the company by that object key
so that final product will be something like this :
http:///companyinfo?id=DE653F58-AB12-43F9-95CD-A7C3A856340A

Does surveymonkey api return the unique ID associated with their unique id specifications?

Can anyone tell me if they are know for certain whether or not survey monkey's api has the ability to return the unique ID associated with their designated unique id specifications:
http://help.surveymonkey.com/articles/en_US/kb/Can-I-track-respondents-using-a-unique-ID
It states the following regarding viewing this in the aforementioned link:
When these results come back on the survey, that custom ID of "00001"
appears in the Custom Value field in the Analyze > Browse Responses
section.
However I cannot find any mention of this data being returned from their API in the API documentation:
https://developer.surveymonkey.com
It can be retrieved by sending 'custom_id' as a requested field to 'get_respondent_list':
https://developer.surveymonkey.com/mashery/get_respondent_list
It will then be in the 'custom_id' field in the respondent's dictionary.

Include count of linked records in OData result

I've got a table "Events" with a linked table "Registrations", and I want to create an OData service that returns records from the Events table, plus the number of registrations for each event. The data will be consumed client-side in JavaScript, so I want to keep the size of the returned data down and not include all linked registration records completely.
For example:
ID Title Date Regs
1 Breakfast 01.01.01 12:00 4
2 Party 01.01.01 20:00 20
I'm building the service with ASP.NET MVC4. The tables are in an MSSQL database. I am really just getting started with OData and LINQ.
I tried using the WebAPI OData system first (using classes of EntitySetController) but was getting cryptic server errors as soon as I included the Registrations table in the entity set. ("The complex type 'Models.Registration' refers to the entity type 'Models.Event' through the property 'Event'.")
I had more success building a WCF OData system, and can request event information and information on related registrations.
However, I have no clue how to include the aggregate count information in the event result set. Do I need to create a custom entity set that will be the source for the OData service? I probably included too litte information here for finding a solution, but I don't really know where to look. Can somebody help me?
If you're willing to make an extra request per Event, you could query http://.../YourService.svc/Events(<key>)/Registrations/$count (or http://.../YourService.svc/Events(<key>)/$links/Registrations?$inlinecount=allpages if you're also using the links to the Registration entities).
Examples of both of these approaches on a public service:
http://services.odata.org/V3/OData/OData.svc/Suppliers(0)/Products/$count
http://services.odata.org/V3/OData/OData.svc/Suppliers(0)/$links/Products?$inlinecount=allpages&$format=json
I'm guessing that you'd prefer this information to come bundled together with the rest of the Events response though. It's not ideal, but you could issue a query along these lines:
http://services.odata.org/V3/OData/OData.svc/Suppliers?$format=json&$expand=Products&$select=Products/ID,*
I'm expanding Products (analogous to your Registrations) and selecting Products/ID in order to force the response to include an array that is the same size as the nested Products collection. I don't care about ID -- I just chose a piece of data that would be small. With this JSON response, your javascript client can get the length of the Products array and use that as the number of Products that are linked to the given Supplier.
(Note: to have your service support $select queries using WCF Data Services, you'll need to include this line when you initialize the service: config.DataServiceBehavior.AcceptProjectionRequests = true;)
Edit to add: The approach using $expand and $select won't be guaranteed to give you the correct count if your server does server-driving paging. In general, there isn't a simple single-response way to do what you're asking for in OData v3, but in OData v4, this will be possible with the new expand/select syntax.
i'm using oData v4 and i used this syntax :
var url = '.../odata/clients?$expand=Orders($count=true)';
// ...
a field called Orders#odata.count has been added to the response entity which contains the correct count.
and now to access the JSON property containing a dash you have to do it like this :
var ordersCount = response.value['Orders#odata.count'];
hope this helps.
Can you edit your Event model and add a RegistrationCount property? That'd be the simplest way I think
What I ended up doing was actually very simple; I created a View in SQL Server that returns the table including the registration counts. Never thought about using a view, since I've never used them before...
I used this to get the child count without returning the entities:
/Parents$expand=Children($count=true;$top=0)

Organising resource (URI) in REST API

Scenario 1
In my web application say for there is a screen for adding an employee to system. As soon as user tabs after entering name of the employee, it generates the employee code automatically (which is the next field) based on some logic and already present records in the database.
Now I want to expose rest API for this application so that third party devs can build on top of it. So, I will have a resource called as /Employee which will respond for GET, PUT and DELETE verbs. But when a client needs to autofill the code, which is a GET operation, it will be on what resource? Should I make a new resource /EmployeeCodeFor/{Name} or I should get it on /Employee/{Name}/GenerateCode? If I go with /Employee/{Name}/GenerateCode then what about my resource to GET, PUT and DELETE for Employee i.e. actually /Employee/{Id}?
Scenario 2
Here lets take the case of a stackoverflow post. So lets say the resource would be /Post/{Id}. In the same way as in the previous example it lists me possible duplicate question as soon as I tab out of the Title field.
Again on what URL I should get those possible duplicates?
I can't think of more scenarios just now. But many such kind of scenarios may come up in real life application development. How to implement them in RESTful way?
Update on scenario 1
Code and Id are two different fields. Id is primary key, code can be duplicate across departments just to illustrate. Also to generate a code, name should be provided first. So, if user types a name "FirstName LastName" then server might generate FL003 as code assuming that there are already two more employees with firstname starting from F and lastname starting from L in the said department. Department can be identified based on the logged in user.
One way to allow the server an opportunity to pre-fill a bunch of elements in a new resource is to do
POST /Employees
{with empty body}
=>
201 Created
Location: http://example.org/employee/3443
<Employee Id="3443">
<Code>E1001</Code>
<FirstName></FirstName>
<LastName></LastName>
</Employee>
This gives the server one chance to provide default values. If you are looking for a more interactive way for the server to provide feedback during the input, I have another approach but it will take quite a bit more explaining.
Scenario 1
Let say your employee code is a unique identifier. In this case, to get it, you would allow the user to complete any field for the new employee and then make a POST. The server would generate the code and respond to the POST with a link to /Employee/{generated_code} which is the record for your newly created employee.
A GET on /Employee would return a list of all employees. A GET on /Employee/{a_code} will give you the employee detail.
Scenario 2
You could have some kind of query on the /Post collection like /Post?title_like={question_title}. A GET /Post?title_like=REST How to would return you a list of all questions containing "REST How to".