diff --git a/WD7UVN_HFT_2023241.Client/Shared/WD7UVN_HFT_2023241.Client.Shared.csproj b/WD7UVN_HFT_2023241.Client/Shared/WD7UVN_HFT_2023241.Client.Shared.csproj index 26dd80b..0ee4ce4 100644 --- a/WD7UVN_HFT_2023241.Client/Shared/WD7UVN_HFT_2023241.Client.Shared.csproj +++ b/WD7UVN_HFT_2023241.Client/Shared/WD7UVN_HFT_2023241.Client.Shared.csproj @@ -1,4 +1,4 @@ - + net5.0 diff --git a/WD7UVN_HFT_2023241.Client/TUI/CRUD.cs b/WD7UVN_HFT_2023241.Client/TUI/CRUD.cs index 176a7d1..fe4071a 100644 --- a/WD7UVN_HFT_2023241.Client/TUI/CRUD.cs +++ b/WD7UVN_HFT_2023241.Client/TUI/CRUD.cs @@ -1,10 +1,7 @@ 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 { diff --git a/WD7UVN_HFT_2023241.Client/TUI/NonCRUD.cs b/WD7UVN_HFT_2023241.Client/TUI/NonCRUD.cs index 67879a6..64e956a 100644 --- a/WD7UVN_HFT_2023241.Client/TUI/NonCRUD.cs +++ b/WD7UVN_HFT_2023241.Client/TUI/NonCRUD.cs @@ -1,8 +1,5 @@ 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 diff --git a/WD7UVN_HFT_2023241.Client/TUI/Program.cs b/WD7UVN_HFT_2023241.Client/TUI/Program.cs index e6061cc..4f9b769 100644 --- a/WD7UVN_HFT_2023241.Client/TUI/Program.cs +++ b/WD7UVN_HFT_2023241.Client/TUI/Program.cs @@ -2,7 +2,6 @@ using System.Threading; using System.Threading.Tasks; using ConsoleTools; -using WD7UVN_HFT_2023241.Client.Shared; namespace WD7UVN_HFT_2023241.Client.TUI { diff --git a/WD7UVN_HFT_2023241.Client/TUI/RestService.cs b/WD7UVN_HFT_2023241.Client/TUI/RestService.cs new file mode 100644 index 0000000..65907bc --- /dev/null +++ b/WD7UVN_HFT_2023241.Client/TUI/RestService.cs @@ -0,0 +1,264 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Http; +using System.Net.Security; +using WD7UVN_HFT_2023241.Models; + +namespace WD7UVN_HFT_2023241.Client.TUI +{ + public class RestService + { + private static HttpClient client; + + private static bool Ping(string url) + { + try + { + ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); + + WebClient wc = new WebClient(); + wc.DownloadData(url); + return true; + } + catch + { + return false; + } + } + + public static void Init(string baseurl = "https://localhost:5001", string pingableEndpoint = "/swagger") + { + int tries = 0; + bool isOk = false; + do + { + isOk = Ping(baseurl + pingableEndpoint); + tries++; + } while (isOk == false && tries < 5); + + if (isOk == false) + { + throw new EndpointNotAvailableException("Endpoint is not available!"); + } + + HttpClientHandler handler = new HttpClientHandler(); + handler.ClientCertificateOptions = ClientCertificateOption.Manual; + handler.ServerCertificateCustomValidationCallback = + (httpRequestMessage, cert, cetChain, policyErrors) => + { + return true; + }; + + client = new HttpClient(handler); + client.BaseAddress = new Uri(baseurl); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add( + new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue + ("application/json")); + try + { + client.GetAsync("").GetAwaiter().GetResult(); + } + catch (HttpRequestException e) + { + Console.WriteLine(e.Message); + throw new ArgumentException("Endpoint is not available!"); + } + + } + + public static List Get(string endpoint) + { + List items = new List(); + HttpResponseMessage response = client.GetAsync(endpoint).GetAwaiter().GetResult(); + if (response.IsSuccessStatusCode) + { + items = response.Content.ReadAsAsync>().GetAwaiter().GetResult(); + } + else + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + return items; + } + + public static T GetSingle(string endpoint) + { + T item = default(T); + HttpResponseMessage response = client.GetAsync(endpoint).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 T Get(int id, string endpoint) + { + T item = default(T); + HttpResponseMessage response = client.GetAsync(endpoint + 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 void Post(T item, string endpoint) + { + HttpResponseMessage response = + client.PostAsJsonAsync(endpoint, item).GetAwaiter().GetResult(); + + if (!response.IsSuccessStatusCode) + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + response.EnsureSuccessStatusCode(); + } + + public static void Delete(int id, string endpoint) + { + HttpResponseMessage response = + client.DeleteAsync(endpoint + id.ToString()).GetAwaiter().GetResult(); + + if (!response.IsSuccessStatusCode) + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + + response.EnsureSuccessStatusCode(); + } + + public static void Put(T item, string endpoint) + { + HttpResponseMessage response = + client.PutAsJsonAsync(endpoint, item).GetAwaiter().GetResult(); + + if (!response.IsSuccessStatusCode) + { + var error = response.Content.ReadAsAsync().GetAwaiter().GetResult(); + throw new ArgumentException(error.Msg); + } + + response.EnsureSuccessStatusCode(); + } + + public static List WhoWorksInMaintainerTeam(int id) + { + var item = default(List); + HttpResponseMessage response = client.GetAsync("/api/WhoWorksInMaintainerTeam?id=" + 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 List GetSubordinates(int id) + { + var item = default(List); + HttpResponseMessage response = client.GetAsync("/api/GetSubordinates?id=" + 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 List WhoUsesService(int id) + { + var item = default(List); + HttpResponseMessage response = client.GetAsync("/api/WhoUsesService?id=" + 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=" + 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 List WhoMaintainsService(int id) + { + var item = default(List); + HttpResponseMessage response = client.GetAsync("/api/WhoMaintainsService?id=" + 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 + { + public RestExceptionInfo() + { + + } + public string Msg { get; set; } + } + + public class EndpointNotAvailableException : Exception + { + public EndpointNotAvailableException() : base("The client could not reach the API endpoint.") + { + } + + public EndpointNotAvailableException(string message) : base(message) + { + } + + public EndpointNotAvailableException(string message, Exception innerException) : base(message, innerException) + { + } + } +} diff --git a/WD7UVN_HFT_2023241.Client/TUI/WD7UVN_HFT_2023241.Client.TUI.csproj b/WD7UVN_HFT_2023241.Client/TUI/WD7UVN_HFT_2023241.Client.TUI.csproj index 1157649..f0af545 100644 --- a/WD7UVN_HFT_2023241.Client/TUI/WD7UVN_HFT_2023241.Client.TUI.csproj +++ b/WD7UVN_HFT_2023241.Client/TUI/WD7UVN_HFT_2023241.Client.TUI.csproj @@ -7,11 +7,11 @@ - - + + diff --git a/WD7UVN_HFT_2023241.sln b/WD7UVN_HFT_2023241.sln index 36d6821..29b79c0 100644 --- a/WD7UVN_HFT_2023241.sln +++ b/WD7UVN_HFT_2023241.sln @@ -21,9 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared", "Shared", "{18D51E EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WD7UVN_HFT_2023241.Client.TUI", "WD7UVN_HFT_2023241.Client\TUI\WD7UVN_HFT_2023241.Client.TUI.csproj", "{F68C5B1A-C3B9-4AFC-BBD3-9DCC02A22EA0}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WD7UVN_HFT_2023241.Client.Shared", "WD7UVN_HFT_2023241.Client\Shared\WD7UVN_HFT_2023241.Client.Shared.csproj", "{77C8BEDB-0D81-4703-8FBC-B4DCD32584BD}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WD7UVN_SzTGUI_2023242.Client.WPF", "WD7UVN_SzTGUI_2023242.Client.WPF\WD7UVN_SzTGUI_2023242.Client.WPF.csproj", "{0A994D03-066F-43E7-872E-4D239FC6F764}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WD7UVN_SzTGUI_2023242.Client.WPF", "WD7UVN_SzTGUI_2023242.Client.WPF\WD7UVN_SzTGUI_2023242.Client.WPF.csproj", "{0A994D03-066F-43E7-872E-4D239FC6F764}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -55,10 +53,6 @@ Global {F68C5B1A-C3B9-4AFC-BBD3-9DCC02A22EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU {F68C5B1A-C3B9-4AFC-BBD3-9DCC02A22EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU {F68C5B1A-C3B9-4AFC-BBD3-9DCC02A22EA0}.Release|Any CPU.Build.0 = Release|Any CPU - {77C8BEDB-0D81-4703-8FBC-B4DCD32584BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {77C8BEDB-0D81-4703-8FBC-B4DCD32584BD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {77C8BEDB-0D81-4703-8FBC-B4DCD32584BD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {77C8BEDB-0D81-4703-8FBC-B4DCD32584BD}.Release|Any CPU.Build.0 = Release|Any CPU {0A994D03-066F-43E7-872E-4D239FC6F764}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {0A994D03-066F-43E7-872E-4D239FC6F764}.Debug|Any CPU.Build.0 = Debug|Any CPU {0A994D03-066F-43E7-872E-4D239FC6F764}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -74,7 +68,6 @@ Global {316342CA-7521-4A01-8BDB-5AA76D4DE855} = {E7A96BA5-BFD6-4265-A90F-880015BEFE07} {63F3B585-3E8C-41F5-8D6C-FB59932D0819} = {E7A96BA5-BFD6-4265-A90F-880015BEFE07} {F68C5B1A-C3B9-4AFC-BBD3-9DCC02A22EA0} = {8FABCF39-D228-4E36-927D-09B4CCF88935} - {77C8BEDB-0D81-4703-8FBC-B4DCD32584BD} = {8FABCF39-D228-4E36-927D-09B4CCF88935} {0A994D03-066F-43E7-872E-4D239FC6F764} = {8FABCF39-D228-4E36-927D-09B4CCF88935} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution diff --git a/WD7UVN_SzTGUI_2023242.Client.WPF/MainWindow.xaml b/WD7UVN_SzTGUI_2023242.Client.WPF/MainWindow.xaml index c7c69d8..b8215ac 100644 --- a/WD7UVN_SzTGUI_2023242.Client.WPF/MainWindow.xaml +++ b/WD7UVN_SzTGUI_2023242.Client.WPF/MainWindow.xaml @@ -5,8 +5,79 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WD7UVN_SzTGUI_2023242.Client.WPF" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> + Title="Company database manager" Height="450" Width="900"> + + + + + + + + + + + + + + + + +