Implementing ConsoleMenu for CRUD

TODO: CREATE, DELETE, PUT
This commit is contained in:
TypoMustakes
2024-01-08 16:59:30 +01:00
parent 0538bac931
commit ab97684c9a

View File

@@ -1,116 +1,251 @@
using System; using System;
using ConsoleTools;
using WD7UVN_HFT_2023241.Models; using WD7UVN_HFT_2023241.Models;
namespace WD7UVN_HFT_2023241.Client namespace WD7UVN_HFT_2023241.Client
{ {
public class CRUD
{
private enum Models
{
Customer,
Employee,
Service,
MaintainerTeam
}
private static void MethodChooser(CRUDActions action, Models model)
{
switch (model)
{
case Models.Customer:
switch (action)
{
case CRUDActions.GetAll:
ReadAll.Customer();
break;
case CRUDActions.GetById:
Read.Customer();
break;
case CRUDActions.Create:
Create.Customer();
break;
case CRUDActions.Update:
Update.Customer();
break;
case CRUDActions.Delete:
Delete.Customer();
break;
}
break;
case Models.Employee:
switch (action)
{
case CRUDActions.GetAll:
ReadAll.Employee();
break;
case CRUDActions.GetById:
Read.Employee();
break;
case CRUDActions.Create:
Create.Employee();
break;
case CRUDActions.Update:
Update.Employee();
break;
case CRUDActions.Delete:
Delete.Employee();
break;
}
break;
case Models.Service:
switch (action)
{
case CRUDActions.GetAll:
ReadAll.Service();
break;
case CRUDActions.GetById:
Read.Service();
break;
case CRUDActions.Create:
Create.Service();
break;
case CRUDActions.Update:
Update.Service();
break;
case CRUDActions.Delete:
Delete.Service();
break;
}
break;
case Models.MaintainerTeam:
switch (action)
{
case CRUDActions.GetAll:
ReadAll.MaintainerTeam();
break;
case CRUDActions.GetById:
Read.MaintainerTeam();
break;
case CRUDActions.Create:
Create.MaintainerTeam();
break;
case CRUDActions.Update:
Update.MaintainerTeam();
break;
case CRUDActions.Delete:
Delete.MaintainerTeam();
break;
}
break;
}
}
public static void TypeSelectorMenu(CRUDActions action)
{
var menu = new ConsoleMenu();
menu.Add("Customer", () => MethodChooser(action, Models.Customer));
menu.Add("Employee", () => MethodChooser(action, Models.Employee));
menu.Add("Service", () => MethodChooser(action, Models.Service));
menu.Add("Maintainer team", () => MethodChooser(action, Models.MaintainerTeam));
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
//generic methods are nice and all, but in this instance they would be a pain to implement as they would neccessitate a lot of reflection
// public static void Read<T>()
// {
// var menu = new ConsoleMenu();
// foreach (T c in RestService.Get<T>("/api/" + typeof(T).Name + "/"))
// {
// menu.Add($"{c}", () => Console.WriteLine(c));
// }
// menu.Add("Back", ConsoleMenu.Close);
// menu.Show();
// }
}
public class Read public class Read
{ {
private static void RESTCustomer(int id)
{
Customer res = RestService.Get<Customer>(id, "/api/Customer/");
if (res != null)
{
Console.WriteLine("Name: " + res.NAME);
Console.WriteLine("ID: " + res.ID);
Console.WriteLine("Email: " + res.EMAIL);
Console.WriteLine("Phone: " + res.PHONE);
}
else
{
Console.WriteLine("No such database entry was found :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void Customer() public static void Customer()
{ {
var menu = new ConsoleMenu();
int input; foreach (Customer c in RestService.Get<Customer>("/api/Customer/"))
while (true)
{ {
try menu.Add($"{c.NAME}: {c.ID}", () => RESTCustomer(c.ID));
{
Console.Write("Enter a number for the customer's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
} }
menu.Add("Back", ConsoleMenu.Close);
Customer c = RestService.Get<Customer>(input, "/api/Customer/"); menu.Show();
Console.WriteLine("Name: " + c.NAME);
Console.WriteLine("ID: " + c.ID);
Console.WriteLine("Email: " + c.EMAIL);
Console.WriteLine("Phone: " + c.PHONE);
Console.WriteLine("ID of used service: " + c.SERVICE_ID);
} }
private static void RESTEmployee(int id)
{
Employee res = RestService.Get<Employee>(id, "/api/Employee/");
if (res != null)
{
Console.WriteLine("Name: " + res.NAME);
Console.WriteLine("ID: " + res.ID);
Console.WriteLine("Email: " + res.EMAIL);
Console.WriteLine("Phone: " + res.PHONE);
Console.WriteLine("Maintainer team's ID: " + res.MAINTAINER_ID);
Console.WriteLine("Manager's ID: " + res.MANAGER_ID);
}
else
{
Console.WriteLine("No such database entry was found :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void Employee() public static void Employee()
{ {
var menu = new ConsoleMenu();
int input; foreach (Employee c in RestService.Get<Employee>("/api/Employee/"))
while (true)
{ {
try menu.Add($"{c.NAME}: {c.ID}", () => RESTEmployee(c.ID));
{
Console.Write("Enter a number for the employee's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
} }
menu.Add("Back", ConsoleMenu.Close);
Employee c = RestService.Get<Employee>(input, "/api/Employee/"); menu.Add("Exit", () => Environment.Exit(0));
Console.WriteLine("Name: " + c.NAME); menu.Show();
Console.WriteLine("ID: " + c.ID);
Console.WriteLine("Email: " + c.EMAIL);
Console.WriteLine("Phone: " + c.PHONE);
Console.WriteLine("Maintainer team's ID: " + c.MAINTAINER_ID);
Console.WriteLine("Manager's ID: " + c.MANAGER_ID);
} }
private static void RESTService(int id)
{
Service res = RestService.Get<Service>(id, "/api/Service/");
if (res != null)
{
Console.WriteLine("Name: " + res.NAME);
Console.WriteLine("ID: " + res.ID);
Console.WriteLine("Account: " + res.ACCOUNT);
Console.WriteLine("IP: " + res.IP);
Console.WriteLine("Port: " + res.PORT);
Console.WriteLine("Notes: " + res.NOTES);
Console.WriteLine("Service domain: " + res.SERVICE_DOMAIN);
Console.WriteLine("Version: " + res.VERSION);
Console.WriteLine("Maintainer team's ID: " + res.MAINTAINER_ID);
}
else
{
Console.WriteLine("No such database entry was found :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void Service() public static void Service()
{ {
var menu = new ConsoleMenu();
int input; foreach (Service c in RestService.Get<Service>("/api/Service/"))
while (true)
{ {
try menu.Add($"{c.NAME}: {c.ID}", () => RESTService(c.ID));
{
Console.Write("Enter a number for the service's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
} }
menu.Add("Back", ConsoleMenu.Close);
Service c = RestService.Get<Service>(input, "/api/Service/"); menu.Show();
Console.WriteLine("Name: " + c.NAME);
Console.WriteLine("ID: " + c.ID);
Console.WriteLine("Account: " + c.ACCOUNT);
Console.WriteLine("IP: " + c.IP);
Console.WriteLine("Port: " + c.PORT);
Console.WriteLine("Notes: " + c.NOTES);
Console.WriteLine("Service domain: " + c.SERVICE_DOMAIN);
Console.WriteLine("Version: " + c.VERSION);
Console.WriteLine("Maintainer team's ID: " + c.MAINTAINER_ID);
} }
private static void RESTMaintainerTeam(int id)
{
MaintainerTeam res = RestService.Get<MaintainerTeam>(id, "/api/MaintainerTeam/");
if (res != null)
{
Console.WriteLine("Name: " + res.NAME);
Console.WriteLine("ID: " + res.ID);
Console.WriteLine("Email: " + res.EMAIL);
Console.WriteLine("Leader's ID: " + res.LEADER_ID);
}
else
{
Console.WriteLine("No such database entry was found :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void MaintainerTeam() public static void MaintainerTeam()
{ {
var menu = new ConsoleMenu();
int input; foreach (MaintainerTeam c in RestService.Get<MaintainerTeam>("/api/MaintainerTeam/"))
while (true)
{ {
try menu.Add($"{c.NAME}: {c.ID}", () => RESTMaintainerTeam(c.ID));
{
Console.Write("Enter a number for the team's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
} }
menu.Add("Back", ConsoleMenu.Close);
MaintainerTeam c = RestService.Get<MaintainerTeam>(input, "/api/MaintainerTeam/"); menu.Show();
Console.WriteLine("Name: " + c.NAME);
Console.WriteLine("ID: " + c.ID);
Console.WriteLine("Email: " + c.EMAIL);
Console.WriteLine("Leader's ID: " + c.LEADER_ID);
} }
} }
@@ -118,7 +253,6 @@ namespace WD7UVN_HFT_2023241.Client
{ {
public static void Customer() public static void Customer()
{ {
foreach (Customer c in RestService.Get<Customer>("/api/Customer/")) foreach (Customer c in RestService.Get<Customer>("/api/Customer/"))
{ {
Console.WriteLine("Name: " + c.NAME); Console.WriteLine("Name: " + c.NAME);
@@ -128,11 +262,13 @@ namespace WD7UVN_HFT_2023241.Client
Console.WriteLine("ID of used service: " + c.SERVICE_ID); Console.WriteLine("ID of used service: " + c.SERVICE_ID);
Console.WriteLine(); Console.WriteLine();
} }
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
} }
public static void Employee() public static void Employee()
{ {
foreach (Employee c in RestService.Get<Employee>("/api/Employee/")) foreach (Employee c in RestService.Get<Employee>("/api/Employee/"))
{ {
Console.WriteLine("Name: " + c.NAME); Console.WriteLine("Name: " + c.NAME);
@@ -143,11 +279,13 @@ namespace WD7UVN_HFT_2023241.Client
Console.WriteLine("Manager's ID: " + c.MANAGER_ID); Console.WriteLine("Manager's ID: " + c.MANAGER_ID);
Console.WriteLine(); Console.WriteLine();
} }
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
} }
public static void Service() public static void Service()
{ {
foreach (Service c in RestService.Get<Service>("/api/Service/")) foreach (Service c in RestService.Get<Service>("/api/Service/"))
{ {
Console.WriteLine("Name: " + c.NAME); Console.WriteLine("Name: " + c.NAME);
@@ -161,11 +299,13 @@ namespace WD7UVN_HFT_2023241.Client
Console.WriteLine("Maintainer team's ID: " + c.MAINTAINER_ID); Console.WriteLine("Maintainer team's ID: " + c.MAINTAINER_ID);
Console.WriteLine(); Console.WriteLine();
} }
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
} }
public static void MaintainerTeam() public static void MaintainerTeam()
{ {
foreach (MaintainerTeam c in RestService.Get<MaintainerTeam>("/api/MaintainerTeam/")) foreach (MaintainerTeam c in RestService.Get<MaintainerTeam>("/api/MaintainerTeam/"))
{ {
Console.WriteLine("Name: " + c.NAME); Console.WriteLine("Name: " + c.NAME);
@@ -174,6 +314,9 @@ namespace WD7UVN_HFT_2023241.Client
Console.WriteLine("Leader's ID: " + c.LEADER_ID); Console.WriteLine("Leader's ID: " + c.LEADER_ID);
Console.WriteLine(); Console.WriteLine();
} }
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
} }
} }
@@ -181,7 +324,6 @@ namespace WD7UVN_HFT_2023241.Client
{ {
public static void Customer() public static void Customer()
{ {
while (true) while (true)
{ {
try try
@@ -368,7 +510,6 @@ namespace WD7UVN_HFT_2023241.Client
{ {
public static void Customer() public static void Customer()
{ {
int input; int input;
while (true) while (true)
{ {
@@ -639,28 +780,36 @@ namespace WD7UVN_HFT_2023241.Client
public class Delete public class Delete
{ {
private static void RESTCustomer(int id)
{
try
{
RestService.Delete(id, "/api/Customer/");
Console.WriteLine("Successfully deleted Customer with ID {0}", id);
}
catch (Exception e)
{
Console.WriteLine("Uh-oh...");
Console.WriteLine(e.Message);
}
}
public static void Customer() public static void Customer()
{ {
var menu = new ConsoleMenu();
int input; foreach (Customer c in RestService.Get<Customer>("/api/Customer/"))
while (true)
{ {
try menu.Add($"{c.NAME}: {c.ID}", () => RESTCustomer(c.ID));
{
Console.Write("Enter a number for the customer's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
} }
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
private static void RESTEmployee(int id)
{
try try
{ {
RestService.Delete(input, "/api/Customer/"); RestService.Delete(id, "/api/Employee/");
Console.WriteLine("Successfully deleted Customer with ID {0}", input); Console.WriteLine("Successfully deleted Employee with ID {0}", id);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -668,59 +817,23 @@ namespace WD7UVN_HFT_2023241.Client
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
} }
public static void Employee() public static void Employee()
{ {
var menu = new ConsoleMenu();
int input; foreach (Employee c in RestService.Get<Employee>("/api/Employee/"))
while (true)
{ {
try menu.Add($"{c.NAME}: {c.ID}", () => RESTEmployee(c.ID));
{
Console.Write("Enter a number for the employee's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
}
try
{
RestService.Delete(input, "/api/Employee/");
Console.WriteLine("Successfully deleted Employee with ID {0}", input);
}
catch (Exception e)
{
Console.WriteLine("Uh-oh...");
Console.WriteLine(e.Message);
} }
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
} }
public static void Service() private static void RESTService(int id)
{ {
int input;
while (true)
{
try
{
Console.Write("Enter a number for the service's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
}
try try
{ {
RestService.Delete(input, "/api/Service/"); RestService.Delete(id, "/api/Service/");
Console.WriteLine("Successfully deleted Service with ID {0}", input); Console.WriteLine("Successfully deleted Service with ID {0}", id);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -728,29 +841,23 @@ namespace WD7UVN_HFT_2023241.Client
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
} }
public static void Service()
public static void MaintainerTeam()
{ {
var menu = new ConsoleMenu();
int input; foreach (Service c in RestService.Get<Service>("/api/Service/"))
while (true)
{ {
try menu.Add($"{c.NAME}: {c.ID}", () => RESTService(c.ID));
{
Console.Write("Enter a number for the team's ID: ");
input = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a number.");
}
} }
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
private static void RESTMaintainerTeam(int id)
{
try try
{ {
RestService.Delete(input, "/api/MaintainerTeam/"); RestService.Delete(id, "/api/MaintainerTeam/");
Console.WriteLine("Successfully deleted MaintainerTeam with ID {0}", input); Console.WriteLine("Successfully deleted MaintainerTeam with ID {0}", id);
} }
catch (Exception e) catch (Exception e)
{ {
@@ -758,5 +865,15 @@ namespace WD7UVN_HFT_2023241.Client
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
} }
} }
public static void MaintainerTeam()
{
var menu = new ConsoleMenu();
foreach (MaintainerTeam c in RestService.Get<MaintainerTeam>("/api/MaintainerTeam/"))
{
menu.Add($"{c.NAME}: {c.ID}", () => RESTMaintainerTeam(c.ID));
}
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
} }
} }