Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltester committed Dec 31, 2024
1 parent 9bdcebb commit d673020
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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.instances.EntityInstance;
import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection;

public class ChallengeThingifier {

Expand Down Expand Up @@ -46,14 +47,20 @@ public void populateThingifierFrom(ChallengeDefinitions challengeDefinitions) {
// create all instances from the definitions, then when we want to
// set all the status codes to the specific challenger status
for (ChallengeDefinitionData challenge : challengeDefinitions.getChallenges()) {
challengeThingifier.getThingInstancesNamed(challengeDefn.getName(), EntityRelModel.DEFAULT_DATABASE_NAME)
.createManagedInstance().
EntityInstanceCollection instances = challengeThingifier.getThingInstancesNamed(challengeDefn.getName(), EntityRelModel.DEFAULT_DATABASE_NAME);
createManagedInstance(instances).
overrideValue("id", challenge.id).
setValue("name", challenge.name).
setValue("description", challenge.description);
}
}

private EntityInstance createManagedInstance(EntityInstanceCollection entityStorage) {
EntityInstance instance = new EntityInstance(entityStorage.definition());
entityStorage.addInstance(instance);
return instance;
}

public void populateThingifierFromStatus(ChallengerAuthData challenger){

ChallengerAuthData challengerToUse = challenger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uk.co.compendiumdev.thingifier.core.EntityRelModel;
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.EntityInstance;
import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection;
import uk.co.compendiumdev.thingifier.core.domain.datapopulator.DataPopulator;

Expand All @@ -26,8 +27,9 @@ public void populate(final ERSchema schema, final ERInstanceData database) {
EntityInstanceCollection todo = database.getInstanceCollectionForEntityNamed("todo");

for(String todoItem : todos){
todo.createManagedInstance().
setValue("title", todoItem);
EntityInstance instance = new EntityInstance(todo.definition());
todo.addInstance(instance);
instance.setValue("title", todoItem);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.EntityInstance;
import uk.co.compendiumdev.thingifier.core.domain.instances.EntityInstanceCollection;

import java.util.Random;
Expand All @@ -26,7 +27,7 @@ public void populate(final ERSchema schema, final ERInstanceData database) {

Random random = new Random();
for(String type : types){
items.createManagedInstance().
createManagedInstance(items).
setValue("type", type).
setValue("numberinstock", String.valueOf(random.nextInt(20))).
setValue("isbn13", randomIsbn(random)).
Expand All @@ -35,6 +36,13 @@ public void populate(final ERSchema schema, final ERInstanceData database) {
}
}


private EntityInstance createManagedInstance(EntityInstanceCollection entityStorage) {
EntityInstance instance = new EntityInstance(entityStorage.definition());
entityStorage.addInstance(instance);
return instance;
}

private String randomIsbn(Random random){

String isbn13 = "xxx-x-xx-xxxxxx-x";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public SimulationRoutes(DefaultGUIHTML guiTemplates){

public void setUpData(){
// fake the data storage
this.simulation = new Thingifier();
simulation = new Thingifier();

simulation.setDocumentation("Simulation Mode", "A simulated API, each request generates a new set of data but responses are processed by an API handler.");
this.entityDefn = this.simulation.defineThing("entity", "entities");
entityDefn = simulation.defineThing("entity", "entities");

this.entityDefn.addAsPrimaryKeyField(Field.is("id", FieldType.AUTO_INCREMENT));
this.entityDefn.addFields(
entityDefn.addAsPrimaryKeyField(Field.is("id", FieldType.AUTO_INCREMENT));
entityDefn.addFields(
Field.is("name", FieldType.STRING).
makeMandatory().
withValidation(new MaximumLengthValidationRule(50)).
Expand All @@ -61,22 +61,22 @@ public void setUpData(){
withValidation(new MaximumLengthValidationRule(200))
);

this.entityStorage = this.simulation.getThingInstancesNamed("entity", EntityRelModel.DEFAULT_DATABASE_NAME);
entityStorage = simulation.getThingInstancesNamed("entity", EntityRelModel.DEFAULT_DATABASE_NAME);

for(int id=1; id<=10; id++){

this.entityStorage.createManagedInstance().
//setValue("id", String.valueOf(id)).
setValue("name", "entity number " +id);
createManagedInstance(entityStorage).
//setValue("id", String.valueOf(id)).
setValue("name", "entity number " +id);
}

this.entityStorage.createManagedInstance().
//setValue("id", String.valueOf(id)).
setValue("name", "bob");
createManagedInstance(entityStorage).
//setValue("id", String.valueOf(id)).
setValue("name", "bob");

// this gives us access to the common http processing functions
this.httpApi = new ThingifierHttpApi(this.simulation);
this.jsonThing = new JsonThing(this.simulation.apiConfig().jsonOutput());
httpApi = new ThingifierHttpApi(simulation);
jsonThing = new JsonThing(simulation.apiConfig().jsonOutput());

apiDocDefn = new ThingifierApiDocumentationDefn();
apiDocDefn.setThingifier(simulation);
Expand Down Expand Up @@ -108,6 +108,12 @@ public void setUpData(){

}

private EntityInstance createManagedInstance(EntityInstanceCollection entityStorage) {
EntityInstance instance = new EntityInstance(entityStorage.definition());
entityStorage.addInstance(instance);
return instance;
}

public void configure() {

setUpData();
Expand Down Expand Up @@ -143,14 +149,14 @@ public void configure() {

// process it because the request validated
List<EntityInstance> instances = new ArrayList();
for (EntityInstance possible : this.entityStorage.getInstances()) {
for (EntityInstance possible : entityStorage.getInstances()) {
if (!idsToRemove.contains(
possible.getFieldValue("id").asInteger())) {
instances.add(possible);
}
}

Thingifier cloned = this.simulation.cloneWithDifferentData(instances);
Thingifier cloned = simulation.cloneWithDifferentData(instances);
return new RestApiGetHandler(cloned).handle("entities", anHttpApiRequest.getFilterableQueryParams(), anHttpApiRequest.getHeaders());

};
Expand All @@ -173,7 +179,7 @@ public void configure() {

// process it because the request validated
String id = anHttpApiRequest.getUrlParam(":id");
EntityInstance instance = this.entityStorage.findInstanceByPrimaryKey(id);
EntityInstance instance = entityStorage.findInstanceByPrimaryKey(id);
if (instance == null) {
response = ApiResponse.error404("Could not find Entity with ID " + id);
} else {
Expand Down Expand Up @@ -218,9 +224,9 @@ public void configure() {

return new SparkApiRequestResponseHandler(request, result, simulation).
usingHandler((anHttpApiRequest) -> {
return ApiResponse.created(this.entityStorage.
return ApiResponse.created(entityStorage.
findInstanceByPrimaryKey("11"),
this.simulation.apiConfig());
simulation.apiConfig());
}).handle();
});

Expand All @@ -231,16 +237,16 @@ public void configure() {
if (id.equals("11")) {
// we can create id 11
response = ApiResponse.created(
this.entityStorage.findInstanceByPrimaryKey("11"),
this.simulation.apiConfig());
entityStorage.findInstanceByPrimaryKey("11"),
simulation.apiConfig());
} else {
if (id.equals("10")) {
// 10 is the entity we amend to name:eris
EntityInstance fake = new EntityInstance(entityDefn).
overrideValue("id", "10").setValue("name", "eris");
response = ApiResponse.success().returnSingleInstance(fake);
} else {
final EntityInstance instance = this.entityStorage.findInstanceByPrimaryKey(id);
final EntityInstance instance = entityStorage.findInstanceByPrimaryKey(id);
if (instance == null) {
if (anHttpApiRequest.getVerb() == HttpApiRequest.VERB.POST) {
response = ApiResponse.error404("Could not find Entity with ID " + id);
Expand Down Expand Up @@ -279,7 +285,7 @@ public void configure() {
// we can delete id 9
response = new ApiResponse(204);
} else {
final EntityInstance instance = this.entityStorage.findInstanceByPrimaryKey(id);
final EntityInstance instance = entityStorage.findInstanceByPrimaryKey(id);
if (instance == null) {
response = ApiResponse.error404("Could not find Entity with ID " + id);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public EntityInstanceCollection addInstances(List<EntityInstance> addInstances)
return this;
}

public EntityInstanceCollection addInstance(EntityInstance instance) {
public EntityInstance addInstance(EntityInstance instance) {

if(instance.getEntity()!=definition){
throw new RuntimeException(String.format(
Expand Down Expand Up @@ -134,15 +134,13 @@ public EntityInstanceCollection addInstance(EntityInstance instance) {
}
}

return this;
return instance;
}

/* create and add */
// TODO: this looks like it was added to support testing, consider removing and adding to a test helper
public EntityInstance createManagedInstance() {
EntityInstance instance = new EntityInstance(definition);
addInstance(instance);
return instance;
return addInstance( new EntityInstance(definition));
}

public int countInstances() {
Expand Down

0 comments on commit d673020

Please sign in to comment.