Added new WPF client and accomodated folder structure

This commit is contained in:
MiskolcziRichard
2024-03-26 11:35:07 +01:00
parent 9eb0b9ba0e
commit 12f0dd9cd8
14 changed files with 504 additions and 372 deletions

View File

@@ -0,0 +1,207 @@
using System;
using System.Linq;
using System.Net.NetworkInformation;
using System.Reflection;
using ConsoleTools;
using WD7UVN_HFT_2023241.Models;
using WD7UVN_HFT_2023241.Client.Shared;
namespace WD7UVN_HFT_2023241.Client.TUI
{
public class CRUD
{
public delegate void CRUDMethod<T>(T obj);
private static void MethodChooser<T>(CRUDActions action)
{
CRUDMethod<T> method = null;
switch (action)
{
case CRUDActions.GetAll:
method = View.DisplayAll;
break;
case CRUDActions.GetById:
method = View.Display;
break;
case CRUDActions.Create:
method = Modify.Create;
break;
case CRUDActions.Update:
method = Modify.Update;
break;
case CRUDActions.Delete:
method = Modify.Delete;
break;
}
ShowMenu<T>(method);
}
public static void TypeSelectorMenu(CRUDActions action)
{
var menu = new ConsoleMenu();
menu.Add("Customer", () => MethodChooser<Customer>(action));
menu.Add("Employee", () => MethodChooser<Employee>(action));
menu.Add("Service", () => MethodChooser<Service>(action));
menu.Add("Maintainer team", () => MethodChooser<MaintainerTeam>(action));
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
public static void ShowMenu<T>(CRUDMethod<T> method)
{
var menu = new ConsoleMenu();
if (method != Modify.Create && method != View.DisplayAll)
{
foreach (T c in RestService.Get<T>("/api/" + typeof(T).Name + "/"))
{
menu.Add($"{c.GetType().GetProperty("NAME").GetValue(c)}: {c.GetType().GetProperty("ID").GetValue(c)}", () => method(c));
}
}
else
{
method(default);
}
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
}
public class Modify
{
public static void Delete<T>(T c)
{
try
{
int key = (int)c.GetType().GetProperty("ID").GetValue(c);
RestService.Delete(key, "/api/" + typeof(T).Name + "/");
Console.WriteLine("Successfully deleted {0} with ID {1}", typeof(T).Name, key);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine("The following error occured:");
Console.WriteLine(e.Message);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
}
public static void Update<T>(T item)
{
View.Display(item);
Console.WriteLine("Now you can modify these fields, except for the ID.");
try
{
foreach (PropertyInfo p in item.GetType().GetProperties())
{
if (p.Name != "ID")
{
Console.Write(p.Name + " (" + p.PropertyType.Name + "): ");
string input = Console.ReadLine();
if (p.PropertyType.Name == "Int32")
{
p.SetValue(item, Convert.ToInt32(input));
}
else
{
p.SetValue(item, input);
}
}
}
RestService.Post<T>(item, "/api/" + typeof(T).Name + "/");
Console.WriteLine("\nSuccessfully updated {0} with ID {1}", typeof(T).Name, item.GetType().GetProperty("ID").GetValue(item));
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
catch (FormatException)
{
Console.WriteLine("Invalid format!");
}
}
public static void Create<T>(T? dummy = default)
{
try
{
T item = (T)Activator.CreateInstance(typeof(T));
Console.WriteLine("Please supply all neccessary fields!");
foreach (PropertyInfo p in item.GetType().GetProperties())
{
Console.Write(p.Name + " (" + p.PropertyType.Name + "): ");
string input = Console.ReadLine();
if (p.PropertyType.Name == "Int32")
{
p.SetValue(item, Convert.ToInt32(input));
}
else
{
p.SetValue(item, input);
}
}
RestService.Put<T>(item, "/api/" + typeof(T).Name + "/");
Console.WriteLine("\nSuccessfully created {0} with ID {1}", typeof(T).Name, item.GetType().GetProperty("ID").GetValue(item));
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
catch (FormatException)
{
Console.WriteLine("Invalid format!");
}
}
}
internal class View
{
internal static void Display<T>(T c)
{
if (c != null)
{
foreach (PropertyInfo p in c.GetType().GetProperties())
{
Console.WriteLine(p.Name + ": " + p.GetValue(c));
}
}
else
{
Console.WriteLine("No such database entry was found :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
internal static void DisplayAll<T>(T? dummy = default)
{
var items = RestService.Get<T>("/api/" + typeof(T).Name + "/");
if (items != null)
{
foreach (T item in items)
{
foreach (PropertyInfo p in item.GetType().GetProperties())
{
Console.WriteLine(p.Name + ": " + p.GetValue(item));
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("No such database entry was found :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
}
}

View File

@@ -0,0 +1,170 @@
using System;
using System.Linq;
using System.Collections.Generic;
using ConsoleTools;
using WD7UVN_HFT_2023241.Client.Shared;
using WD7UVN_HFT_2023241.Models;
namespace WD7UVN_HFT_2023241.Client.TUI
{
public class NonCRUD
{
private static void RESTWhoMaintainsService(int id)
{
var res = RestService.WhoMaintainsService(id);
if (res != null)
{
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("ERROR: No such database entry was found :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void WhoMaintainsService()
{
var menu = new ConsoleMenu();
foreach (Service s in RestService.Get<Service>("/api/Service"))
{
menu.Add($"{s.NAME}: {s.ID}", () => RESTWhoMaintainsService(s.ID));
}
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
private static void RESTWhoUsesService(int id)
{
var res = RestService.WhoUsesService(id);
if (res != null)
{
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 :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void WhoUsesService()
{
var menu = new ConsoleMenu();
foreach (Service s in RestService.Get<Service>("/api/Service"))
{
menu.Add($"{s.NAME}: {s.ID}", () => RESTWhoUsesService(s.ID));
}
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
private static void RESTWhoIsResponsibleForService(int id)
{
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 :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void WhoIsResponsibleForService()
{
var menu = new ConsoleMenu();
foreach (Service s in RestService.Get<Service>("/api/Service"))
{
menu.Add($"{s.NAME}: {s.ID}", () => RESTWhoIsResponsibleForService(s.ID));
}
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
private static void RESTGetSubordinates(int id)
{
var res = RestService.GetSubordinates(id);
if (res != null)
{
foreach (Employee e in res)
{
Console.WriteLine("Name: " + 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 :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void GetSubordinates()
{
var menu = new ConsoleMenu();
foreach (Employee e in RestService.Get<Employee>("/api/Employee"))
{
menu.Add($"{e.NAME}: {e.ID}", () => RESTGetSubordinates(e.ID));
}
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
private static void RESTWhoWorksInMaintainerTeam(int id)
{
var res = RestService.WhoWorksInMaintainerTeam(id);
if (res != null)
{
foreach (Employee e in res)
{
Console.WriteLine("Name: " + 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 :/");
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void WhoWorksInMaintainerTeam()
{
var menu = new ConsoleMenu();
foreach(MaintainerTeam m in RestService.Get<MaintainerTeam>("/api/MaintainerTeam"))
{
Console.WriteLine("Team name: " + m.NAME);
Console.WriteLine("ID: " + m.ID + "\n\n");
menu.Add($"{m.NAME}: {m.ID}", () => RESTWhoWorksInMaintainerTeam(m.ID));
}
menu.Add("Back", ConsoleMenu.Close);
menu.Show();
}
}
}

View File

@@ -0,0 +1,84 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using ConsoleTools;
using WD7UVN_HFT_2023241.Client.Shared;
namespace WD7UVN_HFT_2023241.Client.TUI
{
public enum CRUDActions
{
GetAll,
GetById,
Create,
Update,
Delete
}
public class Program
{
private static async Task Main(string[] args)
{
try
{
RestService.Init();
}
catch (EndpointNotAvailableException e)
{
Console.WriteLine(e.Message);
Environment.Exit(1);
}
// var commonConfig = new MenuConfig
// {
// Selector = "--> ",
// EnableFilter = true,
// EnableBreadcrumb = true,
// WriteBreadcrumbAction = titles => Console.WriteLine(string.Join(" / ", titles)),
// };
var nonCrudMenu = new ConsoleMenu(args, level: 2)
.Add("Who maintains specified service?", () => NonCRUD.WhoMaintainsService())
.Add("Get employee's subordinates", () => NonCRUD.GetSubordinates())
.Add("Get clients using a specified service", () => NonCRUD.WhoUsesService())
.Add("Get employees in specified maintainer team", () => NonCRUD.WhoWorksInMaintainerTeam())
.Add("Get employee responsible for specified service", () => NonCRUD.WhoIsResponsibleForService())
.Add("Back", ConsoleMenu.Close)
.Add("Exit", () => Environment.Exit(0))
//.Configure(commonConfig)
.Configure(config =>
{
config.Title = "Non-CRUD Operations";
});
var crudMenu = new ConsoleMenu(args, level: 1)
.Add("Get all", () => CRUD.TypeSelectorMenu(CRUDActions.GetAll))
.Add("Get by id", () => CRUD.TypeSelectorMenu(CRUDActions.GetById))
.Add("Create", () => CRUD.TypeSelectorMenu(CRUDActions.Create))
.Add("Update", () => CRUD.TypeSelectorMenu(CRUDActions.Update))
.Add("Delete", () => CRUD.TypeSelectorMenu(CRUDActions.Delete))
.Add("Back", ConsoleMenu.Close)
.Add("Exit", () => Environment.Exit(0))
//.Configure(commonConfig)
.Configure(config =>
{
config.Title = "Tables";
});
var menu = new ConsoleMenu(args, level: 0)
.Add("CRUD", crudMenu.Show)
.Add("Non-CRUD", nonCrudMenu.Show)
.Add("Exit", () => Environment.Exit(0))
//.Configure(commonConfig)
.Configure(config =>
{
config.Title = "Main menu";
config.EnableWriteTitle = true;
config.EnableBreadcrumb = true;
});
var token = new CancellationTokenSource().Token;
await menu.ShowAsync(token);
}
}
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\WD7UVN_HFT_2023241.Models\WD7UVN_HFT_2023241.Models.csproj" />
<ProjectReference Include="..\Shared\WD7UVN_HFT_2023241.Client.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ConsoleMenu-simple" Version="2.6.1" />
</ItemGroup>
</Project>