diff --git a/WD7UVN_HFT_2023241.Client/CRUD.cs b/WD7UVN_HFT_2023241.Client/CRUD.cs new file mode 100644 index 0000000..81e42da --- /dev/null +++ b/WD7UVN_HFT_2023241.Client/CRUD.cs @@ -0,0 +1,778 @@ +using System; +using WD7UVN_HFT_2023241.Models; + +namespace WD7UVN_HFT_2023241.Client +{ + public class Read + { + public static void Customer() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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."); + } + } + + Customer c = RestService.Get(input, "/api/Customer/"); + 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); + } + + public static void Employee() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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."); + } + } + + Employee c = RestService.Get(input, "/api/Employee/"); + Console.WriteLine("Name: " + c.NAME); + 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); + } + + public static void Service() + { + Console.Clear(); + + 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."); + } + } + + Service c = RestService.Get(input, "/api/Service/"); + 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); + } + + public static void MaintainerTeam() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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."); + } + } + + MaintainerTeam c = RestService.Get(input, "/api/MaintainerTeam/"); + Console.WriteLine("Name: " + c.NAME); + Console.WriteLine("ID: " + c.ID); + Console.WriteLine("Email: " + c.EMAIL); + Console.WriteLine("Leader's ID: " + c.LEADER_ID); + } + } + + public class ReadAll + { + public static void Customer() + { + Console.Clear(); + + foreach (Customer c in RestService.Get("/api/Customer")) + { + 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); + } + } + + public static void Employee() + { + Console.Clear(); + + foreach (Employee c in RestService.Get("/api/Employee")) + { + Console.WriteLine("Name: " + c.NAME); + 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); + } + } + + public static void Service() + { + Console.Clear(); + + foreach (Service c in RestService.Get("/api/Service")) + { + 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); + } + } + + public static void MaintainerTeam() + { + Console.Clear(); + + foreach (MaintainerTeam c in RestService.Get("/api/MaintainerTeam")) + { + Console.WriteLine("Name: " + c.NAME); + Console.WriteLine("ID: " + c.ID); + Console.WriteLine("Email: " + c.EMAIL); + Console.WriteLine("Leader's ID: " + c.LEADER_ID); + } + } + } + + public class Create + { + public static void Customer() + { + Console.Clear(); + + while (true) + { + try + { + Console.Write("ID (integer): "); + int id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Email (text): "); + string email = Console.ReadLine(); + + Console.Write("Phone (text): "); + string phone = Console.ReadLine(); + + Console.Write("ServiceID (integer): "); + int service_id = Convert.ToInt32(Console.ReadLine()); + + Customer c = new Customer() + { + ID = id, + NAME = name, + EMAIL = email, + PHONE = phone, + SERVICE_ID = service_id + }; + + RestService.Put(c, "/api/Customer"); + + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + + public static void Employee() + { + Console.Clear(); + + while (true) + { + try + { + Console.Write("ID (integer): "); + int id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Email (text): "); + string email = Console.ReadLine(); + + Console.Write("Phone (text): "); + string phone = Console.ReadLine(); + + Console.Write("Team ID (integer): "); + int maintainer_id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Manager's ID (integer): "); + int manager_id = Convert.ToInt32(Console.ReadLine()); + + Employee c = new Employee() + { + ID = id, + NAME = name, + EMAIL = email, + PHONE = phone, + MAINTAINER_ID = maintainer_id, + MANAGER_ID = manager_id + }; + + RestService.Put(c, "/api/Employee"); + + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + + public static void Service() + { + Console.Clear(); + + while (true) + { + try + { + Console.Write("ID (integer): "); + int id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Maintainer team's ID (integer): "); + int maintainer_id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Version (integer): "); + int version = Convert.ToInt32(Console.ReadLine()); + + Console.Write("IP (text): "); + string ip = Console.ReadLine(); + + Console.Write("Port (integer): "); + int port = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Service account (text): "); + string account = Console.ReadLine(); + + Console.Write("Service domain (text): "); + string domain = Console.ReadLine(); + + Console.Write("Note(s) (text): "); + string notes = Console.ReadLine(); + + Service c = new Service() + { + ID = id, + NAME = name, + MAINTAINER_ID = maintainer_id, + PORT = port, + IP = ip, + SERVICE_DOMAIN = domain, + ACCOUNT = account, + NOTES = notes, + VERSION = version + }; + + RestService.Put(c, "/api/Service"); + + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + + public static void MaintainerTeam() + { + Console.Clear(); + + while (true) + { + try + { + Console.Write("ID (integer): "); + int id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Email (text): "); + string email = Console.ReadLine(); + + Console.Write("Leader's ID (integer): "); + int leader_id = Convert.ToInt32(Console.ReadLine()); + + MaintainerTeam c = new MaintainerTeam() + { + ID = id, + NAME = name, + LEADER_ID = leader_id, + EMAIL = email + }; + + RestService.Put(c, "/api/MaintainerTeam"); + + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + } + + public class Update + { + public static void Customer() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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."); + } + } + + Customer c = RestService.Get(input, "/api/Customer/"); + + Console.WriteLine("Name: " + c.NAME); + Console.WriteLine("Email: " + c.EMAIL); + Console.WriteLine("Phone: " + c.PHONE); + Console.WriteLine("ID of used service: " + c.SERVICE_ID); + + Console.WriteLine("Now you can modify these fields, except for the ID."); + + while (true) + { + try + { + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Email (text): "); + string email = Console.ReadLine(); + + Console.Write("Phone (text): "); + string phone = Console.ReadLine(); + + Console.Write("ServiceID (integer): "); + int service_id = Convert.ToInt32(Console.ReadLine()); + + c = new Customer() + { + ID = input, + NAME = name, + EMAIL = email, + PHONE = phone, + SERVICE_ID = service_id + }; + + RestService.Post(c, "/api/Customer"); + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + + public static void Employee() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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."); + } + } + + Employee c = RestService.Get(input, "/api/Employee/"); + + Console.WriteLine("Name: " + c.NAME); + 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); + + Console.WriteLine("Now you can modify these fields, except for the ID."); + + while (true) + { + try + { + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Email (text): "); + string email = Console.ReadLine(); + + Console.Write("Phone (text): "); + string phone = Console.ReadLine(); + + Console.Write("Team ID (integer): "); + int maintainer_id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Manager's ID (integer): "); + int manager_id = Convert.ToInt32(Console.ReadLine()); + + c = new Employee() + { + ID = input, + NAME = name, + EMAIL = email, + PHONE = phone, + MANAGER_ID = manager_id, + MAINTAINER_ID = maintainer_id + }; + + RestService.Post(c, "/api/Employee"); + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + + public static void Service() + { + Console.Clear(); + + 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."); + } + } + + Service c = RestService.Get(input, "/api/Service/"); + + 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); + Console.WriteLine("Now you can modify these fields, except for the ID."); + + while (true) + { + try + { + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Maintainer team's ID (integer): "); + int maintainer_id = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Version (integer): "); + int version = Convert.ToInt32(Console.ReadLine()); + + Console.Write("IP (text): "); + string ip = Console.ReadLine(); + + Console.Write("Port (integer): "); + int port = Convert.ToInt32(Console.ReadLine()); + + Console.Write("Service account (text): "); + string account = Console.ReadLine(); + + Console.Write("Service domain (text): "); + string domain = Console.ReadLine(); + + Console.Write("Note(s) (text): "); + string notes = Console.ReadLine(); + + c = new Service() + { + ID = input, + NAME = name, + MAINTAINER_ID = maintainer_id, + PORT = port, + IP = ip, + SERVICE_DOMAIN = domain, + ACCOUNT = account, + NOTES = notes, + VERSION = version + }; + + RestService.Post(c, "/api/Service"); + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + + public static void MaintainerTeam() + { + Console.Clear(); + + 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."); + } + } + + MaintainerTeam c = RestService.Get(input, "/api/MaintainerTeam/"); + + Console.WriteLine("Name: " + c.NAME); + Console.WriteLine("ID: " + c.ID); + Console.WriteLine("Email: " + c.EMAIL); + Console.WriteLine("Leader's ID: " + c.LEADER_ID); + + Console.WriteLine("Now you can modify these fields, except for the ID."); + + while (true) + { + try + { + Console.Write("Name (text): "); + string name = Console.ReadLine(); + + Console.Write("Email (text): "); + string email = Console.ReadLine(); + + Console.Write("Leader's ID (integer): "); + int leader_id = Convert.ToInt32(Console.ReadLine()); + + c = new MaintainerTeam() + { + ID = input, + NAME = name, + LEADER_ID = leader_id, + EMAIL = email + }; + + RestService.Post(c, "/api/MaintainerTeam"); + break; + } + catch (FormatException) + { + Console.WriteLine("Invalid format!"); + } + } + } + } + + public class Delete + { + public static void Customer() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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."); + } + } + + try + { + RestService.Delete(input, "/api/Customer/"); + Console.WriteLine("Successfully deleted Customer with ID {0}", input); + } + catch (Exception e) + { + Console.WriteLine("Uh-oh..."); + Console.WriteLine(e.Message); + } + } + + public static void Employee() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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); + } + } + + public static void Service() + { + Console.Clear(); + + 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 + { + RestService.Delete(input, "/api/Service/"); + Console.WriteLine("Successfully deleted Service with ID {0}", input); + } + catch (Exception e) + { + Console.WriteLine("Uh-oh..."); + Console.WriteLine(e.Message); + } + } + + public static void MaintainerTeam() + { + Console.Clear(); + + int input; + while (true) + { + try + { + 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."); + } + } + + try + { + RestService.Delete(input, "/api/MaintainerTeam/"); + Console.WriteLine("Successfully deleted MaintainerTeam with ID {0}", input); + } + catch (Exception e) + { + Console.WriteLine("Uh-oh..."); + Console.WriteLine(e.Message); + } + } + } +} \ No newline at end of file diff --git a/WD7UVN_HFT_2023241.Client/NonCRUD.cs b/WD7UVN_HFT_2023241.Client/NonCRUD.cs new file mode 100644 index 0000000..45e2049 --- /dev/null +++ b/WD7UVN_HFT_2023241.Client/NonCRUD.cs @@ -0,0 +1,216 @@ +using System; +using System.Linq; +using WD7UVN_HFT_2023241.Models; + +namespace WD7UVN_HFT_2023241.Client +{ + public class NonCRUD + { + + public static void WhoMaintainsService() + { + foreach (Service s in RestService.Get("/api/Service")) + { + Console.WriteLine("Service: " + s.NAME); + Console.WriteLine("ID: " + s.ID); + } + + int id = 1; + bool flag = true; + while (flag) + { + try + { + Console.Write("Your choice: "); + id = Convert.ToInt32(Console.ReadLine()); + flag = false; + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } + + IQueryable res = RestService.WhoMaintainsService(id); + if (res.Count() != 0) + { + foreach (Employee e in res) + { + Console.WriteLine("Név: " + e.NAME); + Console.WriteLine("ID: " + e.ID); + Console.WriteLine("Email: " + e.EMAIL); + Console.WriteLine("Phone: " + e.PHONE); + Console.WriteLine("Manager's ID: " + e.MANAGER_ID); + } + } + else + { + Console.WriteLine("No such database entry was found :/"); + } + } + + public static void WhoUsesService() + { + foreach (Service s in RestService.Get("/api/Service")) + { + Console.WriteLine("Service: " + s.NAME); + Console.WriteLine("ID: " + s.ID); + } + + int id = 1; + bool flag = true; + while (flag) + { + try + { + Console.Write("Your choice: "); + id = Convert.ToInt32(Console.ReadLine()); + flag = false; + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } + + IQueryable res = RestService.WhoUsesService(id); + if (res.Count() != 0) + { + foreach (Customer c in res) + { + Console.WriteLine("Name: " + c.NAME); + Console.WriteLine("ID: " + c.ID); + Console.WriteLine("Email: " + c.EMAIL); + Console.WriteLine("Phone: " + c.PHONE); + } + } + else + { + Console.WriteLine("No such database entry was found :/"); + } + } + + public static void WhoIsResponsibleForService() + { + foreach (Service s in RestService.Get("/api/Service")) + { + Console.WriteLine("Service: " + s.NAME); + Console.WriteLine("ID: " + s.ID); + } + + int id = 1; + bool flag = true; + while (flag) + { + try + { + Console.Write("Your choice: "); + id = Convert.ToInt32(Console.ReadLine()); + flag = false; + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } + + Employee? res = RestService.WhoIsResponsibleForService(id); + 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 ID: " + res.MAINTAINER_ID); + } + else + { + Console.WriteLine("No such database entry was found :/"); + } + } + + public static void GetSubordinates() + { + foreach (Employee e in RestService.Get("/api/Employee")) + { + Console.WriteLine("Name: " + e.NAME); + Console.WriteLine("ID: " + e.ID); + } + + int id = 1; + bool flag = true; + while (flag) + { + try + { + Console.Write("Your choice: "); + id = Convert.ToInt32(Console.ReadLine()); + flag = false; + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } + + IQueryable res = RestService.GetSubordinates(id); + if (res.Count() != 0) + { + foreach (Employee e in res) + { + Console.WriteLine("Név: " + e.NAME); + Console.WriteLine("ID: " + e.ID); + Console.WriteLine("Email: " + e.EMAIL); + Console.WriteLine("Phone: " + e.PHONE); + Console.WriteLine("Maintainer team ID: " + e.MAINTAINER_ID); + } + } + else + { + Console.WriteLine("No such database entry was found :/"); + } + } + + public static void WhoWorksInMaintainerTeam() + { + foreach(MaintainerTeam m in RestService.Get("/api/MaintainerTeam")) + { + Console.WriteLine("Team name: " + m.NAME); + Console.WriteLine("ID: " + m.ID + "\n\n"); + } + + int id = 1; + bool flag = true; + while (flag) + { + try + { + Console.Write("Your choice: "); + id = Convert.ToInt32(Console.ReadLine()); + flag = false; + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } + + IQueryable res = RestService.WhoWorksInMaintainerTeam(id); + if (res.Count() != 0) + { + foreach (Employee e in RestService.WhoWorksInMaintainerTeam(id)) + { + Console.WriteLine("Név: " + e.NAME); + Console.WriteLine("ID: " + e.ID); + Console.WriteLine("Email: " + e.EMAIL); + Console.WriteLine("Phone: " + e.PHONE); + Console.WriteLine("Manager's ID: " + e.MANAGER_ID); + } + } + else + { + Console.WriteLine("No such database entry was found :/"); + } + } + } +} \ No newline at end of file diff --git a/WD7UVN_HFT_2023241.Client/Program.cs b/WD7UVN_HFT_2023241.Client/Program.cs index 91c832b..cffc223 100644 --- a/WD7UVN_HFT_2023241.Client/Program.cs +++ b/WD7UVN_HFT_2023241.Client/Program.cs @@ -1,62 +1,178 @@ using System; -using WD7UVN_HFT_2023241.Models; -using System.Collections.Generic; namespace WD7UVN_HFT_2023241.Client { class Program { + static bool FLAG = true; + static RestService rest = new RestService(); + static void Main(string[] args) { - RestService rest = new RestService(); + ActionMenuHandler(); + } - Console.WriteLine("All employees:\n"); - List employeeList = rest.Get("/api/Employee/"); - foreach (Employee e in employeeList) - { - Console.WriteLine( - e.NAME + - ": " + - e.ID - ); - } + static void ActionMenuHandler() + { + while (FLAG) + { + int choice = ActionMenu(); + if (choice == 6) + { + NonCRUDMenuHandler(); + } + else if (choice == 0) + { + FLAG = false; + } + else if (1 <= choice && choice <= 5) + { + switch (TypeSelectorMenu()) + { + case 1: + if (choice == 1) { Read.Employee(); } + else if (choice == 2) { ReadAll.Employee(); } + else if (choice == 3) { Create.Employee(); } + else if (choice == 4) { Update.Employee(); } + else if (choice == 5) { Delete.Employee(); } + break; + case 2: + if (choice == 1) { Read.Customer(); } + else if (choice == 2) { ReadAll.Customer(); } + else if (choice == 3) { Create.Customer(); } + else if (choice == 4) { Update.Customer(); } + else if (choice == 5) { Delete.Customer(); } + break; + case 3: + if (choice == 1) { Read.Service(); } + else if (choice == 2) { ReadAll.Service(); } + else if (choice == 3) { Create.Service(); } + else if (choice == 4) { Update.Service(); } + else if (choice == 5) { Delete.Service(); } + break; + case 4: + if (choice == 1) { Read.MaintainerTeam(); } + else if (choice == 2) { ReadAll.MaintainerTeam(); } + else if (choice == 3) { Create.MaintainerTeam(); } + else if (choice == 4) { Update.MaintainerTeam(); } + else if (choice == 5) { Delete.MaintainerTeam(); } + break; + } + } + } + } - Console.WriteLine("\n\nAll customers:\n"); - List customerList = rest.Get("/api/Customer/"); - foreach (Customer c in customerList) - { - Console.WriteLine( - c.NAME + - ": " + - c.ID - ); - } + static int TypeSelectorMenu() + { + Console.Clear(); + Console.WriteLine("1.) Employee"); + Console.WriteLine("2.) Customer"); + Console.WriteLine("3.) Service"); + Console.WriteLine("4.) MaintainerTeam"); - Console.WriteLine("\n\nMaintainer teams:\n"); - List maintainerTeamList = rest.Get("/api/MaintainerTeam/"); - foreach (MaintainerTeam m in maintainerTeamList) - { - Console.WriteLine( - m.NAME + - ": " + - m.ID - ); - } + while (true) + { + try + { + Console.Write("Your choice: "); + return Convert.ToInt32(Console.ReadLine()); + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } - Console.WriteLine("\n\nMaintained services:\n"); - List serviceList = rest.Get("/api/Service/"); - foreach (Service s in serviceList) - { - Console.WriteLine( - s.NAME + - ": " + - s.ID + - " -> " + - s.IP - + ":" + - s.PORT - ); - } + } + + static int ReadMenuHandler() + { + Console.Clear(); + Console.WriteLine("1.) "); + + while (true) + { + try + { + Console.Write("Your choice: "); + return Convert.ToInt32(Console.ReadLine()); + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } + } + + static void NonCRUDMenuHandler() + { + switch (NonCRUDMenu()) + { + case 1: + NonCRUD.WhoWorksInMaintainerTeam(); + break; + case 2: + NonCRUD.GetSubordinates(); + break; + case 3: + NonCRUD.WhoUsesService(); + break; + case 4: + NonCRUD.WhoIsResponsibleForService(); + break; + case 5: + NonCRUD.WhoMaintainsService(); + break; + } + } + + + static int NonCRUDMenu() + { + Console.Clear(); + Console.WriteLine("1.) Who works in specified maintainer team?"); + Console.WriteLine("2.) Get manager's subordinates"); + Console.WriteLine("3.) Which client(s) use(s) specified service?"); + Console.WriteLine("4.) Who (which manager) is responsible for a specified service?"); + Console.WriteLine("5.) Get all employees who maintain a specified service"); + + while (true) + { + try + { + Console.Write("Your choice: "); + return Convert.ToInt32(Console.ReadLine()); + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } + } + + static int ActionMenu() + { + Console.Clear(); + Console.WriteLine("1.) Read"); + Console.WriteLine("2.) ReadAll"); + Console.WriteLine("3.) Create"); + Console.WriteLine("4.) Update"); + Console.WriteLine("5.) Delete"); + Console.WriteLine("6.) Non-CRUD"); + Console.WriteLine("0.) Exit"); + + while (true) + { + try + { + Console.Write("Your choice: "); + return Convert.ToInt32(Console.ReadLine()); + } + catch (FormatException) + { + Console.WriteLine("You must enter a number"); + } + } } } } diff --git a/WD7UVN_HFT_2023241.Client/RestService.cs b/WD7UVN_HFT_2023241.Client/RestService.cs index 5b8a0d1..6cc097c 100644 --- a/WD7UVN_HFT_2023241.Client/RestService.cs +++ b/WD7UVN_HFT_2023241.Client/RestService.cs @@ -1,14 +1,16 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Http; using System.Net.Security; +using WD7UVN_HFT_2023241.Models; namespace WD7UVN_HFT_2023241.Client { class RestService { - HttpClient client; + private static HttpClient client; public RestService(string baseurl = "https://localhost:5001", string pingableEndpoint = "/swagger") { @@ -64,7 +66,7 @@ namespace WD7UVN_HFT_2023241.Client } - public List Get(string endpoint) + public static List Get(string endpoint) { List items = new List(); HttpResponseMessage response = client.GetAsync(endpoint).GetAwaiter().GetResult(); @@ -80,7 +82,7 @@ namespace WD7UVN_HFT_2023241.Client return items; } - public T GetSingle(string endpoint) + public static T GetSingle(string endpoint) { T item = default(T); HttpResponseMessage response = client.GetAsync(endpoint).GetAwaiter().GetResult(); @@ -96,7 +98,7 @@ namespace WD7UVN_HFT_2023241.Client return item; } - public T Get(int id, string endpoint) + public static T Get(int id, string endpoint) { T item = default(T); HttpResponseMessage response = client.GetAsync(endpoint + "/" + id.ToString()).GetAwaiter().GetResult(); @@ -112,7 +114,7 @@ namespace WD7UVN_HFT_2023241.Client return item; } - public void Post(T item, string endpoint) + public static void Post(T item, string endpoint) { HttpResponseMessage response = client.PostAsJsonAsync(endpoint, item).GetAwaiter().GetResult(); @@ -125,7 +127,7 @@ namespace WD7UVN_HFT_2023241.Client response.EnsureSuccessStatusCode(); } - public void Delete(int id, string endpoint) + public static void Delete(int id, string endpoint) { HttpResponseMessage response = client.DeleteAsync(endpoint + "/" + id.ToString()).GetAwaiter().GetResult(); @@ -139,7 +141,7 @@ namespace WD7UVN_HFT_2023241.Client response.EnsureSuccessStatusCode(); } - public void Put(T item, string endpoint) + public static void Put(T item, string endpoint) { HttpResponseMessage response = client.PutAsJsonAsync(endpoint, item).GetAwaiter().GetResult(); @@ -153,6 +155,85 @@ namespace WD7UVN_HFT_2023241.Client response.EnsureSuccessStatusCode(); } + public static IQueryable WhoWorksInMaintainerTeam(int id) + { + IQueryable item = default(IQueryable); + HttpResponseMessage response = client.GetAsync("/api/WhoWorksInMaintainerTeam/" + id.ToString()).GetAwaiter().GetResult(); + if (response.IsSuccessStatusCode) + { + item = response.Content.ReadAsAsync>().GetAwaiter().GetResult(); + } + else + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + return item; + } + + public static IQueryable GetSubordinates(int id) + { + IQueryable item = default(IQueryable); + HttpResponseMessage response = client.GetAsync("/api/GetSubordinates/" + id.ToString()).GetAwaiter().GetResult(); + if (response.IsSuccessStatusCode) + { + item = response.Content.ReadAsAsync>().GetAwaiter().GetResult(); + } + else + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + return item; + } + + public static IQueryable WhoUsesService(int id) + { + IQueryable item = default(IQueryable); + HttpResponseMessage response = client.GetAsync("/api/WhoUsesService/" + id.ToString()).GetAwaiter().GetResult(); + if (response.IsSuccessStatusCode) + { + item = response.Content.ReadAsAsync>().GetAwaiter().GetResult(); + } + else + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + return item; + } + + public static Employee WhoIsResponsibleForService(int id) + { + Employee item = default(Employee); + HttpResponseMessage response = client.GetAsync("/api/WhoIsResponsibleForService/" + id.ToString()).GetAwaiter().GetResult(); + if (response.IsSuccessStatusCode) + { + item = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + } + else + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + return item; + } + + public static IQueryable WhoMaintainsService(int id) + { + IQueryable item = default(IQueryable); + HttpResponseMessage response = client.GetAsync("/api/WhoMaintainsService/" + id.ToString()).GetAwaiter().GetResult(); + if (response.IsSuccessStatusCode) + { + item = response.Content.ReadAsAsync>().GetAwaiter().GetResult(); + } + else + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + return item; + } } public class RestExceptionInfo {