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

Added FamilyMechList and Mechanism MCS #3

Merged
merged 2 commits into from
Mar 7, 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions Controllers/FamilyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand Down
57 changes: 57 additions & 0 deletions Controllers/FamilyMechListController.cs
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);
}
}
}
57 changes: 57 additions & 0 deletions Controllers/Mechanism.cs
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);
}
}
}
11 changes: 11 additions & 0 deletions Models/FamilyMechList.cs
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; }
}
}
9 changes: 9 additions & 0 deletions Models/Mechanism.cs
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; }
}
}
96 changes: 96 additions & 0 deletions Services/FamilyMechListService.cs
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();
}
Comment on lines +47 to +73
Copy link
Collaborator

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


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;
}
}
}
9 changes: 5 additions & 4 deletions Services/FamilyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IReadOnlyList<Family>> GetFamiliesAsync()
{
Expand Down Expand Up @@ -48,23 +49,23 @@ 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);

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();

command.CommandText = @"UPDATE Family SET isDel = 1 WHERE uuid = @uuid;";

command.Parameters.AddWithValue("@uuid", uuid);

await command.ExecuteNonQueryAsync();
}

Expand Down
Loading
Loading