-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial simpleAPI with some integer field type fixes
- Loading branch information
1 parent
0aca49c
commit b3ebfc0
Showing
11 changed files
with
224 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ava/uk/co/compendiumdev/challenge/practicemodes/simpleapi/SimpleAPITestDataPopulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package uk.co.compendiumdev.challenge.practicemodes.simpleapi; | ||
|
||
import uk.co.compendiumdev.thingifier.core.domain.datapopulator.DataPopulator; | ||
import uk.co.compendiumdev.thingifier.core.domain.definitions.ERSchema; | ||
import uk.co.compendiumdev.thingifier.core.domain.instances.ERInstanceData; | ||
import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection; | ||
|
||
import java.util.Random; | ||
|
||
public class SimpleAPITestDataPopulator implements DataPopulator { | ||
|
||
@Override | ||
public void populate(final ERSchema schema, final ERInstanceData database) { | ||
|
||
String [] types={ | ||
"book", | ||
"book", | ||
"dvd", | ||
"blu-ray", | ||
"cd", | ||
"cd", | ||
"dvd", | ||
"blu-ray"}; | ||
|
||
EntityInstanceCollection items = database.getInstanceCollectionForEntityNamed("item"); | ||
|
||
Random random = new Random(); | ||
for(String type : types){ | ||
items.createManagedInstance(). | ||
setValue("type", type). | ||
setValue("numberinstock", String.valueOf(random.nextInt(20))). | ||
setValue("isbn13", randomIsbn(random)). | ||
setValue("price", String.valueOf(random.nextInt(99)) + "." + String.valueOf(random.nextInt(99)) ) | ||
; | ||
} | ||
} | ||
|
||
private String randomIsbn(Random random){ | ||
|
||
String isbn13 = "xxx-x-xx-xxxxxx-x"; | ||
|
||
while(isbn13.contains("x")){ | ||
isbn13 = isbn13.replaceFirst("x", String.valueOf(random.nextInt(9))); | ||
} | ||
|
||
return isbn13; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
.../src/main/java/uk/co/compendiumdev/challenge/practicemodes/simpleapi/SimpleApiRoutes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package uk.co.compendiumdev.challenge.practicemodes.simpleapi; | ||
|
||
import uk.co.compendiumdev.thingifier.Thingifier; | ||
import uk.co.compendiumdev.thingifier.api.docgen.ThingifierApiDocumentationDefn; | ||
import uk.co.compendiumdev.thingifier.apiconfig.ThingifierApiConfig; | ||
import uk.co.compendiumdev.thingifier.application.httprouting.ThingifierAutoDocGenRouting; | ||
import uk.co.compendiumdev.thingifier.application.httprouting.ThingifierHttpApiRoutings; | ||
import uk.co.compendiumdev.thingifier.core.EntityRelModel; | ||
import uk.co.compendiumdev.thingifier.core.domain.definitions.EntityDefinition; | ||
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.Field; | ||
import uk.co.compendiumdev.thingifier.core.domain.definitions.field.definition.FieldType; | ||
import uk.co.compendiumdev.thingifier.core.domain.definitions.validation.MatchesRegexValidationRule; | ||
import uk.co.compendiumdev.thingifier.core.domain.definitions.validation.MaximumLengthValidationRule; | ||
import uk.co.compendiumdev.thingifier.htmlgui.htmlgen.DefaultGUIHTML; | ||
import uk.co.compendiumdev.thingifier.htmlgui.routing.DefaultGuiRoutings; | ||
|
||
/* | ||
The simple API is a no-auth API where anyone can amend, create, delete items. | ||
To make this Safe all the fields will be primitives and no Strings. | ||
*/ | ||
public class SimpleApiRoutes { | ||
|
||
public Thingifier simplethings; | ||
public EntityDefinition entityDefn; | ||
private ThingifierApiDocumentationDefn apiDocDefn; | ||
private ThingifierAutoDocGenRouting simpleApiDocsRouting; | ||
private ThingifierHttpApiRoutings simpleApiHttpRouting; | ||
private DefaultGuiRoutings simpleApiGuiRouting; | ||
|
||
public SimpleApiRoutes(){ | ||
// fake the data storage | ||
this.simplethings = new Thingifier(); | ||
|
||
simplethings.setDocumentation( | ||
"Simple API Mode", | ||
"A simple API, no auth protection so you can add and delete what you want in a multi-user mode."); | ||
this.entityDefn = simplethings.defineThing("item", "items", 100); | ||
|
||
// TODO: add descriptions on a field level to explain what they are and show this in documentation | ||
this.entityDefn.addAsPrimaryKeyField(Field.is("id", FieldType.AUTO_INCREMENT)); | ||
this.entityDefn.addFields( | ||
Field.is("type", FieldType.ENUM). | ||
makeMandatory(). | ||
withExample("book"). | ||
withExample("blu-ray"). | ||
withExample("cd"). | ||
withExample("dvd"), | ||
Field.is("isbn13", FieldType.STRING). | ||
makeMandatory(). | ||
withValidation(new MatchesRegexValidationRule("[0-9]{3}[-]?[0-9]{1}[-]?[0-9]{2}[-]?[0-9]{6}[-]?[0-9]{1}")). | ||
withValidation(new MaximumLengthValidationRule(17)). | ||
withExample("123-4-56-789012-3"), | ||
Field.is("price",FieldType.FLOAT). | ||
makeMandatory(). | ||
withExample("97.99"). | ||
withMinimumValue(0f). | ||
withMaximumValue(50000.0f), | ||
Field.is("numberinstock", FieldType.INTEGER). | ||
withDefaultValue("0"). | ||
withMaximumValue(100). | ||
withMinimumValue(0) | ||
); | ||
|
||
simplethings.setDataGenerator(new SimpleAPITestDataPopulator()); | ||
|
||
simplethings.apiConfig().setFrom(new ThingifierApiConfig("/simpleapi")); | ||
// do not convert floats to int | ||
simplethings.apiConfig().setApiToEnforceDeclaredTypesInInput(false); | ||
|
||
// TODO: should probably have a support multiple databases config somewhere | ||
simplethings.getERmodel().populateDatabase(EntityRelModel.DEFAULT_DATABASE_NAME); | ||
|
||
} | ||
|
||
public void configure() { | ||
|
||
DefaultGUIHTML gui = new DefaultGUIHTML(); | ||
|
||
simpleApiGuiRouting = new DefaultGuiRoutings(simplethings, gui). | ||
configureRoutes("/simpleapi/gui"); | ||
|
||
apiDocDefn = new ThingifierApiDocumentationDefn(); | ||
apiDocDefn.setThingifier(simplethings); | ||
apiDocDefn.setPathPrefix("/simpleapi"); // where can the API endpoints be found | ||
simpleApiDocsRouting = new ThingifierAutoDocGenRouting( | ||
simplethings, | ||
apiDocDefn, | ||
gui); | ||
|
||
simpleApiHttpRouting = new ThingifierHttpApiRoutings(simplethings, apiDocDefn); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters