Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Initial OpenAtmos JSON Configuration #9

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Controllers/OpenAtmosController.cs
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);
}

}
}
145 changes: 145 additions & 0 deletions Services/OpenAtmosService.cs
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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 22 in Services/OpenAtmosService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

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

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 84 in Services/OpenAtmosService.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
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;
}

}
}
1 change: 1 addition & 0 deletions Services/PropertyVersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Data.Common;
using MySqlConnector;
using Microsoft.AspNetCore.Mvc;
using System;

namespace Chemistry_Cafe_API.Services
{
Expand Down
Loading