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

Additional routes for front end #8

Merged
merged 6 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
21 changes: 14 additions & 7 deletions Controllers/PropertyListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,54 @@ namespace Chemistry_Cafe_API.Controllers
[ApiController]
public class PropertyListController : ControllerBase
{
private PropertyListService userpreferencesService;
private PropertyListService propertyListService;

//Injects sql data source setup in Program.cs
public PropertyListController([FromServices] MySqlDataSource db)
{
this.userpreferencesService = new PropertyListService(db);
this.propertyListService = new PropertyListService(db);
}

// GET: api/PropertyList/all
[HttpGet("all")]
public async Task<IReadOnlyList<PropertyList>> Get()
{
return await userpreferencesService.GetPropertyListsAsync();
return await propertyListService.GetPropertyListsAsync();
}

// GET api/PropertyList/5
[HttpGet("{uuid}")]
public async Task<PropertyList?> Get(Guid uuid)
{
return await userpreferencesService.GetPropertyListAsync(uuid);
return await propertyListService.GetPropertyListAsync(uuid);
}

// GET api/PropertyList/Properties/5
[HttpGet("Properties/{parent_uuid}")]
public async Task<IReadOnlyList<Property>> GetProperties(Guid parent_uuid)
{
return await propertyListService.GetPropertiesAsync(parent_uuid);
}

// POST api/PropertyList/create
[HttpPost("create")]
public async Task<Guid> Create([FromBody] PropertyList userPreferneces)
{
return await userpreferencesService.CreatePropertyListAsync(userPreferneces);
return await propertyListService.CreatePropertyListAsync(userPreferneces);
}

// PUT api/PropertyList/5
[HttpPut("update")]
public async Task Put([FromBody] PropertyList userpreferences)
{
await userpreferencesService.UpdatePropertyListAsync(userpreferences);
await propertyListService.UpdatePropertyListAsync(userpreferences);
}

// DELETE api/PropertyList/delete/5
[HttpDelete("delete/{uuid}")]
public async Task Delete(Guid uuid)
{
await userpreferencesService.DeletePropertyListAsync(uuid);
await propertyListService.DeletePropertyListAsync(uuid);
}
}
}
15 changes: 15 additions & 0 deletions Controllers/ReactantProductListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Chemistry_Cafe_API.Services;
using Microsoft.AspNetCore.Mvc;
using MySqlConnector;
using System;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand Down Expand Up @@ -33,6 +34,20 @@ public async Task<IReadOnlyList<ReactantProductList>> Get()
return await userpreferencesService.GetReactantProductListAsync(uuid);
}

// GET api/ReactantProductList/Reactants/5
[HttpGet("Reactants/{reaction_reactant_list_uuid}")]
public async Task<IReadOnlyList<ReactantsProducts>> GetReactants(Guid reaction_reactant_list_uuid)
{
return await userpreferencesService.GetReactantsAsync(reaction_reactant_list_uuid);
}

// GET api/ReactantProductList/Products/5
[HttpGet("Products/{reaction_product_list_uuid}")]
public async Task<IReadOnlyList<ReactantsProducts>> GetProducts(Guid reaction_product_list_uuid)
{
return await userpreferencesService.GetProductsAsync(reaction_product_list_uuid);
}

// POST api/ReactantProductList/create
[HttpPost("create")]
public async Task Create([FromBody] ReactantProductList reactantProduct)
Expand Down
4 changes: 2 additions & 2 deletions Controllers/ReactionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public async Task<IReadOnlyList<Reaction>> GetTags(Guid tag_mechanism_uuid)
// POST api/Reaction/create
[HttpPost("create")]

public async Task<Guid> Create([FromBody] Reaction reaction)
public async Task<Guid> Create([FromBody] string type)
{
return await reactionService.CreateReactionAsync(reaction);
return await reactionService.CreateReactionAsync(type);
}

// PUT api/Reaction/5
Expand Down
28 changes: 28 additions & 0 deletions Models/Property.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Chemistry_Cafe_API.Models
{
public class Property
{
public Guid property_list_uuid { get; set; }
public Guid parent_uuid { get; set; }
public string? version { get; set; }
public bool property_list_isDel { get; set; }
public Guid property_version_uuid { get; set; }
public Guid parent_property_uuid { get; set; }
public string? frozen_version { get; set; }
public Guid mechanism_uuid { get; set; }
public Guid property_type { get; set; }
public float? float_value { get; set; }
public double? double_value { get; set; }
public int? int_value { get; set; }
public string? string_value { get; set; }
public string? action { get; set; }
public Guid user_uuid { get; set; }
public DateTime datetime { get; set; }
public bool property_version_isDel { get; set; }
public Guid property_type_uuid { get; set; }
public string? name { get; set; }
public string? units { get; set; }
public string? validation { get; set; }
public bool property_type_isDel { get; set; }
}
}
11 changes: 11 additions & 0 deletions Models/ReactantsProducts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Chemistry_Cafe_API.Models
{
public class ReactantsProducts
{
public Guid reactant_product_uuid { get; set; }
public Guid reaction_uuid { get; set; }
public Guid species_uuid { get; set;}
public int quantity { get; set; }
public string? type { get; set; }
}
}
75 changes: 75 additions & 0 deletions Services/PropertyListService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public async Task<IReadOnlyList<PropertyList>> GetPropertyListsAsync()
return result.FirstOrDefault();
}

public async Task<IReadOnlyList<Property>> GetPropertiesAsync(Guid parent_uuid)
{
using var connection = await database.OpenConnectionAsync();
using var command = connection.CreateCommand();

command.CommandText = @"SELECT * FROM Property_List RIGHT JOIN Property_Version ON Property_List.uuid = Property_Version.parent_property_uuid
LEFT JOIN PropertyType ON Property_Version.property_type = PropertyType.uuid
WHERE Property_List.parent_uuid = @parent_uuid ORDER BY datetime DESC;";
command.Parameters.AddWithValue("@parent_uuid", parent_uuid);

return await ReadAllPropertiesAsync(await command.ExecuteReaderAsync());
}

public async Task<Guid> CreatePropertyListAsync(PropertyList userPreferences)
{
using var connection = await database.OpenConnectionAsync();
Expand Down Expand Up @@ -92,5 +105,67 @@ private async Task<IReadOnlyList<PropertyList>> ReadAllAsync(DbDataReader reader
}
return propertyList;
}

private async Task<IReadOnlyList<Property>> ReadAllPropertiesAsync(DbDataReader reader)
{
var propertyList = new List<Property>();
using (reader)
{
while (await reader.ReadAsync())
{
var property = new Property
{
property_list_uuid = reader.GetGuid(0),
parent_uuid = reader.GetGuid(1),
version = reader.GetString(2),
property_list_isDel = reader.GetBoolean(3),
property_version_uuid = reader.GetGuid(4),
parent_property_uuid = reader.GetGuid(5),
frozen_version = reader.GetString(6),
mechanism_uuid = reader.GetGuid(7),
property_type = reader.GetGuid(8),
user_uuid = reader.GetGuid(14),
datetime = reader.GetDateTime(15),
property_version_isDel = reader.GetBoolean(16),
property_type_uuid = reader.GetGuid(17),
property_type_isDel = reader.GetBoolean(21)
};
if (!reader.IsDBNull(9))
{
property.float_value = reader.GetFloat(9);
}
if (!reader.IsDBNull(10))
{
property.double_value = reader.GetDouble(10);
}
if (!reader.IsDBNull(11))
{
property.int_value = reader.GetInt32(11);
}
if (!reader.IsDBNull(12))
{
property.string_value = reader.GetString(12);
}
if (!reader.IsDBNull(13))
{
property.action = reader.GetString(13);
}
if (!reader.IsDBNull(18))
{
property.name = reader.GetString(18);
}
if (!reader.IsDBNull(19))
{
property.units = reader.GetString(19);
}
if (!reader.IsDBNull(20))
{
property.validation = reader.GetString(20);
}
propertyList.Add(property);
}
}
return propertyList;
}
}
}
24 changes: 17 additions & 7 deletions Services/PropertyTypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Data.Common;
using MySqlConnector;
using Microsoft.AspNetCore.Mvc;
using System.Xml;
using System.Xml.Linq;


namespace Chemistry_Cafe_API.Services
Expand Down Expand Up @@ -82,14 +84,22 @@ private async Task<IReadOnlyList<PropertyType>> ReadAllAsync(DbDataReader reader
{
while (await reader.ReadAsync())
{
var propertytype = new PropertyType
var propertytype = new PropertyType();
propertytype.uuid = reader.GetGuid(0);
if (!reader.IsDBNull(1))
{
uuid = reader.GetGuid(0),
name = reader.GetString(1),
units = reader.GetString(2),
validation = reader.GetString(3),
isDel = reader.GetBoolean(4),
};
propertytype.name = reader.GetString(1);
}
if (!reader.IsDBNull(2))
{
propertytype.units = reader.GetString(2);
}
if (!reader.IsDBNull(3))
{
propertytype.validation = reader.GetString(3);
}
propertytype.isDel = reader.GetBoolean(4);

propertytypes.Add(propertytype);
}
}
Expand Down
27 changes: 21 additions & 6 deletions Services/PropertyVersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<Guid> CreatePropertyVersionAsync(PropertyVersion newPropertyVe

command.CommandText = @"INSERT INTO Property_Version (uuid, parent_property_uuid, frozen_version, mechanism_uuid, property_type,
float_value, double_value, int_value, string_value, action, user_uuid, datetime)
VALUES (@uuid, @parent_propert_uuid, @frozen_version, @mechanism_uuid, @property_type, @float_value, @double_value, @int_value, @string_value, @action, @user_uuid, @datetime);";
VALUES (@uuid, @parent_property_uuid, @frozen_version, @mechanism_uuid, @property_type, @float_value, @double_value, @int_value, @string_value, @action, @user_uuid, @datetime);";

command.Parameters.AddWithValue("@uuid", propertyVersionID);
command.Parameters.AddWithValue("@parent_property_uuid", newPropertyVersion.parent_property_uuid);
Expand Down Expand Up @@ -108,15 +108,30 @@ private async Task<IReadOnlyList<PropertyVersion>> ReadAllAsync(DbDataReader rea
frozen_version = reader.GetString(2),
mechanism_uuid = reader.GetGuid(3),
property_type = reader.GetGuid(4),
float_value = reader.GetFloat(5),
double_value = reader.GetDouble(6),
int_value = reader.GetInt32(7),
string_value = reader.GetString(8),
action = reader.GetString(9),
user_uuid = reader.GetGuid(10),
datetime = reader.GetDateTime(11),
isDel = reader.GetBoolean(12),
};
if (!reader.IsDBNull(5))
{
property.float_value = reader.GetFloat(5);
}
if (!reader.IsDBNull(6))
{
property.double_value = reader.GetDouble(6);
}
if (!reader.IsDBNull(7))
{
property.int_value = reader.GetInt32(7);
}
if (!reader.IsDBNull(8))
{
property.string_value = reader.GetString(8);
}
if (!reader.IsDBNull(9))
{
property.action = reader.GetString(9);
}
propertyVersion.Add(property);
}
}
Expand Down
44 changes: 44 additions & 0 deletions Services/ReactantProductListService.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 Expand Up @@ -29,6 +30,28 @@ public async Task<IReadOnlyList<ReactantProductList>> GetReactantProductListsAsy
return result.FirstOrDefault();
}

public async Task<IReadOnlyList<ReactantsProducts>> GetReactantsAsync(Guid reaction_reactant_list_uuid)
{
using var connection = await database.OpenConnectionAsync();
using var command = connection.CreateCommand();

command.CommandText = "SELECT Reactant_Product_List.reactant_product_uuid, Reactant_Product_List.reaction_uuid, Reactant_Product_List.species_uuid, Reactant_Product_List.quantity, Species.type " +
"FROM Reactant_Product_List LEFT JOIN Species ON species_uuid = uuid WHERE reactant_product_uuid = @reaction_reactant_list_uuid";
command.Parameters.AddWithValue("@reaction_reactant_list_uuid", reaction_reactant_list_uuid);
return await ReadAllReactantsProductsAsync(await command.ExecuteReaderAsync());
}

public async Task<IReadOnlyList<ReactantsProducts>> GetProductsAsync(Guid reaction_product_list_uuid)
{
using var connection = await database.OpenConnectionAsync();
using var command = connection.CreateCommand();

command.CommandText = "SELECT Reactant_Product_List.reactant_product_uuid, Reactant_Product_List.reaction_uuid, Reactant_Product_List.species_uuid, Reactant_Product_List.quantity, Species.type " +
"FROM Reactant_Product_List LEFT JOIN Species ON species_uuid = uuid WHERE reactant_product_uuid = @reaction_product_list_uuid";
command.Parameters.AddWithValue("@reaction_product_list_uuid", reaction_product_list_uuid);
return await ReadAllReactantsProductsAsync(await command.ExecuteReaderAsync());
}

public async Task CreateReactantProductListAsync(ReactantProductList reactantProduct)
{
using var connection = await database.OpenConnectionAsync();
Expand Down Expand Up @@ -91,5 +114,26 @@ private async Task<IReadOnlyList<ReactantProductList>> ReadAllAsync(DbDataReader
}
return reactantProductList;
}

private async Task<IReadOnlyList<ReactantsProducts>> ReadAllReactantsProductsAsync(DbDataReader reader)
{
var reactantProductList = new List<ReactantsProducts>();
using (reader)
{
while (await reader.ReadAsync())
{
var property = new ReactantsProducts
{
reactant_product_uuid = reader.GetGuid(0),
reaction_uuid = reader.GetGuid(1),
species_uuid = reader.GetGuid(2),
quantity = reader.GetInt32(3),
type = reader.GetString(4)
};
reactantProductList.Add(property);
}
}
return reactantProductList;
}
}
}
Loading
Loading