99import uk .co .compendiumdev .thingifier .core .domain .definitions .relationship .RelationshipVectorDefinition ;
1010
1111import java .util .HashMap ;
12+ import java .util .List ;
1213import java .util .Map ;
1314
1415public class ApiRoutingDefinitionDocGenerator {
@@ -72,14 +73,19 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
7273
7374 String uniqueIdentifier ="?" ;
7475 String uniqueIdFieldName ="fieldName" ;
76+ String uniqueUrlIdentifier = "{?}" ;
7577
7678 // a thing can have many id fields, so should choose one to be in the url
7779 // e.g. set a field as 'usedForIndividualRouting'
7880 Field uniqueIdField = getUniqueIdField (entityDefn );
7981 if (uniqueIdField !=null ){
8082 uniqueIdentifier = uniqueReferenceText .get (uniqueIdField .getType ());
8183 uniqueIdFieldName = uniqueIdField .getName ();
84+ uniqueUrlIdentifier = "{" + uniqueIdFieldName + "}" ;
8285 }
86+
87+
88+
8389// final List<Field> idFields = entityDefn.getFieldsOfType(FieldType.ID);
8490// if(config.willUrlsShowIdsIfAvailable() && !idFields.isEmpty()){
8591// uniqueIdentifier=uniqueID;
@@ -129,6 +135,7 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
129135 201 , String .format ("Created a %s" ,entityDefn .getName ())
130136 )
131137 ).returnPayload (201 , entityDefn .getName ()).
138+ requestPayload ("create_" + entityDefn .getName ()).
132139 addPossibleStatus (RoutingStatus .returnValue (
133140 400 , String .format ("Error when creating a %s" , entityDefn .getName ())));
134141
@@ -148,12 +155,13 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
148155 defn .addRouting ("method not allowed" ,
149156 RoutingVerb .TRACE , pluralUrl , RoutingStatus .returnValue (405 ));
150157
151- String aUrlWGuid = pluralUrl + "/" + uniqueIdentifier ;
158+ String aUrlWGuid = pluralUrl + "/" + uniqueUrlIdentifier ;
152159 // we should be able to get specific things based on the GUID e.g. GET project/GUID
153160 defn .addRouting (
154161 String .format ("return a specific instances of %s using a %s" ,
155162 entityDefn .getName (),uniqueIdFieldName ),
156163 RoutingVerb .GET , aUrlWGuid , RoutingStatus .returnedFromCall ()).
164+ addRequestUrlParam (entityDefn .getField (uniqueIdFieldName )).
157165 addPossibleStatus (RoutingStatus .returnValue (
158166 200 , String .format ("A specific %s" , entityDefn .getName ()))).
159167 returnPayload (200 , entityDefn .getName ()).
@@ -163,6 +171,7 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
163171 defn .addRouting (String .format ("headers for a specific instances of %s using a %s" ,
164172 entityDefn .getName (),uniqueIdFieldName ),
165173 RoutingVerb .HEAD , aUrlWGuid , RoutingStatus .returnedFromCall ()).
174+ addRequestUrlParam (entityDefn .getField (uniqueIdFieldName )).
166175 addPossibleStatus (RoutingStatus .returnValue (
167176 200 , String .format ("Headers for a specific %s" , entityDefn .getName ()))).
168177 addPossibleStatus (RoutingStatus .returnValue (
@@ -173,9 +182,11 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
173182 String .format ("amend a specific instances of %s using a %s with a body containing the fields to amend" ,
174183 entityDefn .getName (), uniqueIdFieldName ),
175184 RoutingVerb .POST , aUrlWGuid , RoutingStatus .returnedFromCall ()).
185+ addRequestUrlParam (entityDefn .getField (uniqueIdFieldName )).
176186 addPossibleStatus (RoutingStatus .returnValue (
177187 200 , String .format ("Amended the specific %s" , entityDefn .getName ()))).
178188 returnPayload (200 , entityDefn .getName ()).
189+ requestPayload (entityDefn .getName ()).
179190 addPossibleStatus (RoutingStatus .returnValue (
180191 404 , String .format ("Could not find a specific %s" ,entityDefn .getName ())));;
181192
@@ -189,9 +200,11 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
189200 String .format ("amend a specific instances of %1$s using a %2$s with a body containing the fields to amend" ,
190201 entityDefn .getName (),uniqueIdFieldName )+ifGUIDamendment ,
191202 RoutingVerb .PUT , aUrlWGuid , RoutingStatus .returnedFromCall ()).
203+ addRequestUrlParam (entityDefn .getField (uniqueIdFieldName )).
192204 addPossibleStatus (RoutingStatus .returnValue (
193205 200 , String .format ("Replaced the specific %s details" , entityDefn .getName ()))).
194206 returnPayload (200 , entityDefn .getName ()).
207+ requestPayload (entityDefn .getName ()).
195208 addPossibleStatus (RoutingStatus .returnValue (
196209 404 , String .format ("Could not find a specific %s" , entityDefn .getName ())));;
197210
@@ -200,6 +213,7 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
200213 String .format ("delete a specific instances of %s using a %s" ,
201214 entityDefn .getName (), uniqueIdFieldName ),
202215 RoutingVerb .DELETE , aUrlWGuid , RoutingStatus .returnedFromCall ()).
216+ addRequestUrlParam (entityDefn .getField (uniqueIdFieldName )).
203217 addPossibleStatus (RoutingStatus .returnValue (
204218 200 , String .format ("Deleted a specific %s" , entityDefn .getName ()))).
205219 addPossibleStatus (RoutingStatus .returnValue (
@@ -209,10 +223,11 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
209223 defn .addRouting (
210224 String .format ("show all Options for endpoint of %s" , aUrlWGuid ),
211225 RoutingVerb .OPTIONS , aUrlWGuid , RoutingStatus .returnValue (204 ),
212- new ResponseHeader ("Allow" , "OPTIONS, GET, HEAD, POST, PUT, DELETE" ));
226+ new ResponseHeader ("Allow" , "OPTIONS, GET, HEAD, POST, PUT, DELETE" ))
227+ .addRequestUrlParam (entityDefn .getField (uniqueIdFieldName ));
213228
214- defn .addRouting ("method not allowed" , RoutingVerb .PATCH , aUrlWGuid , RoutingStatus .returnValue (405 ));
215- defn .addRouting ("method not allowed" , RoutingVerb .TRACE , aUrlWGuid , RoutingStatus .returnValue (405 ));
229+ defn .addRouting ("method not allowed" , RoutingVerb .PATCH , aUrlWGuid , RoutingStatus .returnValue (405 )). addRequestUrlParam ( entityDefn . getField ( uniqueIdFieldName )) ;
230+ defn .addRouting ("method not allowed" , RoutingVerb .TRACE , aUrlWGuid , RoutingStatus .returnValue (405 )). addRequestUrlParam ( entityDefn . getField ( uniqueIdFieldName )) ;
216231
217232
218233 for (RelationshipVectorDefinition rel : entityDefn .related ().getRelationships ()) {
@@ -224,19 +239,23 @@ public ApiRoutingDefinition generate(String apiPathPrefix) {
224239 }
225240
226241 private Field getUniqueIdField (final EntityDefinition thingDefn ) {
227- return thingDefn .getPrimaryKeyField ();
242+ Field aField = thingDefn .getPrimaryKeyField ();
243+
244+ if (aField ==null ) {
245+ final List <Field > idFields = thingDefn .getFieldsOfType (FieldType .AUTO_INCREMENT );
246+ if (config .willUrlsShowIdsIfAvailable () && !idFields .isEmpty ()) {
247+ aField = idFields .get (0 );
248+ } else {
249+ final List <Field > guidFields = thingDefn .getFieldsOfType (FieldType .AUTO_GUID );
250+ if (!guidFields .isEmpty ()) {
251+ aField = guidFields .get (0 );
252+ } else {
253+ aField = null ;
254+ }
255+ }
256+ }
228257
229- // final List<Field> idFields = thingDefn.getFieldsOfType(FieldType.AUTO_INCREMENT);
230- // if(config.willUrlsShowIdsIfAvailable() && !idFields.isEmpty()){
231- // return idFields.get(0);
232- // }else{
233- // final List<Field> guidFields = thingDefn.getFieldsOfType(FieldType.AUTO_GUID);
234- // if(!guidFields.isEmpty()) {
235- // return guidFields.get(0);
236- // }else{
237- // return null;
238- // }
239- // }
258+ return aField ;
240259 }
241260
242261 private void addRoutingsForRelationship (final ApiRoutingDefinition defn , final RelationshipVectorDefinition relationship ) {
@@ -259,6 +278,8 @@ private void addRoutingsForRelationship(final ApiRoutingDefinition defn, final R
259278 uniqueIdFieldName = uniqueIdField .getName ();
260279 }
261280
281+ String uniqueUrlParam = "{" + uniqueIdentifier + "}" ;
282+
262283// final List<Field> idFields = thingDefn.getFieldsOfType(FieldType.ID);
263284// if(config.willUrlsShowIdsIfAvailable() && !idFields.isEmpty()){
264285// uniqueIdentifier=uniqueID;
0 commit comments