diff --git a/.gitignore b/.gitignore index 104b544..b061e00 100644 --- a/.gitignore +++ b/.gitignore @@ -482,3 +482,33 @@ $RECYCLE.BIN/ # Vim temporary swap files *.swp + +bin +obj +.vs +.vs/Chemistry Cafe API/v17/.suo +.vs/Chemistry Cafe API/v17/.suo +.vs/Chemistry Cafe API/v17/.suo +.vs/Chemistry Cafe API/v17/DocumentLayout.json +bin/Debug/net8.0/Chemistry Cafe API.pdb +bin/Debug/net8.0/Chemistry Cafe API.dll +bin/Debug/net8.0/Chemistry Cafe API.exe +obj/Chemistry Cafe API.csproj.nuget.dgspec.json +obj/Chemistry Cafe API.csproj.nuget.g.props +obj/Debug/net8.0/apphost.exe +obj/Debug/net8.0/ApiEndpoints.json +obj/Debug/net8.0/Chemistry Cafe API.AssemblyInfo.cs +obj/Debug/net8.0/Chemistry Cafe API.AssemblyInfoInputs.cache +obj/Debug/net8.0/Chemistry Cafe API.assets.cache +obj/Debug/net8.0/Chemistry Cafe API.csproj.AssemblyReference.cache +obj/Debug/net8.0/Chemistry Cafe API.csproj.CoreCompileInputs.cache +obj/Debug/net8.0/Chemistry Cafe API.csproj.FileListAbsolute.txt +obj/Debug/net8.0/Chemistry Cafe API.dll +obj/Debug/net8.0/Chemistry Cafe API.GeneratedMSBuildEditorConfig.editorconfig +obj/Debug/net8.0/Chemistry Cafe API.genruntimeconfig.cache +obj/Debug/net8.0/Chemistry Cafe API.pdb +obj/Debug/net8.0/Chemistry Cafe API.sourcelink.json +obj/Debug/net8.0/ref/Chemistry Cafe API.dll +obj/Debug/net8.0/refint/Chemistry Cafe API.dll +obj/project.assets.json +obj/project.nuget.cache diff --git a/Controllers/FamilyController.cs b/Controllers/FamilyController.cs index ca8b92c..ab04fcc 100644 --- a/Controllers/FamilyController.cs +++ b/Controllers/FamilyController.cs @@ -12,7 +12,7 @@ namespace Chemistry_Cafe_API.Controllers public class FamilyController : ControllerBase { private FamilyService familyService; - + //Injects sql data source setup in Program.cs public FamilyController([FromServices] MySqlDataSource db) { @@ -40,7 +40,7 @@ public async Task CreateFamily([FromBody] string name) await familyService.CreateFamilyAsync(name); } - // PUT api/family/5 + // PUT api/Family/5 [HttpPut("update")] public async Task Put([FromBody] Family family) { @@ -49,7 +49,7 @@ public async Task Put([FromBody] Family family) // DELETE api/Family/delete/5 [HttpDelete("delete/{uuid}")] - public async Task Delete(int uuid) + public async Task Delete(Guid uuid) { await familyService.DeleteFamilyAsync(uuid); } diff --git a/Controllers/FamilyMechListController.cs b/Controllers/FamilyMechListController.cs new file mode 100644 index 0000000..cc7a023 --- /dev/null +++ b/Controllers/FamilyMechListController.cs @@ -0,0 +1,57 @@ +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 FamilyMechListController : ControllerBase + { + private FamilyMechListService familyMechListService; + + //Injects sql data source setup in Program.cs + public FamilyMechListController([FromServices] MySqlDataSource db) + { + this.familyMechListService = new FamilyMechListService(db); + } + + // GET: api/FamilyMechList/all + [HttpGet("all")] + public async Task> Get() + { + return await familyMechListService.GetFamiliesAsync(); + } + + // GET api/FamilyMechList/5 + [HttpGet("{uuid}")] + public async Task GetFamilyAsync(Guid uuid) + { + return await familyMechListService.GetFamilyMechListAsync(uuid); + } + + // POST api/FamilyMechList/create + [HttpPost("create")] + public async Task CreateFamily([FromBody] FamilyMechList newFamilyMechList) + { + await familyMechListService.CreateFamilyMechListAsync(newFamilyMechList); + } + + // PUT api/FamilyMechList/5 + [HttpPut("update")] + public async Task Put([FromBody] FamilyMechList newFamilyMechList) + { + await familyMechListService.UpdateFamilyMechListAsync(newFamilyMechList); + } + + // DELETE api/FamilyMechList/delete/5 + [HttpDelete("delete/{uuid}")] + public async Task Delete(Guid uuid) + { + await familyMechListService.DeleteFamilyMechListAsync(uuid); + } + } +} diff --git a/Controllers/Mechanism.cs b/Controllers/Mechanism.cs new file mode 100644 index 0000000..12220b4 --- /dev/null +++ b/Controllers/Mechanism.cs @@ -0,0 +1,57 @@ +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 MechanismController : ControllerBase + { + private MechanismService mechanismService; + + //Injects sql data source setup in Program.cs + public MechanismController([FromServices] MySqlDataSource db) + { + this.mechanismService = new MechanismService(db); + } + + // GET: api/Mechanism/all + [HttpGet("all")] + public async Task> Get() + { + return await mechanismService.GetFamiliesAsync(); + } + + // GET api/Mechanism/5 + [HttpGet("{uuid}")] + public async Task GetMechanismAsync(Guid uuid) + { + return await mechanismService.GetMechanismAsync(uuid); + } + + // POST api/Mechanism/create + [HttpPost("create")] + public async Task CreateMechanism([FromBody] string name) + { + await mechanismService.CreateMechanismAsync(name); + } + + // PUT api/Mechanism/5 + [HttpPut("update")] + public async Task Put([FromBody] Mechanism mechanism) + { + await mechanismService.UpdateMechanismAsync(mechanism); + } + + // DELETE api/Mechanism/delete/5 + [HttpDelete("delete/{uuid}")] + public async Task Delete(Guid uuid) + { + await mechanismService.DeleteMechanismAsync(uuid); + } + } +} diff --git a/Models/FamilyMechList.cs b/Models/FamilyMechList.cs new file mode 100644 index 0000000..b30ec56 --- /dev/null +++ b/Models/FamilyMechList.cs @@ -0,0 +1,11 @@ +namespace Chemistry_Cafe_API.Models +{ + public class FamilyMechList + { + public Guid uuid { get; set; } + public Guid family_uuid { get; set; } + public Guid mechanism_uuid { get; set; } + public string? version { get; set; } + public bool isDel { get; set; } + } +} diff --git a/Models/Mechanism.cs b/Models/Mechanism.cs new file mode 100644 index 0000000..b8baa0d --- /dev/null +++ b/Models/Mechanism.cs @@ -0,0 +1,9 @@ +namespace Chemistry_Cafe_API.Models +{ + public class Mechanism + { + public Guid uuid { get; set; } + public string? name { get; set; } + public bool isDel { get; set; } + } +} diff --git a/Services/FamilyMechListService.cs b/Services/FamilyMechListService.cs new file mode 100644 index 0000000..625b038 --- /dev/null +++ b/Services/FamilyMechListService.cs @@ -0,0 +1,96 @@ +using Chemistry_Cafe_API.Models; +using System.Data.Common; +using MySqlConnector; +using Microsoft.AspNetCore.Mvc; + +namespace Chemistry_Cafe_API.Services +{ + public class FamilyMechListService (MySqlDataSource database) + { + public async Task> GetFamiliesAsync() + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = "SELECT * FROM Family_Mechanism_List"; + return await ReadAllAsync(await command.ExecuteReaderAsync()); + } + + public async Task GetFamilyMechListAsync(Guid uuid) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = @"SELECT * FROM Family_Mechanism_List WHERE uuid = @id"; + command.Parameters.AddWithValue("@id", uuid); + + var result = await ReadAllAsync(await command.ExecuteReaderAsync()); + return result.FirstOrDefault(); + } + + public async Task CreateFamilyMechListAsync(FamilyMechList newFamilyMechList) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + Guid familyMechListID = Guid.NewGuid(); + + command.CommandText = @"INSERT INTO Family_Mechanism_List (uuid, family_uuid, mechanism_uuid, version) VALUES (@uuid, @family_uuid, @mechanism_uuid, @version);"; + + command.Parameters.AddWithValue("@uuid", familyMechListID); + command.Parameters.AddWithValue("@family_uuid", newFamilyMechList.family_uuid); + command.Parameters.AddWithValue("@mechanism_uuid", newFamilyMechList.mechanism_uuid); + command.Parameters.AddWithValue("@version", newFamilyMechList.version); + + await command.ExecuteNonQueryAsync(); + } + public async Task UpdateFamilyMechListAsync(FamilyMechList familyMechList) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = @"UPDATE Family_Mechanism_List SET family_uuid = @family_uuid, mechanism_uuid = @mechanism_uuid, version = @version, isDel = @isDel WHERE uuid = @uuid;"; + + command.Parameters.AddWithValue("@uuid", familyMechList.uuid); + command.Parameters.AddWithValue("@family_uuid", familyMechList.family_uuid); + command.Parameters.AddWithValue("@mechanism_uuid", familyMechList.mechanism_uuid); + command.Parameters.AddWithValue("@version", familyMechList.version); + command.Parameters.AddWithValue("@isDel", familyMechList.isDel); + + await command.ExecuteNonQueryAsync(); + } + + public async Task DeleteFamilyMechListAsync(Guid uuid) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = @"UPDATE Family_Mechanism_List SET isDel = 1 WHERE uuid = @uuid;"; + + command.Parameters.AddWithValue("@uuid", uuid); + + await command.ExecuteNonQueryAsync(); + } + + private async Task> ReadAllAsync(DbDataReader reader) + { + var familyMechList = new List(); + using (reader) + { + while (await reader.ReadAsync()) + { + var familyMech = new FamilyMechList + { + uuid = reader.GetGuid(0), + family_uuid = reader.GetGuid(1), + mechanism_uuid = reader.GetGuid(2), + version = reader.GetString(3), + isDel = reader.GetBoolean(4), + }; + familyMechList.Add(familyMech); + } + } + return familyMechList; + } + } +} diff --git a/Services/FamilyService.cs b/Services/FamilyService.cs index 9997a24..d43e2a8 100644 --- a/Services/FamilyService.cs +++ b/Services/FamilyService.cs @@ -3,9 +3,10 @@ using MySqlConnector; using Microsoft.AspNetCore.Mvc; + namespace Chemistry_Cafe_API.Services { - public class FamilyService (MySqlDataSource database) + public class FamilyService(MySqlDataSource database) { public async Task> GetFamiliesAsync() { @@ -48,7 +49,7 @@ public async Task UpdateFamilyAsync(Family family) using var command = connection.CreateCommand(); command.CommandText = @"UPDATE Family SET name = @name, isDel = @isDel WHERE uuid = @uuid;"; - + command.Parameters.AddWithValue("@uuid", family.uuid); command.Parameters.AddWithValue("@name", family.name); command.Parameters.AddWithValue("@isDel", family.isDel); @@ -56,7 +57,7 @@ public async Task UpdateFamilyAsync(Family family) await command.ExecuteNonQueryAsync(); } - public async Task DeleteFamilyAsync(int uuid) + public async Task DeleteFamilyAsync(Guid uuid) { using var connection = await database.OpenConnectionAsync(); using var command = connection.CreateCommand(); @@ -64,7 +65,7 @@ public async Task DeleteFamilyAsync(int uuid) command.CommandText = @"UPDATE Family SET isDel = 1 WHERE uuid = @uuid;"; command.Parameters.AddWithValue("@uuid", uuid); - + await command.ExecuteNonQueryAsync(); } diff --git a/Services/MechanismService.cs b/Services/MechanismService.cs new file mode 100644 index 0000000..ec49e86 --- /dev/null +++ b/Services/MechanismService.cs @@ -0,0 +1,90 @@ +using Chemistry_Cafe_API.Models; +using System.Data.Common; +using MySqlConnector; +using Microsoft.AspNetCore.Mvc; + +namespace Chemistry_Cafe_API.Services +{ + public class MechanismService(MySqlDataSource database) + { + public async Task> GetFamiliesAsync() + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = "SELECT * FROM Mechanism"; + return await ReadAllAsync(await command.ExecuteReaderAsync()); + } + + public async Task GetMechanismAsync(Guid uuid) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = @"SELECT * FROM Mechanism WHERE uuid = @id"; + command.Parameters.AddWithValue("@id", uuid); + + var result = await ReadAllAsync(await command.ExecuteReaderAsync()); + return result.FirstOrDefault(); + } + + public async Task CreateMechanismAsync(string name) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + Guid mechanismID = Guid.NewGuid(); + + command.CommandText = @"INSERT INTO Mechanism (uuid, name) VALUES (@uuid, @name);"; + + command.Parameters.AddWithValue("@uuid", mechanismID); + command.Parameters.AddWithValue("@name", name); + + await command.ExecuteNonQueryAsync(); + } + public async Task UpdateMechanismAsync(Mechanism mechanism) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = @"UPDATE Mechanism SET name = @name, isDel = @isDel WHERE uuid = @uuid;"; + + command.Parameters.AddWithValue("@uuid", mechanism.uuid); + command.Parameters.AddWithValue("@name", mechanism.name); + command.Parameters.AddWithValue("@isDel", mechanism.isDel); + + await command.ExecuteNonQueryAsync(); + } + + public async Task DeleteMechanismAsync(Guid uuid) + { + using var connection = await database.OpenConnectionAsync(); + using var command = connection.CreateCommand(); + + command.CommandText = @"UPDATE Mechanism SET isDel = 1 WHERE uuid = @uuid;"; + + command.Parameters.AddWithValue("@uuid", uuid); + + await command.ExecuteNonQueryAsync(); + } + + private async Task> ReadAllAsync(DbDataReader reader) + { + var mechanisms = new List(); + using (reader) + { + while (await reader.ReadAsync()) + { + var mechanism = new Mechanism + { + uuid = reader.GetGuid(0), + name = reader.GetString(1), + isDel = reader.GetBoolean(2), + }; + mechanisms.Add(mechanism); + } + } + return mechanisms; + } + } +}