public async Task <ActionResult> CreateDataElement( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerId, [FromRoute] Guid instanceGuid, [FromQuery] string elementType = "default") { bool startService = true; IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType, startService); Application application = repositoryService.GetApplication(org, app); if (application != null) { return(NotFound($"AppId {org}/{app} was not found")); } Instance instanceBefore = await instanceService.GetInstance(app, org, instanceOwnerId, instanceGuid); if (instanceBefore == null) { return(BadRequest("Unknown instance")); } object serviceModel = null; if (Request.ContentType == null) { serviceModel = serviceImplementation.CreateNewServiceModel(); } else { serviceModel = ParseContentAndDeserializeServiceModel(serviceImplementation.GetServiceModelType(), out ActionResult contentError); if (contentError != null) { return(contentError); } } serviceImplementation.SetServiceModel(serviceModel); // send events to trigger application business logic await serviceImplementation.RunServiceEvent(ServiceEventType.Instantiation); await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation); InstancesController.SetAppSelfLinks(instanceBefore, Request); Instance instanceAfter = await dataService.InsertData(serviceModel, instanceGuid, serviceImplementation.GetServiceModelType(), org, app, instanceOwnerId); InstancesController.SetAppSelfLinks(instanceAfter, Request); List <DataElement> createdElements = CompareAndReturnCreatedElements(instanceBefore, instanceAfter); string dataUrl = createdElements.First().DataLinks.Apps; return(Created(dataUrl, instanceAfter)); }
private async Task <ActionResult> PutFormData(string org, string app, Instance instance, Guid dataGuid, string elementType) { Guid instanceGuid = Guid.Parse(instance.Id.Split("/")[1]); IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType); object serviceModel = ParseContentAndDeserializeServiceModel(serviceImplementation.GetServiceModelType(), out ActionResult contentError); if (contentError != null) { return(contentError); } if (serviceModel == null) { return(BadRequest("No data found in content")); } serviceImplementation.SetServiceModel(serviceModel); // send events to trigger application business logic await serviceImplementation.RunServiceEvent(ServiceEventType.DataRetrieval); await serviceImplementation.RunServiceEvent(ServiceEventType.Calculation); try { // Run the model Validation that handles validation defined on the model TryValidateModel(serviceModel); // send events to trigger application business logic await serviceImplementation.RunServiceEvent(ServiceEventType.Validation); } catch (Exception ex) { logger.LogError($"Validation errors are currently ignored: {ex.Message}"); } // Save Formdata to database Instance instanceAfter = await this.dataService.UpdateData( serviceModel, instanceGuid, serviceImplementation.GetServiceModelType(), org, app, int.Parse(instance.InstanceOwnerId), dataGuid); InstancesController.SetAppSelfLinks(instanceAfter, Request); DataElement updatedElement = instanceAfter.Data.First(d => d.Id == dataGuid.ToString()); string dataUrl = updatedElement.DataLinks.Apps; return(Created(dataUrl, updatedElement)); }
private async Task <ActionResult> CreateFormData( string org, string app, Instance instanceBefore, string elementType) { bool startService = true; Guid instanceGuid = Guid.Parse(instanceBefore.Id.Split("/")[1]); IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType, startService); object serviceModel; if (Request.ContentType == null) { serviceModel = serviceImplementation.CreateNewServiceModel(); } else { serviceModel = ParseContentAndDeserializeServiceModel(serviceImplementation.GetServiceModelType(), out ActionResult contentError); if (contentError != null) { return(contentError); } } serviceImplementation.SetServiceModel(serviceModel); // send events to trigger application business logic await serviceImplementation.RunServiceEvent(ServiceEventType.Instantiation); await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation); InstancesController.SetAppSelfLinks(instanceBefore, Request); Instance instanceAfter = await dataService.InsertFormData(serviceModel, instanceGuid, serviceImplementation.GetServiceModelType(), org, app, int.Parse(instanceBefore.InstanceOwnerId)); InstancesController.SetAppSelfLinks(instanceAfter, Request); List <DataElement> createdElements = CompareAndReturnCreatedElements(instanceBefore, instanceAfter); string dataUrl = createdElements.First().DataLinks.Apps; return(Created(dataUrl, createdElements)); }