From 190418d4097ca6083a0a9bc85b324698bfd8b40f Mon Sep 17 00:00:00 2001 From: ecyr20 Date: Tue, 9 Apr 2024 00:23:28 -0500 Subject: [PATCH 1/3] Initial OpenAtmos Config Initial JSON file for OpenAtmos. It takes in a Tag Mechanism uuid then sends the JSON in string format. --- Controllers/OpenAtmosController.cs | 30 ++++++ Services/OpenAtmosService.cs | 146 +++++++++++++++++++++++++++++ Services/PropertyVersionService.cs | 1 + 3 files changed, 177 insertions(+) create mode 100644 Controllers/OpenAtmosController.cs create mode 100644 Services/OpenAtmosService.cs diff --git a/Controllers/OpenAtmosController.cs b/Controllers/OpenAtmosController.cs new file mode 100644 index 0000000..ce1e9c3 --- /dev/null +++ b/Controllers/OpenAtmosController.cs @@ -0,0 +1,30 @@ +using Chemistry_Cafe_API.Models; +using Chemistry_Cafe_API.Services; +using Microsoft.AspNetCore.Mvc; +using MySqlConnector; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace Chemistry_Cafe_API.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class OpenAtmosController : ControllerBase + { + private OpenAtmosService openAtmosService; + + //Injects sql data source setup in Program.cs + public OpenAtmosController([FromServices] MySqlDataSource db) + { + this.openAtmosService = new OpenAtmosService(db); + } + + // GET: api/OpenAtmos/JSON + [HttpGet("JSON/{tag_mechanism_uuid}")] + public async Task Get(Guid tag_mechanism_uuid) + { + return await openAtmosService.Get(tag_mechanism_uuid); + } + + } +} diff --git a/Services/OpenAtmosService.cs b/Services/OpenAtmosService.cs new file mode 100644 index 0000000..ae2e2e5 --- /dev/null +++ b/Services/OpenAtmosService.cs @@ -0,0 +1,146 @@ +using Chemistry_Cafe_API.Models; +using System.Data.Common; +using MySqlConnector; +using Microsoft.AspNetCore.Mvc; + +namespace Chemistry_Cafe_API.Services +{ + public class OpenAtmosService(MySqlDataSource database) + { + public async Task Get(Guid tag_mechanism_uuid) + { + ReactionService reactionService = new ReactionService(database); + SpeciesService speciesService = new SpeciesService(database); + TagMechanismService tagMechanismService = new TagMechanismService(database); + PropertyListService propertyListService = new PropertyListService(database); + ReactantProductListService reactantProductListService = new ReactantProductListService(database); + + var mechanism = tagMechanismService.GetTagMechanismAsync(tag_mechanism_uuid).Result; + + string JSON = "{\n" + + " \"version\": \"1.0.0\",\n" + + " \"name\": \"" + mechanism.tag + "\", \n"; + + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + var reactionList = reactionService.GetTags(tag_mechanism_uuid).Result; + var speciesList = speciesService.GetTags(tag_mechanism_uuid).Result; + + JSON += " \"species\": [ \n"; + + + foreach ( var species in speciesList ) + { + JSON += " { "; + JSON += " \"name\": \"" + species.type + "\", \n"; + var properties = propertyListService.GetPropertiesAsync(species.uuid).Result; + foreach ( var property in properties ) + { + JSON += " \"" + property.name + " " + property.units + "\": "; + + if (property.float_value.HasValue) + { + JSON += property.float_value.ToString(); + } + else if(property.double_value.HasValue) + { + JSON += property.double_value.ToString(); + } + else if (property.int_value.HasValue) + { + JSON += property.int_value.ToString(); + } + else if(property.string_value != null) + { + JSON += property.string_value; + } + + JSON += " , \n"; + } + JSON.Remove(JSON.Length - 1); + JSON.Remove(JSON.Length - 1); + JSON += " }, \n"; + } + JSON.Remove(JSON.LastIndexOf(',')); + + JSON += " ],\n" + + " \"phases\": [ \n" + + " { \n" + + " \"name\": \"gas\", \n" + + " \"species\": [ \n"; + foreach(Species species in speciesList) + { + JSON += " \"" + species.type + "\", \n"; + } + JSON.Remove(JSON.LastIndexOf(',')); + JSON += " ] \n" + + " } \n" + + " ],\n" + + " \"reactions\": [ \n"; + + foreach (var reaction in reactionList) + { + JSON += " { \n"; + JSON += " \"type\" : \"" + reaction.type.ToUpper() + "\", \n"; + var properties = propertyListService.GetPropertiesAsync(reaction.uuid).Result; + foreach (var property in properties) + { + if (property.units != null) + { + JSON += " \"" + property.name + " " + property.units + "\": "; + } + else + { + JSON += " \"" + property.name + "\": "; + } + + if (property.float_value.HasValue) + { + JSON += property.float_value.ToString(); + } + else if (property.double_value.HasValue) + { + JSON += property.double_value.ToString(); + } + else if (property.int_value.HasValue) + { + JSON += property.int_value.ToString(); + } + else if (property.string_value != null) + { + JSON += property.string_value; + } + + JSON += " , \n"; + } + var reactants = reactantProductListService.GetReactantsAsync(reaction.uuid).Result; + JSON += " \"reactants\": [ \n" + + " {"; + foreach (ReactantsProducts reactant in reactants) + { + JSON += " \"species name\": \"" + reactant.type + "\", \n"; + JSON += " \"coefficient\": \"" + reactant.quantity + "\" \n"; + } + JSON += " }\n" + + " ], \n"; + + var products = reactantProductListService.GetProductsAsync(reaction.uuid).Result; + JSON += " \"products\": [ \n" + + " {"; + foreach (ReactantsProducts product in products) + { + JSON += " \"species name\": \"" + product.type + "\", \n"; + JSON += " \"coefficient\": \"" + product.quantity + "\" \n"; + } + JSON += " }\n" + + " ], \n"; + JSON += " }"; + } + + JSON += " ]\n}"; + return JSON; + } + + } +} diff --git a/Services/PropertyVersionService.cs b/Services/PropertyVersionService.cs index c8cf185..c0ccc33 100644 --- a/Services/PropertyVersionService.cs +++ b/Services/PropertyVersionService.cs @@ -2,6 +2,7 @@ using System.Data.Common; using MySqlConnector; using Microsoft.AspNetCore.Mvc; +using System; namespace Chemistry_Cafe_API.Services { From bf3d3ffd46feb94b6fa1719bf6d9b55bb5f3416e Mon Sep 17 00:00:00 2001 From: ecyr20 Date: Tue, 9 Apr 2024 00:26:48 -0500 Subject: [PATCH 2/3] Fixed minor Formatting errors --- Services/OpenAtmosService.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Services/OpenAtmosService.cs b/Services/OpenAtmosService.cs index ae2e2e5..e26e67c 100644 --- a/Services/OpenAtmosService.cs +++ b/Services/OpenAtmosService.cs @@ -32,7 +32,7 @@ public async Task Get(Guid tag_mechanism_uuid) foreach ( var species in speciesList ) { - JSON += " { "; + JSON += " {\n"; JSON += " \"name\": \"" + species.type + "\", \n"; var properties = propertyListService.GetPropertiesAsync(species.uuid).Result; foreach ( var property in properties ) @@ -58,8 +58,7 @@ public async Task Get(Guid tag_mechanism_uuid) JSON += " , \n"; } - JSON.Remove(JSON.Length - 1); - JSON.Remove(JSON.Length - 1); + JSON.Remove(JSON.LastIndexOf(',')); JSON += " }, \n"; } JSON.Remove(JSON.LastIndexOf(',')); @@ -109,14 +108,14 @@ public async Task Get(Guid tag_mechanism_uuid) } else if (property.string_value != null) { - JSON += property.string_value; + JSON += "\"" + property.string_value + "\""; } JSON += " , \n"; } var reactants = reactantProductListService.GetReactantsAsync(reaction.uuid).Result; JSON += " \"reactants\": [ \n" + - " {"; + " {\n"; foreach (ReactantsProducts reactant in reactants) { JSON += " \"species name\": \"" + reactant.type + "\", \n"; @@ -127,7 +126,7 @@ public async Task Get(Guid tag_mechanism_uuid) var products = reactantProductListService.GetProductsAsync(reaction.uuid).Result; JSON += " \"products\": [ \n" + - " {"; + " {\n"; foreach (ReactantsProducts product in products) { JSON += " \"species name\": \"" + product.type + "\", \n"; From 46ef87f70f46ea89ca96087d50e0e28d254627d5 Mon Sep 17 00:00:00 2001 From: ecyr20 Date: Tue, 9 Apr 2024 00:31:36 -0500 Subject: [PATCH 3/3] I forgot on newline character --- Services/OpenAtmosService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Services/OpenAtmosService.cs b/Services/OpenAtmosService.cs index e26e67c..05d8031 100644 --- a/Services/OpenAtmosService.cs +++ b/Services/OpenAtmosService.cs @@ -134,7 +134,7 @@ public async Task Get(Guid tag_mechanism_uuid) } JSON += " }\n" + " ], \n"; - JSON += " }"; + JSON += " }\n"; } JSON += " ]\n}";