This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from NCAR/OpenAtmos-JSON
Initial OpenAtmos JSON Configuration
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> Get(Guid tag_mechanism_uuid) | ||
{ | ||
return await openAtmosService.Get(tag_mechanism_uuid); | ||
} | ||
|
||
} | ||
} |
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,145 @@ | ||
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<string> 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"; | ||
Check warning on line 22 in Services/OpenAtmosService.cs GitHub Actions / build
|
||
|
||
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 += " {\n"; | ||
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.LastIndexOf(',')); | ||
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"; | ||
Check warning on line 84 in Services/OpenAtmosService.cs GitHub Actions / build
|
||
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" + | ||
" {\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" + | ||
" {\n"; | ||
foreach (ReactantsProducts product in products) | ||
{ | ||
JSON += " \"species name\": \"" + product.type + "\", \n"; | ||
JSON += " \"coefficient\": \"" + product.quantity + "\" \n"; | ||
} | ||
JSON += " }\n" + | ||
" ], \n"; | ||
JSON += " }\n"; | ||
} | ||
|
||
JSON += " ]\n}"; | ||
return JSON; | ||
} | ||
|
||
} | ||
} |
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