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
Added FamilyMechList and Mechanism MCS #3
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,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<IReadOnlyList<FamilyMechList>> Get() | ||
{ | ||
return await familyMechListService.GetFamiliesAsync(); | ||
} | ||
|
||
// GET api/FamilyMechList/5 | ||
[HttpGet("{uuid}")] | ||
public async Task<FamilyMechList?> 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); | ||
} | ||
} | ||
} |
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,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<IReadOnlyList<Mechanism>> Get() | ||
{ | ||
return await mechanismService.GetFamiliesAsync(); | ||
} | ||
|
||
// GET api/Mechanism/5 | ||
[HttpGet("{uuid}")] | ||
public async Task<Mechanism?> 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); | ||
} | ||
} | ||
} |
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,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; } | ||
} | ||
} |
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,9 @@ | ||
namespace Chemistry_Cafe_API.Models | ||
{ | ||
public class Mechanism | ||
{ | ||
public Guid uuid { get; set; } | ||
public string? name { get; set; } | ||
public bool isDel { get; set; } | ||
} | ||
} |
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,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<IReadOnlyList<FamilyMechList>> 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<FamilyMechList?> 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<IReadOnlyList<FamilyMechList>> ReadAllAsync(DbDataReader reader) | ||
{ | ||
var familyMechList = new List<FamilyMechList>(); | ||
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; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the need to write SQL if you use LINQ