From 19153c9d0243f38ca9b0642c2313f511fca29887 Mon Sep 17 00:00:00 2001 From: Matthew Burke Date: Thu, 27 Jul 2023 19:15:49 -0400 Subject: [PATCH 1/7] Cleaned references and imports --- PWAPPv2/App.xaml.cs | 10 +---- PWAPPv2/MainWindow.xaml.cs | 40 +++++++------------ PWAPPv2/PWAPPv2.csproj | 1 - PWAPPv2/Properties/AssemblyInfo.cs | 2 - PWAPPv2/PwImage.cs | 6 --- PWAPPv2/Source/API/APIConfig.cs | 8 +--- PWAPPv2/Source/API/APIConnection.cs | 12 ++---- PWAPPv2/Source/API/APIRequestBuilder.cs | 10 +---- PWAPPv2/Source/DataObjects/APICredentials.cs | 8 +--- PWAPPv2/Source/DataObjects/Attachment.cs | 8 +--- PWAPPv2/Source/DataObjects/ComboBoxData.cs | 8 +--- PWAPPv2/Source/DataObjects/Exceptions.cs | 4 -- PWAPPv2/Source/DataObjects/NumValList.cs | 7 +--- PWAPPv2/Source/DataObjects/NumValPair.cs | 8 +--- PWAPPv2/Source/DataObjects/PWImage.cs | 7 +--- PWAPPv2/Source/DataObjects/ReferFromBox.cs | 4 -- PWAPPv2/Source/DataObjects/ReferToBox.cs | 4 -- PWAPPv2/Source/DataObjects/Referral.cs | 12 ++---- PWAPPv2/Source/DataObjects/ReferralTypeBox.cs | 12 ++---- PWAPPv2/Source/Database/DatabaseConfig.cs | 8 +--- PWAPPv2/Source/Database/DatabaseConnection.cs | 12 ++---- PWAPPv2/Source/Patient.cs | 6 +-- PWAPPv2/Source/PatientGUIAdapter.cs | 8 +--- 23 files changed, 48 insertions(+), 157 deletions(-) delete mode 100644 PWAPPv2/PwImage.cs diff --git a/PWAPPv2/App.xaml.cs b/PWAPPv2/App.xaml.cs index 1f66738..7e80dca 100644 --- a/PWAPPv2/App.xaml.cs +++ b/PWAPPv2/App.xaml.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Data; -using System.Linq; -using System.Threading.Tasks; -using System.Windows; +using System.Windows; namespace PWAPPv2 { @@ -16,7 +10,7 @@ namespace PWAPPv2 public static string[] Args; void app_Startup(object sender, StartupEventArgs e) { - if(e.Args.Length > 0) + if (e.Args.Length > 0) { Args = e.Args; } diff --git a/PWAPPv2/MainWindow.xaml.cs b/PWAPPv2/MainWindow.xaml.cs index 2e7c7ce..6b8c9e0 100644 --- a/PWAPPv2/MainWindow.xaml.cs +++ b/PWAPPv2/MainWindow.xaml.cs @@ -1,19 +1,9 @@ -using System; +using Microsoft.Win32; +using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -using System.IO; -using Microsoft.Win32; /** * TODO: @@ -49,7 +39,7 @@ namespace PWAPPv2 { args = App.Args; } - catch(Exception) + catch (Exception) { } images = new List(); @@ -122,15 +112,15 @@ namespace PWAPPv2 { TypeBox.GetSelectedID(); } - catch(Source.DataObjects.ReferralTypeDefaultException) + catch (Source.DataObjects.ReferralTypeDefaultException) { } - catch(Source.DataObjects.InvalidReferralTypeException) + catch (Source.DataObjects.InvalidReferralTypeException) { } - catch(NullReferenceException) + catch (NullReferenceException) { } @@ -141,12 +131,12 @@ namespace PWAPPv2 { this.Close(); } - + //OK Button private void Button_Click_1(object sender, RoutedEventArgs e) { Source.DataObjects.Referral referral; - if(images.Count == 0) + if (images.Count == 0) { referral = new Source.DataObjects.Referral(apiCreds, patient, (Source.DataObjects.ReferralTypeBox)TypeBox, @@ -167,9 +157,9 @@ namespace PWAPPv2 { string referralString = referral.ToJsonString(); string result = apiConnection.SendPostRequestAsync("api/PWMakeReferral", referralString); - if(images.Count > 0) + if (images.Count > 0) { - foreach(Source.DataObjects.PWImage im in images) + foreach (Source.DataObjects.PWImage im in images) { Source.DataObjects.Attachment att = new Source.DataObjects.Attachment(apiCreds, im, result); string json = att.ToJsonString(); @@ -183,7 +173,7 @@ namespace PWAPPv2 } this.Close(); } - catch(Source.DataObjects.Referral.InvalidReferalDataException) + catch (Source.DataObjects.Referral.InvalidReferalDataException) { } @@ -197,7 +187,7 @@ namespace PWAPPv2 openFileDialog.Filter = "Image files (*.jpg,*.jpeg)|*.jpg;*.jpeg"; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); - if(openFileDialog.ShowDialog() == true) + if (openFileDialog.ShowDialog() == true) { foreach (string filename in openFileDialog.FileNames) { @@ -205,7 +195,7 @@ namespace PWAPPv2 { images.Add(new Source.DataObjects.PWImage(filename)); } - catch(NullReferenceException) + catch (NullReferenceException) { images = new List(); images.Add(new Source.DataObjects.PWImage(filename)); @@ -222,7 +212,7 @@ namespace PWAPPv2 ImageList.Items.Add(CreateImageGridItem(image)); } } - catch(NullReferenceException) + catch (NullReferenceException) { } } @@ -230,7 +220,7 @@ namespace PWAPPv2 private void Button_Click_3(object sender, RoutedEventArgs e) { int index = ImageList.SelectedIndex; - if(index == -1) + if (index == -1) { return; } diff --git a/PWAPPv2/PWAPPv2.csproj b/PWAPPv2/PWAPPv2.csproj index 5f2858b..c100bc5 100644 --- a/PWAPPv2/PWAPPv2.csproj +++ b/PWAPPv2/PWAPPv2.csproj @@ -210,7 +210,6 @@ MSBuild:Compile Designer - diff --git a/PWAPPv2/Properties/AssemblyInfo.cs b/PWAPPv2/Properties/AssemblyInfo.cs index aef6f17..2318293 100644 --- a/PWAPPv2/Properties/AssemblyInfo.cs +++ b/PWAPPv2/Properties/AssemblyInfo.cs @@ -1,6 +1,4 @@ using System.Reflection; -using System.Resources; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Windows; diff --git a/PWAPPv2/PwImage.cs b/PWAPPv2/PwImage.cs deleted file mode 100644 index 6b5f444..0000000 --- a/PWAPPv2/PwImage.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace PWAPPv2 -{ - internal class PwImage - { - } -} \ No newline at end of file diff --git a/PWAPPv2/Source/API/APIConfig.cs b/PWAPPv2/Source/API/APIConfig.cs index 6d6eb52..6c11bce 100644 --- a/PWAPPv2/Source/API/APIConfig.cs +++ b/PWAPPv2/Source/API/APIConfig.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Xml; namespace PWAPPv2.Source @@ -21,12 +17,12 @@ namespace PWAPPv2.Source { cfgDoc.Load(path); } - catch(XmlException) + catch (XmlException) { Console.WriteLine("An error has occured parsing the config file."); return; } - catch(System.IO.FileNotFoundException) + catch (System.IO.FileNotFoundException) { Console.WriteLine("Config file could not be found!"); return; diff --git a/PWAPPv2/Source/API/APIConnection.cs b/PWAPPv2/Source/API/APIConnection.cs index 2efb4e5..fe6038c 100644 --- a/PWAPPv2/Source/API/APIConnection.cs +++ b/PWAPPv2/Source/API/APIConnection.cs @@ -1,13 +1,9 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; -using System.IO; -using Newtonsoft.Json; +using System.Text; namespace PWAPPv2.Source.API { @@ -82,7 +78,7 @@ namespace PWAPPv2.Source.API text = sr.ReadToEnd(); } } - catch(WebException wex) + catch (WebException wex) { text = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd(); } @@ -100,7 +96,7 @@ namespace PWAPPv2.Source.API var response = client.PostAsync(apiUri, contetnt).Result; - if(response.IsSuccessStatusCode) + if (response.IsSuccessStatusCode) { return response.Content.ReadAsStringAsync().Result; } diff --git a/PWAPPv2/Source/API/APIRequestBuilder.cs b/PWAPPv2/Source/API/APIRequestBuilder.cs index 48da6aa..692c885 100644 --- a/PWAPPv2/Source/API/APIRequestBuilder.cs +++ b/PWAPPv2/Source/API/APIRequestBuilder.cs @@ -1,11 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Xml; -using System.Xml.Serialization; -using Newtonsoft.Json; +using Newtonsoft.Json; +using System; namespace PWAPPv2.Source.API { diff --git a/PWAPPv2/Source/DataObjects/APICredentials.cs b/PWAPPv2/Source/DataObjects/APICredentials.cs index c90a014..5df1277 100644 --- a/PWAPPv2/Source/DataObjects/APICredentials.cs +++ b/PWAPPv2/Source/DataObjects/APICredentials.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PWAPPv2.Source.DataObjects +namespace PWAPPv2.Source.DataObjects { class APICredentials { diff --git a/PWAPPv2/Source/DataObjects/Attachment.cs b/PWAPPv2/Source/DataObjects/Attachment.cs index 2ef9eec..3c516c7 100644 --- a/PWAPPv2/Source/DataObjects/Attachment.cs +++ b/PWAPPv2/Source/DataObjects/Attachment.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PWAPPv2.Source.DataObjects +namespace PWAPPv2.Source.DataObjects { class Attachment { diff --git a/PWAPPv2/Source/DataObjects/ComboBoxData.cs b/PWAPPv2/Source/DataObjects/ComboBoxData.cs index 70f7736..f424081 100644 --- a/PWAPPv2/Source/DataObjects/ComboBoxData.cs +++ b/PWAPPv2/Source/DataObjects/ComboBoxData.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; +using System.Windows.Controls; namespace PWAPPv2.Source.DataObjects { diff --git a/PWAPPv2/Source/DataObjects/Exceptions.cs b/PWAPPv2/Source/DataObjects/Exceptions.cs index cf5bb7b..f2bf798 100644 --- a/PWAPPv2/Source/DataObjects/Exceptions.cs +++ b/PWAPPv2/Source/DataObjects/Exceptions.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PWAPPv2.Source.DataObjects { diff --git a/PWAPPv2/Source/DataObjects/NumValList.cs b/PWAPPv2/Source/DataObjects/NumValList.cs index d63cf08..8211398 100644 --- a/PWAPPv2/Source/DataObjects/NumValList.cs +++ b/PWAPPv2/Source/DataObjects/NumValList.cs @@ -1,8 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PWAPPv2.Source.DataObjects { @@ -18,11 +15,11 @@ namespace PWAPPv2.Source.DataObjects public NumValList(string Content, char Delim) { string[] split = Content.Split(Delim); - if((split.Length % 2) != 0) + if ((split.Length % 2) != 0) { throw new UnevenValuesException(); } - for(int i = 0; i < split.Length; i++) + for (int i = 0; i < split.Length; i++) { this.Add(int.Parse(split[i]), split[i + 1]); i++; diff --git a/PWAPPv2/Source/DataObjects/NumValPair.cs b/PWAPPv2/Source/DataObjects/NumValPair.cs index f6bc7bd..5628a32 100644 --- a/PWAPPv2/Source/DataObjects/NumValPair.cs +++ b/PWAPPv2/Source/DataObjects/NumValPair.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PWAPPv2.Source.DataObjects +namespace PWAPPv2.Source.DataObjects { class NumValPair { diff --git a/PWAPPv2/Source/DataObjects/PWImage.cs b/PWAPPv2/Source/DataObjects/PWImage.cs index 30ad628..2f5ce19 100644 --- a/PWAPPv2/Source/DataObjects/PWImage.cs +++ b/PWAPPv2/Source/DataObjects/PWImage.cs @@ -1,12 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Media.Imaging; -using System.Windows.Media; using System.Drawing; using System.IO; +using System.Windows.Media.Imaging; namespace PWAPPv2.Source.DataObjects { diff --git a/PWAPPv2/Source/DataObjects/ReferFromBox.cs b/PWAPPv2/Source/DataObjects/ReferFromBox.cs index b0128ff..742472c 100644 --- a/PWAPPv2/Source/DataObjects/ReferFromBox.cs +++ b/PWAPPv2/Source/DataObjects/ReferFromBox.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; using System.Windows.Controls; namespace PWAPPv2.Source.DataObjects diff --git a/PWAPPv2/Source/DataObjects/ReferToBox.cs b/PWAPPv2/Source/DataObjects/ReferToBox.cs index 73c8d37..5267970 100644 --- a/PWAPPv2/Source/DataObjects/ReferToBox.cs +++ b/PWAPPv2/Source/DataObjects/ReferToBox.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; using System.Windows.Controls; namespace PWAPPv2.Source.DataObjects diff --git a/PWAPPv2/Source/DataObjects/Referral.cs b/PWAPPv2/Source/DataObjects/Referral.cs index 9a5458a..c361c74 100644 --- a/PWAPPv2/Source/DataObjects/Referral.cs +++ b/PWAPPv2/Source/DataObjects/Referral.cs @@ -1,11 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Windows; using System.Windows.Controls; using System.Windows.Documents; -using System.Windows; namespace PWAPPv2.Source.DataObjects { @@ -40,7 +36,7 @@ namespace PWAPPv2.Source.DataObjects string RemarksText = new TextRange(RemarksBox.Document.ContentStart, RemarksBox.Document.ContentEnd).Text; int contact = 0; - if(ContactBox.IsChecked == true) + if (ContactBox.IsChecked == true) { contact = 1; } @@ -76,11 +72,11 @@ namespace PWAPPv2.Source.DataObjects { MessageBox.Show("Please select a referral type"); } - catch(Source.DataObjects.ReferToDefaultException) + catch (Source.DataObjects.ReferToDefaultException) { MessageBox.Show("Please select a refer to"); } - catch(Source.DataObjects.ReferFromDefaultException) + catch (Source.DataObjects.ReferFromDefaultException) { MessageBox.Show("Please select a refer from"); } diff --git a/PWAPPv2/Source/DataObjects/ReferralTypeBox.cs b/PWAPPv2/Source/DataObjects/ReferralTypeBox.cs index 4d5b410..14635d4 100644 --- a/PWAPPv2/Source/DataObjects/ReferralTypeBox.cs +++ b/PWAPPv2/Source/DataObjects/ReferralTypeBox.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; using System.Windows.Controls; namespace PWAPPv2.Source.DataObjects @@ -28,7 +24,7 @@ namespace PWAPPv2.Source.DataObjects TypeID.Add(id); } - catch(Exception) + catch (Exception) { throw new DataInvalidException(); } @@ -36,7 +32,7 @@ namespace PWAPPv2.Source.DataObjects } - foreach(string name in TypeName) + foreach (string name in TypeName) { Add(name); } @@ -52,9 +48,9 @@ namespace PWAPPv2.Source.DataObjects { throw new ReferralTypeDefaultException(); } - for(int i = 0; i < TypeName.Count; i++) + for (int i = 0; i < TypeName.Count; i++) { - if(TypeName[i] == Selected) + if (TypeName[i] == Selected) { return TypeID[i]; } diff --git a/PWAPPv2/Source/Database/DatabaseConfig.cs b/PWAPPv2/Source/Database/DatabaseConfig.cs index 971b1a7..789b76e 100644 --- a/PWAPPv2/Source/Database/DatabaseConfig.cs +++ b/PWAPPv2/Source/Database/DatabaseConfig.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Xml; namespace PWAPPv2.Source.Database @@ -22,12 +18,12 @@ namespace PWAPPv2.Source.Database { cfg.Load(path); } - catch(XmlException) + catch (XmlException) { Console.WriteLine("An error has occured parsing the config file."); return; } - catch(System.IO.FileNotFoundException) + catch (System.IO.FileNotFoundException) { Console.WriteLine("Config file could not be found!"); } diff --git a/PWAPPv2/Source/Database/DatabaseConnection.cs b/PWAPPv2/Source/Database/DatabaseConnection.cs index 3d26098..c644ca7 100644 --- a/PWAPPv2/Source/Database/DatabaseConnection.cs +++ b/PWAPPv2/Source/Database/DatabaseConnection.cs @@ -1,10 +1,6 @@ -using System; +using MySqlConnector; +using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Microsoft.Data.SqlClient; -using MySqlConnector; namespace PWAPPv2.Source.Database { @@ -28,7 +24,7 @@ namespace PWAPPv2.Source.Database { Connection.Open(); } - catch(Exception) + catch (Exception) { throw new DatabaseConnectionException(); } @@ -99,7 +95,7 @@ namespace PWAPPv2.Source.Database { } - + class DatabaseConnectionException : Exception { } diff --git a/PWAPPv2/Source/Patient.cs b/PWAPPv2/Source/Patient.cs index c46a0da..30f9c4b 100644 --- a/PWAPPv2/Source/Patient.cs +++ b/PWAPPv2/Source/Patient.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; namespace PWAPPv2.Source { diff --git a/PWAPPv2/Source/PatientGUIAdapter.cs b/PWAPPv2/Source/PatientGUIAdapter.cs index aa6e5cc..3cd9d14 100644 --- a/PWAPPv2/Source/PatientGUIAdapter.cs +++ b/PWAPPv2/Source/PatientGUIAdapter.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PWAPPv2.Source +namespace PWAPPv2.Source { class PatientGUIAdapter { From 4a4bcc81e04948a8e9baf6f7c01bd2925b163a98 Mon Sep 17 00:00:00 2001 From: Matthew Burke Date: Mon, 14 Aug 2023 17:10:38 -0400 Subject: [PATCH 2/7] Created Unit Test Project --- PWAPPv2.sln | 6 ++ PWAPPv2/MainWindow.xaml | 2 +- PWAPPv2/MainWindow.xaml.cs | 38 ++++++---- PWAPPv2/Source/API/APIConfig.cs | 2 +- PWAPPv2/Source/API/APIConnection.cs | 35 +-------- PWAPPv2/Source/DataObjects/APICredentials.cs | 7 +- UnitTests/Properties/AssemblyInfo.cs | 20 ++++++ UnitTests/UnitTest1.cs | 29 ++++++++ UnitTests/UnitTests.csproj | 74 ++++++++++++++++++++ UnitTests/packages.config | 5 ++ 10 files changed, 167 insertions(+), 51 deletions(-) create mode 100644 UnitTests/Properties/AssemblyInfo.cs create mode 100644 UnitTests/UnitTest1.cs create mode 100644 UnitTests/UnitTests.csproj create mode 100644 UnitTests/packages.config diff --git a/PWAPPv2.sln b/PWAPPv2.sln index 9ee99ea..5b08ebc 100644 --- a/PWAPPv2.sln +++ b/PWAPPv2.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.33529.622 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PWAPPv2", "PWAPPv2\PWAPPv2.csproj", "{45E26AF8-41D7-4308-A2C8-D55A0350DB47}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{164B9220-F337-4E88-B619-0C52C502B9C4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {45E26AF8-41D7-4308-A2C8-D55A0350DB47}.Debug|Any CPU.Build.0 = Debug|Any CPU {45E26AF8-41D7-4308-A2C8-D55A0350DB47}.Release|Any CPU.ActiveCfg = Release|Any CPU {45E26AF8-41D7-4308-A2C8-D55A0350DB47}.Release|Any CPU.Build.0 = Release|Any CPU + {164B9220-F337-4E88-B619-0C52C502B9C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {164B9220-F337-4E88-B619-0C52C502B9C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {164B9220-F337-4E88-B619-0C52C502B9C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {164B9220-F337-4E88-B619-0C52C502B9C4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PWAPPv2/MainWindow.xaml b/PWAPPv2/MainWindow.xaml index 57a5183..920b7a5 100644 --- a/PWAPPv2/MainWindow.xaml +++ b/PWAPPv2/MainWindow.xaml @@ -69,7 +69,7 @@ - + diff --git a/PWAPPv2/MainWindow.xaml.cs b/PWAPPv2/MainWindow.xaml.cs index 6b8c9e0..3242c7b 100644 --- a/PWAPPv2/MainWindow.xaml.cs +++ b/PWAPPv2/MainWindow.xaml.cs @@ -33,6 +33,8 @@ namespace PWAPPv2 List images; + string ConfigPath = "C:\\PWAPP\\Config\\Config.xml"; + public MainWindow() { try @@ -41,28 +43,37 @@ namespace PWAPPv2 } catch (Exception) { } + try + { + images = new List(); - images = new List(); + apiconfig.LoadConfig(ConfigPath); + DataConfig = new Source.Database.DatabaseConfig(ConfigPath); + Source.Database.DatabaseConnection dbcon = new Source.Database.DatabaseConnection(DataConfig); - apiconfig.LoadConfig("./Config/Config.xml"); - DataConfig = new Source.Database.DatabaseConfig("./Config/Config.xml"); - Source.Database.DatabaseConnection dbcon = new Source.Database.DatabaseConnection(DataConfig); + apiCreds = new Source.DataObjects.APICredentials(apiconfig); - apiCreds = new Source.DataObjects.APICredentials(apiconfig); + apiConnection = new Source.API.APIConnection("http://apipatientweb.azurewebsites.net/", + apiCreds); - apiConnection = new Source.API.APIConnection("http://apipatientweb.azurewebsites.net/", - apiCreds); + InitializeComponent(); - InitializeComponent(); + PopulateComboBoxes(); - PopulateComboBoxes(); + patient = new Source.Patient(); - patient = new Source.Patient(); + patient.BuildFromDatabase(dbcon, args[0]); - patient.BuildFromDatabase(dbcon, args[0]); - - this.DataContext = new Source.PatientGUIAdapter(patient); + this.DataContext = new Source.PatientGUIAdapter(patient); + } + catch(NullReferenceException) + { + } + catch(Exception e) + { + MessageBox.Show(e.Message); + } } private void PopulateComboBoxes() @@ -244,7 +255,6 @@ namespace PWAPPv2 imageGrid.HorizontalAlignment = HorizontalAlignment.Left; imageGrid.VerticalAlignment = VerticalAlignment.Top; imageGrid.ShowGridLines = true; - imageGrid.Background = new SolidColorBrush(Colors.LightSteelBlue); ColumnDefinition imageColumn = new ColumnDefinition(); imageColumn.Width = new GridLength(50); diff --git a/PWAPPv2/Source/API/APIConfig.cs b/PWAPPv2/Source/API/APIConfig.cs index 6c11bce..3a88069 100644 --- a/PWAPPv2/Source/API/APIConfig.cs +++ b/PWAPPv2/Source/API/APIConfig.cs @@ -3,7 +3,7 @@ using System.Xml; namespace PWAPPv2.Source { - class APIConfig + public class APIConfig { public string PWUserID; public string PWPassword; diff --git a/PWAPPv2/Source/API/APIConnection.cs b/PWAPPv2/Source/API/APIConnection.cs index fe6038c..6847714 100644 --- a/PWAPPv2/Source/API/APIConnection.cs +++ b/PWAPPv2/Source/API/APIConnection.cs @@ -7,7 +7,7 @@ using System.Text; namespace PWAPPv2.Source.API { - class APIConnection + public class APIConnection { private static string BaseURL; @@ -52,39 +52,6 @@ namespace PWAPPv2.Source.API } } - public string APIPost() - { - string text; - try - { - HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://apipatientweb.azurewebsites.net/api/PWReferralTypes"); - request.Method = "POST"; - request.ContentType = "application/json; charset=\"utf-8\""; - request.Accept = "application/json"; - - string postData = Source.API.APIRequestBuilder.BuildJsonBodyRequest(Credentials); - - ASCIIEncoding encoding = new ASCIIEncoding(); - byte[] data = encoding.GetBytes(postData); - request.ContentLength = data.Length; - Stream newStream = request.GetRequestStream(); - newStream.Write(data, 0, data.Length); - newStream.Close(); - - var response = (HttpWebResponse)request.GetResponse(); - - using (var sr = new StreamReader(response.GetResponseStream())) - { - text = sr.ReadToEnd(); - } - } - catch (WebException wex) - { - text = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd(); - } - return text; - } - public string SendPostRequestAsync(string apiUri) { HttpClient client = new HttpClient(); diff --git a/PWAPPv2/Source/DataObjects/APICredentials.cs b/PWAPPv2/Source/DataObjects/APICredentials.cs index 5df1277..77e513c 100644 --- a/PWAPPv2/Source/DataObjects/APICredentials.cs +++ b/PWAPPv2/Source/DataObjects/APICredentials.cs @@ -1,12 +1,17 @@ namespace PWAPPv2.Source.DataObjects { - class APICredentials + public class APICredentials { public string UserID; public string Password; public string PracticeId; public string APIid; + public APICredentials() + { + + } + public APICredentials(Source.APIConfig config) { UserID = config.PWUserID; diff --git a/UnitTests/Properties/AssemblyInfo.cs b/UnitTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..806675f --- /dev/null +++ b/UnitTests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("UnitTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("UnitTests")] +[assembly: AssemblyCopyright("Copyright © 2023")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("164b9220-f337-4e88-b619-0c52c502b9c4")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UnitTests/UnitTest1.cs b/UnitTests/UnitTest1.cs new file mode 100644 index 0000000..3de928a --- /dev/null +++ b/UnitTests/UnitTest1.cs @@ -0,0 +1,29 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using PWAPPv2.Source.API; +using PWAPPv2.Source.DataObjects; + +// System.Threading.Tasks.Task.Result.get returned "\"1|Endo|2|Implant|3|Oral Surgery|4|Ortho|5|Pedo|6|Perio|7|Restore|8|Other \\n\"" string + +namespace UnitTests +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestV1Get() + { + APICredentials creds = new APICredentials(); + creds.UserID = "testdoctor"; + creds.Password = "testdoctor"; + creds.PracticeId = "210"; + creds.APIid = "12345678"; + + APIConnection conn = new APIConnection("http://apipatientweb.azurewebsites.net/", creds); + + string res = conn.SendPostRequestAsync("api/PWReferralTypes"); + Assert.AreEqual("\"1|Endo|2|Implant|3|Oral Surgery|4|Ortho|5|Pedo|6|Perio|7|Restore|8|Other \\n\"", res); + + } + } +} diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj new file mode 100644 index 0000000..720bce6 --- /dev/null +++ b/UnitTests/UnitTests.csproj @@ -0,0 +1,74 @@ + + + + + + Debug + AnyCPU + {164B9220-F337-4E88-B619-0C52C502B9C4} + Library + Properties + UnitTests + UnitTests + v4.7.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.2.1.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.2.1.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + {45e26af8-41d7-4308-a2c8-d55a0350db47} + PWAPPv2 + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/UnitTests/packages.config b/UnitTests/packages.config new file mode 100644 index 0000000..f84cb10 --- /dev/null +++ b/UnitTests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From 87a73ff24be7341c5fd94680ee4585aaefa40fbd Mon Sep 17 00:00:00 2001 From: Matthew Burke Date: Thu, 24 Aug 2023 12:45:56 -0400 Subject: [PATCH 3/7] Refactor to api connections. Update Checker. --- PWAPPv2/Config/PracticeConfig.xml | 10 ++ PWAPPv2/Config/UniversalConfig.txt | 5 + PWAPPv2/MainWindow.xaml.cs | 64 ++++++++--- PWAPPv2/PWAPPv2.csproj | 2 + PWAPPv2/Source/API/APIConfig.cs | 22 +++- PWAPPv2/Source/API/APIConnection.cs | 101 ++++++++++++------ PWAPPv2/Source/API/PWApiConnection.cs | 50 +++++++++ PWAPPv2/Source/Config/Configuration.cs | 54 ++++++++++ PWAPPv2/Source/DataObjects/APICredentials.cs | 8 ++ PWAPPv2/Source/Database/DatabaseConnection.cs | 9 ++ UnitTests/ConfigTest.cs | 21 ++++ UnitTests/PWConnectionTest.cs | 22 ++++ UnitTests/UnitTest1.cs | 30 ++++++ UnitTests/UnitTests.csproj | 2 + 14 files changed, 355 insertions(+), 45 deletions(-) create mode 100644 PWAPPv2/Config/PracticeConfig.xml create mode 100644 PWAPPv2/Config/UniversalConfig.txt create mode 100644 PWAPPv2/Source/API/PWApiConnection.cs create mode 100644 PWAPPv2/Source/Config/Configuration.cs create mode 100644 UnitTests/ConfigTest.cs create mode 100644 UnitTests/PWConnectionTest.cs diff --git a/PWAPPv2/Config/PracticeConfig.xml b/PWAPPv2/Config/PracticeConfig.xml new file mode 100644 index 0000000..2296f93 --- /dev/null +++ b/PWAPPv2/Config/PracticeConfig.xml @@ -0,0 +1,10 @@ + + + 10.0.1.107 + root + + testdoctor + testdoctor + 210 + 12345678 + \ No newline at end of file diff --git a/PWAPPv2/Config/UniversalConfig.txt b/PWAPPv2/Config/UniversalConfig.txt new file mode 100644 index 0000000..78fa985 --- /dev/null +++ b/PWAPPv2/Config/UniversalConfig.txt @@ -0,0 +1,5 @@ + + + http://apipatientweb.azurewebsites.net/ + https://localhost:44354/api/PWAppVersion + \ No newline at end of file diff --git a/PWAPPv2/MainWindow.xaml.cs b/PWAPPv2/MainWindow.xaml.cs index 3242c7b..efbe229 100644 --- a/PWAPPv2/MainWindow.xaml.cs +++ b/PWAPPv2/MainWindow.xaml.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Windows; using System.Windows.Controls; +using System.Windows.Forms; using System.Windows.Media; /** @@ -17,16 +18,22 @@ namespace PWAPPv2 /// public partial class MainWindow : Window { - static Source.APIConfig apiconfig = new Source.APIConfig(); + static Source.APIConfig apiconfig;// = new Source.APIConfig(); static Source.Database.DatabaseConfig DataConfig; static Source.API.APIConnection apiConnection; Source.DataObjects.APICredentials apiCreds; + Source.API.PWApiConnection pwapiConnection; + Source.DataObjects.ComboBoxData TypeBox; Source.DataObjects.ComboBoxData ToBox; Source.DataObjects.ComboBoxData FromBox; + Source.Config.Configuration practiceConfig; + Source.Config.Configuration universalConfig; + + string[] args; Source.Patient patient; @@ -43,18 +50,49 @@ namespace PWAPPv2 } catch (Exception) { } + + try + { + practiceConfig = new Source.Config.Configuration(".\\Config\\PracticeConfig.xml"); + universalConfig = new Source.Config.Configuration(".\\Config\\UniversalConfig.xml"); + } + catch + (Exception) + { + System.Windows.MessageBox.Show("An error has occured in the configurations files."); + } + + pwapiConnection = new Source.API.PWApiConnection(practiceConfig, universalConfig); + + try + { + if(pwapiConnection.CheckForUpdate() == true) + { + string message = "An update is available! Would you like to install it?"; + string title = "Update available!"; + MessageBoxButtons buttons = MessageBoxButtons.YesNo; + DialogResult result = System.Windows.Forms.MessageBox.Show(message, title, buttons); + if(result == System.Windows.Forms.DialogResult.Yes) + { + System.Windows.MessageBox.Show("HAHA NO UPDATE FOR YOU!"); + } + } + } + catch (Exception) + { + System.Windows.MessageBox.Show("An error has occured while checking for updates"); + } + try { images = new List(); - apiconfig.LoadConfig(ConfigPath); - DataConfig = new Source.Database.DatabaseConfig(ConfigPath); - Source.Database.DatabaseConnection dbcon = new Source.Database.DatabaseConnection(DataConfig); + Source.Database.DatabaseConnection dbcon = new Source.Database.DatabaseConnection(practiceConfig); + + apiconfig = new Source.APIConfig(practiceConfig, universalConfig); apiCreds = new Source.DataObjects.APICredentials(apiconfig); - - apiConnection = new Source.API.APIConnection("http://apipatientweb.azurewebsites.net/", - apiCreds); + apiConnection = new Source.API.APIConnection(universalConfig.Get("PWBaseURI"), apiCreds); InitializeComponent(); @@ -72,7 +110,7 @@ namespace PWAPPv2 } catch(Exception e) { - MessageBox.Show(e.Message); + System.Windows.MessageBox.Show(e.Message); } } @@ -176,11 +214,11 @@ namespace PWAPPv2 string json = att.ToJsonString(); apiConnection.SendPostRequestAsync("api/PWAttachment", json); } - MessageBox.Show("Referral added successfully!"); + System.Windows.MessageBox.Show("Referral added successfully!"); } else { - MessageBox.Show(result); + System.Windows.MessageBox.Show(result); } this.Close(); } @@ -193,12 +231,12 @@ namespace PWAPPv2 //AddImage Button private void Button_Click_2(object sender, RoutedEventArgs e) { - OpenFileDialog openFileDialog = new OpenFileDialog(); + System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog(); openFileDialog.Multiselect = true; openFileDialog.Filter = "Image files (*.jpg,*.jpeg)|*.jpg;*.jpeg"; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); - if (openFileDialog.ShowDialog() == true) + if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { foreach (string filename in openFileDialog.FileNames) { @@ -252,7 +290,7 @@ namespace PWAPPv2 { Grid imageGrid = new Grid(); imageGrid.Width = 477; - imageGrid.HorizontalAlignment = HorizontalAlignment.Left; + imageGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; imageGrid.VerticalAlignment = VerticalAlignment.Top; imageGrid.ShowGridLines = true; diff --git a/PWAPPv2/PWAPPv2.csproj b/PWAPPv2/PWAPPv2.csproj index c100bc5..c797ca4 100644 --- a/PWAPPv2/PWAPPv2.csproj +++ b/PWAPPv2/PWAPPv2.csproj @@ -213,6 +213,8 @@ + + diff --git a/PWAPPv2/Source/API/APIConfig.cs b/PWAPPv2/Source/API/APIConfig.cs index 3a88069..4d99093 100644 --- a/PWAPPv2/Source/API/APIConfig.cs +++ b/PWAPPv2/Source/API/APIConfig.cs @@ -1,4 +1,5 @@ -using System; +using PWAPPv2.Source.Config; +using System; using System.Xml; namespace PWAPPv2.Source @@ -10,6 +11,25 @@ namespace PWAPPv2.Source public string PWPracticeID; public string PWApiID; + public string PWBaseURI; + public string PWUpdateURI; + public string PWAppVersion; + + public APIConfig() { } + + public APIConfig(Config.Configuration practiceConfig, Config.Configuration universalConfig) + { + PWUserID = practiceConfig.Get("pwuserid"); + PWPassword = practiceConfig.Get("pwpassword"); + PWPracticeID = practiceConfig.Get("pwpracticeid"); + PWApiID = practiceConfig.Get("pwapiid"); + + PWBaseURI = universalConfig.Get("PWBaseURI"); + PWUpdateURI = universalConfig.Get("PWUpdateURI"); + PWAppVersion = universalConfig.Get("PWAppVersion"); + + } + public void LoadConfig(string path) { XmlDocument cfgDoc = new XmlDocument(); diff --git a/PWAPPv2/Source/API/APIConnection.cs b/PWAPPv2/Source/API/APIConnection.cs index 6847714..ba72ecc 100644 --- a/PWAPPv2/Source/API/APIConnection.cs +++ b/PWAPPv2/Source/API/APIConnection.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.Identity.Client.Platforms.Features.DesktopOs.Kerberos; +using System; +using System.Diagnostics; using System.IO; using System.Net; using System.Net.Http; @@ -13,34 +15,28 @@ namespace PWAPPv2.Source.API public DataObjects.APICredentials Credentials; - private static bool ResponseReady = false; - private static string Response = ""; + + HttpClient Client; public APIConnection(string baseUrl, DataObjects.APICredentials credentials) { Credentials = credentials; BaseURL = baseUrl; + + Client = new HttpClient(); + Client.BaseAddress = new Uri(BaseURL); + Client.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json")); + } - public string GetResponse() - { - while (!ResponseReady) ; - return Response; - } + /// + /// Dont use. Doesn't work, sillyhead. + /// + /// public static async void APIGet(string Call) { - //WebRequest request = WebRequest.Create(BaseURL + Call); - //request.Method = "GET"; - //request.ContentType = "application/json; charset=utf-8"; - //var response = (HttpWebResponse)request.GetResponse(); - //string text; - //using (var sr = new StreamReader(response.GetResponseStream())) - //{ - // text = sr.ReadToEnd(); - //} - //return text; - using (var client = new HttpClient()) { client.BaseAddress = new Uri(BaseURL); @@ -52,16 +48,16 @@ namespace PWAPPv2.Source.API } } + /// + /// Send a post requst without data. Essentially an advanced get since I was a retard when I wrote the original API. + /// + /// + /// public string SendPostRequestAsync(string apiUri) { - HttpClient client = new HttpClient(); - client.BaseAddress = new Uri(BaseURL); - client.DefaultRequestHeaders.Accept.Add( - new MediaTypeWithQualityHeaderValue("application/json")); - StringContent contetnt = new StringContent(Credentials.ToJsonString(), Encoding.UTF8, "application/json"); - var response = client.PostAsync(apiUri, contetnt).Result; + var response = Client.PostAsync(apiUri, contetnt).Result; if (response.IsSuccessStatusCode) { @@ -69,16 +65,18 @@ namespace PWAPPv2.Source.API } return ""; } + + /// + /// Send a post request with data + /// + /// + /// + /// public string SendPostRequestAsync(string apiUri, string PostData) { - HttpClient client = new HttpClient(); - client.BaseAddress = new Uri(BaseURL); - client.DefaultRequestHeaders.Accept.Add( - new MediaTypeWithQualityHeaderValue("application/json")); - StringContent contetnt = new StringContent(PostData, Encoding.UTF8, "application/json"); - var response = client.PostAsync(apiUri, contetnt).Result; + var response = Client.PostAsync(apiUri, contetnt).Result; if (response.IsSuccessStatusCode) { @@ -87,5 +85,46 @@ namespace PWAPPv2.Source.API return ""; } + /// + /// Send a post request with the API credengials in the header of the request. + /// + /// + /// + /// + /// + public string SendPostWithCredsInHeader(string apiUri, string PostData) + { + Client.DefaultRequestHeaders.Add("UserID", Credentials.UserID); + Client.DefaultRequestHeaders.Add("Password", Credentials.Password); + Client.DefaultRequestHeaders.Add("PracticeID", Credentials.PracticeId); + Client.DefaultRequestHeaders.Add("ApiID", Credentials.APIid); + + StringContent content = new StringContent(PostData, Encoding.UTF8, "application/json"); + + var response = Client.PostAsync(apiUri, content).Result; + + Client.DefaultRequestHeaders.Remove("UserID"); + Client.DefaultRequestHeaders.Remove("Password"); + Client.DefaultRequestHeaders.Remove("PraticeID"); + Client.DefaultRequestHeaders.Remove("ApiID"); + + if (response.IsSuccessStatusCode) + { + + return response.Content.ReadAsStringAsync().Result; + } + throw new RequestFailedExcpetion(response.StatusCode.ToString()); + + } + + } + + public class RequestFailedExcpetion : Exception + { + public string RequestError; + public RequestFailedExcpetion(string errorCode) + { + RequestError = "Server responded with error code: " + errorCode; + } } } diff --git a/PWAPPv2/Source/API/PWApiConnection.cs b/PWAPPv2/Source/API/PWApiConnection.cs new file mode 100644 index 0000000..c188642 --- /dev/null +++ b/PWAPPv2/Source/API/PWApiConnection.cs @@ -0,0 +1,50 @@ +using PWAPPv2.Source.DataObjects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PWAPPv2.Source.API +{ + public class PWApiConnection + { + private APIConnection BaseConnection; + private APIConnection UpdateConnection; + + private APICredentials Credentials; + private string AppVersion; + + public PWApiConnection(Source.Config.Configuration practiceConfig, Source.Config.Configuration universalConfig) + { + Credentials = new APICredentials(practiceConfig); + BaseConnection = new APIConnection(universalConfig.Get("PWBaseURI"), Credentials); + UpdateConnection = new APIConnection(universalConfig.Get("PWUpdateURI"), Credentials); + + AppVersion = universalConfig.Get("PWAppVersion"); + + } + + public string PostToApi(string uri, string data) + { + return BaseConnection.SendPostRequestAsync(uri, data); + } + + public string GetFromApi(string uri) + { + return BaseConnection.SendPostRequestAsync(uri); + } + + public bool CheckForUpdate() + { + string currentVersion = UpdateConnection.SendPostWithCredsInHeader("", ""); + currentVersion = currentVersion.Replace("\"", ""); + if(currentVersion != AppVersion) + { + return true; + } + return false; + } + + } +} diff --git a/PWAPPv2/Source/Config/Configuration.cs b/PWAPPv2/Source/Config/Configuration.cs new file mode 100644 index 0000000..a81d2ee --- /dev/null +++ b/PWAPPv2/Source/Config/Configuration.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.IO; + +namespace PWAPPv2.Source.Config +{ + public class Configuration + { + private Dictionary values; + + public Configuration(string file) + { + values = new Dictionary(); + if(!File.Exists(file)) + { + throw new ConfigFileNotFoundException(); + } + + string f = File.ReadAllText(file); + + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(f); + + XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes; + foreach (XmlNode xmlNode in xmlNodeList) + { + values.Add(xmlNode.Name, xmlNode.InnerText); + } + + } + + public string Get(string key) + { + try + { + return values[key]; + } + catch + { + throw new ConfigValueNotFoundException(); + } + } + + + } + + public class ConfigFileNotFoundException : Exception { } + + public class ConfigValueNotFoundException: Exception { } +} diff --git a/PWAPPv2/Source/DataObjects/APICredentials.cs b/PWAPPv2/Source/DataObjects/APICredentials.cs index 77e513c..c870733 100644 --- a/PWAPPv2/Source/DataObjects/APICredentials.cs +++ b/PWAPPv2/Source/DataObjects/APICredentials.cs @@ -20,6 +20,14 @@ APIid = config.PWApiID; } + public APICredentials(Source.Config.Configuration practiceConfig) + { + UserID = practiceConfig.Get("pwuserid"); + Password = practiceConfig.Get("pwpassword"); + PracticeId = practiceConfig.Get("pwpracticeid"); + APIid = practiceConfig.Get("pwapiid"); + } + public string BuildJsonBodyContents() { return "'UserID':'" + UserID + "'," + diff --git a/PWAPPv2/Source/Database/DatabaseConnection.cs b/PWAPPv2/Source/Database/DatabaseConnection.cs index c644ca7..33efa00 100644 --- a/PWAPPv2/Source/Database/DatabaseConnection.cs +++ b/PWAPPv2/Source/Database/DatabaseConnection.cs @@ -4,6 +4,9 @@ using System.Collections.Generic; namespace PWAPPv2.Source.Database { + /// + /// Class DatabaseConnection creates and maintains a connection to and OpenDental MySqlDatabase. + /// class DatabaseConnection { @@ -18,6 +21,12 @@ namespace PWAPPv2.Source.Database Connection = new MySqlConnection(SqlString); } + public DatabaseConnection(Config.Configuration config) + { + SqlString = "server=" + config.Get("ODhost") + ";Uid=" + config.Get("ODuser") + ";database=" + config.Get("ODdatabase") + ";Pwd=" + config.Get("ODpassword"); + Connection = new MySqlConnection(SqlString); + } + public void Connect() { try diff --git a/UnitTests/ConfigTest.cs b/UnitTests/ConfigTest.cs new file mode 100644 index 0000000..0510881 --- /dev/null +++ b/UnitTests/ConfigTest.cs @@ -0,0 +1,21 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using PWAPPv2.Source; + +namespace UnitTests +{ + [TestClass] + public class ConfigTest + { + [TestMethod] + public void TestGetPracticeConfigs() + { + PWAPPv2.Source.Config.Configuration configuration = new PWAPPv2.Source.Config.Configuration(".\\Config\\PracticeConfig.xml"); + Assert.IsNotNull(configuration); + + string apiKey = configuration.Get("pwapiid"); + Assert.IsNotNull(apiKey); + + } + } +} diff --git a/UnitTests/PWConnectionTest.cs b/UnitTests/PWConnectionTest.cs new file mode 100644 index 0000000..1194a09 --- /dev/null +++ b/UnitTests/PWConnectionTest.cs @@ -0,0 +1,22 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace UnitTests +{ + [TestClass] + public class PWConnectionTest + { + [TestMethod] + public void TestCheckUpdate() + { + PWAPPv2.Source.Config.Configuration practiceConfig = new PWAPPv2.Source.Config.Configuration(".\\Config\\PracticeConfig.xml"); + PWAPPv2.Source.Config.Configuration universalConfig = new PWAPPv2.Source.Config.Configuration(".\\Config\\UniversalConfig.xml"); + + PWAPPv2.Source.API.PWApiConnection pwApiConnection = new PWAPPv2.Source.API.PWApiConnection(practiceConfig, universalConfig); + Assert.IsNotNull(pwApiConnection); + + + Assert.IsFalse(pwApiConnection.CheckForUpdate()); + } + } +} diff --git a/UnitTests/UnitTest1.cs b/UnitTests/UnitTest1.cs index 3de928a..f6a0f58 100644 --- a/UnitTests/UnitTest1.cs +++ b/UnitTests/UnitTest1.cs @@ -25,5 +25,35 @@ namespace UnitTests Assert.AreEqual("\"1|Endo|2|Implant|3|Oral Surgery|4|Ortho|5|Pedo|6|Perio|7|Restore|8|Other \\n\"", res); } + + [TestMethod] + public void TestV2Get() + { + APICredentials creds = new APICredentials(); + creds.UserID = "testdoctor"; + creds.Password = "testdoctor"; + creds.PracticeId = "210"; + creds.APIid = "12345678"; + + APIConnection conn = new APIConnection("https://localhost:44354/", creds); + string res = conn.SendPostWithCredsInHeader("api/PWAppVersion", ""); + + } + + [TestMethod] + [ExpectedException(typeof(RequestFailedExcpetion))] + public void TestInvalidCredentialsV2() + { + APICredentials creds = new APICredentials(); + creds.UserID = "testdoctor"; + creds.Password = "badPassword"; + creds.PracticeId = "210"; + creds.APIid = "12345678"; + + APIConnection conn = new APIConnection("https://localhost:44354/", creds); + string res = conn.SendPostWithCredsInHeader("api/PWAppVersion", ""); + + } + } } diff --git a/UnitTests/UnitTests.csproj b/UnitTests/UnitTests.csproj index 720bce6..6f705ec 100644 --- a/UnitTests/UnitTests.csproj +++ b/UnitTests/UnitTests.csproj @@ -51,6 +51,8 @@ + + From 8cff012772d885ed6339164ede833a2d229997d4 Mon Sep 17 00:00:00 2001 From: goomatt33 Date: Mon, 20 Nov 2023 16:20:20 -0500 Subject: [PATCH 4/7] Auto updater added --- PWAPPv2.sln | 20 +++-- PWAPPv2/MainWindow.xaml.cs | 58 ++++++++----- PWAPPv2/Source/API/APIConnection.cs | 1 + PWAPPv2/Source/API/PWApiConnection.cs | 31 +++++++ PWAppUpdater/App.xaml | 9 ++ PWAppUpdater/App.xaml.cs | 17 ++++ PWAppUpdater/AssemblyInfo.cs | 10 +++ PWAppUpdater/FTP/FTPManager.cs | 45 ++++++++++ PWAppUpdater/MainWindow.xaml | 16 ++++ PWAppUpdater/MainWindow.xaml.cs | 46 ++++++++++ PWAppUpdater/PWAppUpdater.csproj | 14 +++ PWAppUpdater/Updater.cs | 52 +++++++++++ PWAppUpdater/Zip/ZipManager.cs | 28 ++++++ PWAppUpdater/app.manifest | 9 ++ PWAppUpdater2/FTP/FTPManager.cs | 45 ++++++++++ PWAppUpdater2/Form1.Designer.cs | 98 +++++++++++++++++++++ PWAppUpdater2/Form1.cs | 51 +++++++++++ PWAppUpdater2/Form1.resx | 120 ++++++++++++++++++++++++++ PWAppUpdater2/PWAppUpdaterForm.csproj | 11 +++ PWAppUpdater2/Program.cs | 17 ++++ PWAppUpdater2/Updater.cs | 77 +++++++++++++++++ PWAppUpdater2/Zip/ZipManager.cs | 28 ++++++ UnitTests/UnitTests.csproj | 6 -- 23 files changed, 776 insertions(+), 33 deletions(-) create mode 100644 PWAppUpdater/App.xaml create mode 100644 PWAppUpdater/App.xaml.cs create mode 100644 PWAppUpdater/AssemblyInfo.cs create mode 100644 PWAppUpdater/FTP/FTPManager.cs create mode 100644 PWAppUpdater/MainWindow.xaml create mode 100644 PWAppUpdater/MainWindow.xaml.cs create mode 100644 PWAppUpdater/PWAppUpdater.csproj create mode 100644 PWAppUpdater/Updater.cs create mode 100644 PWAppUpdater/Zip/ZipManager.cs create mode 100644 PWAppUpdater/app.manifest create mode 100644 PWAppUpdater2/FTP/FTPManager.cs create mode 100644 PWAppUpdater2/Form1.Designer.cs create mode 100644 PWAppUpdater2/Form1.cs create mode 100644 PWAppUpdater2/Form1.resx create mode 100644 PWAppUpdater2/PWAppUpdaterForm.csproj create mode 100644 PWAppUpdater2/Program.cs create mode 100644 PWAppUpdater2/Updater.cs create mode 100644 PWAppUpdater2/Zip/ZipManager.cs diff --git a/PWAPPv2.sln b/PWAPPv2.sln index 5b08ebc..27b0408 100644 --- a/PWAPPv2.sln +++ b/PWAPPv2.sln @@ -1,11 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.33529.622 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34009.444 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PWAPPv2", "PWAPPv2\PWAPPv2.csproj", "{45E26AF8-41D7-4308-A2C8-D55A0350DB47}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{164B9220-F337-4E88-B619-0C52C502B9C4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PWAppUpdater", "PWAppUpdater\PWAppUpdater.csproj", "{B84B314C-C17A-42A3-BCAE-038F48343F5A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PWAppUpdaterForm", "PWAppUpdater2\PWAppUpdaterForm.csproj", "{093798DD-2EDD-4519-AAC6-9DFF0DE3C15A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,10 +19,14 @@ Global {45E26AF8-41D7-4308-A2C8-D55A0350DB47}.Debug|Any CPU.Build.0 = Debug|Any CPU {45E26AF8-41D7-4308-A2C8-D55A0350DB47}.Release|Any CPU.ActiveCfg = Release|Any CPU {45E26AF8-41D7-4308-A2C8-D55A0350DB47}.Release|Any CPU.Build.0 = Release|Any CPU - {164B9220-F337-4E88-B619-0C52C502B9C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {164B9220-F337-4E88-B619-0C52C502B9C4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {164B9220-F337-4E88-B619-0C52C502B9C4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {164B9220-F337-4E88-B619-0C52C502B9C4}.Release|Any CPU.Build.0 = Release|Any CPU + {B84B314C-C17A-42A3-BCAE-038F48343F5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B84B314C-C17A-42A3-BCAE-038F48343F5A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B84B314C-C17A-42A3-BCAE-038F48343F5A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B84B314C-C17A-42A3-BCAE-038F48343F5A}.Release|Any CPU.Build.0 = Release|Any CPU + {093798DD-2EDD-4519-AAC6-9DFF0DE3C15A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {093798DD-2EDD-4519-AAC6-9DFF0DE3C15A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {093798DD-2EDD-4519-AAC6-9DFF0DE3C15A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {093798DD-2EDD-4519-AAC6-9DFF0DE3C15A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PWAPPv2/MainWindow.xaml.cs b/PWAPPv2/MainWindow.xaml.cs index efbe229..05b2727 100644 --- a/PWAPPv2/MainWindow.xaml.cs +++ b/PWAPPv2/MainWindow.xaml.cs @@ -1,10 +1,10 @@ -using Microsoft.Win32; -using System; +using System; using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; -using System.Windows.Media; /** * TODO: @@ -40,7 +40,10 @@ namespace PWAPPv2 List images; - string ConfigPath = "C:\\PWAPP\\Config\\Config.xml"; + + //string ConfigPath = "C:\\PWAPP\\Config\\Config.xml"; + + string ConfigPath; public MainWindow() { @@ -53,7 +56,10 @@ namespace PWAPPv2 try { - practiceConfig = new Source.Config.Configuration(".\\Config\\PracticeConfig.xml"); + ConfigPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + ConfigPath = Path.Combine(ConfigPath, "PWAPP\\Config\\"); + + practiceConfig = new Source.Config.Configuration(ConfigPath + "PracticeConfig.xml"); universalConfig = new Source.Config.Configuration(".\\Config\\UniversalConfig.xml"); } catch @@ -66,15 +72,28 @@ namespace PWAPPv2 try { - if(pwapiConnection.CheckForUpdate() == true) + if (pwapiConnection.CheckForUpdate() == true) { string message = "An update is available! Would you like to install it?"; string title = "Update available!"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result = System.Windows.Forms.MessageBox.Show(message, title, buttons); - if(result == System.Windows.Forms.DialogResult.Yes) + if (result == System.Windows.Forms.DialogResult.Yes) { - System.Windows.MessageBox.Show("HAHA NO UPDATE FOR YOU!"); + //System.Windows.MessageBox.Show("HAHA NO UPDATE FOR YOU!"); + Process p = new Process(); + p.StartInfo.FileName = "E:\\PatientWebAPI\\PWAPPv2\\PWAPPv2\\PWAppUpdater2\\bin\\Release\\net6.0-windows\\PWAppUpdaterForm.exe"; + p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; + p.StartInfo.UseShellExecute = false; + p.StartInfo.RedirectStandardOutput = true; + p.StartInfo.RedirectStandardError = true; + p.StartInfo.Arguments = args[0]; + if(System.Environment.OSVersion.Version.Major >= 6) + { + p.StartInfo.Verb = "runas"; + } + p.Start(); + Environment.Exit(0); } } } @@ -104,11 +123,11 @@ namespace PWAPPv2 this.DataContext = new Source.PatientGUIAdapter(patient); } - catch(NullReferenceException) + catch (NullReferenceException) { } - catch(Exception e) + catch (Exception e) { System.Windows.MessageBox.Show(e.Message); } @@ -123,20 +142,21 @@ namespace PWAPPv2 private void PopulateReferTypesBox() { - string ReferTypeString = apiConnection.SendPostRequestAsync("api/PWReferralTypes"); + //string ReferTypeString = apiConnection.SendPostRequestAsync("api/PWReferralTypes"); + string ReferTypeString = pwapiConnection.GetReferalTypes(); TypeBox = new Source.DataObjects.ReferralTypeBox(boxReferType, ReferTypeString); } private void PopulateReferToBox() { - string ReferToString = apiConnection.SendPostRequestAsync("api/PWReferTo"); + string ReferToString = pwapiConnection.GetReferTo(); //apiConnection.SendPostRequestAsync("api/PWReferTo"); ToBox = new Source.DataObjects.ReferToBox(boxReferTo, ReferToString); } private void PopulateReferFromBox() { - string ReferFromString = apiConnection.SendPostRequestAsync("api/PWReferFrom"); + string ReferFromString = pwapiConnection.GetReferFrom(); //apiConnection.SendPostRequestAsync("api/PWReferFrom"); FromBox = new Source.DataObjects.ReferFromBox(boxReferFrom, ReferFromString); } @@ -205,7 +225,7 @@ namespace PWAPPv2 try { string referralString = referral.ToJsonString(); - string result = apiConnection.SendPostRequestAsync("api/PWMakeReferral", referralString); + string result = pwapiConnection.SendReferral(referralString); //apiConnection.SendPostRequestAsync("api/PWMakeReferral", referralString); if (images.Count > 0) { foreach (Source.DataObjects.PWImage im in images) @@ -214,12 +234,10 @@ namespace PWAPPv2 string json = att.ToJsonString(); apiConnection.SendPostRequestAsync("api/PWAttachment", json); } - System.Windows.MessageBox.Show("Referral added successfully!"); - } - else - { - System.Windows.MessageBox.Show(result); } + + System.Windows.MessageBox.Show("Referral added successfully!"); + this.Close(); } catch (Source.DataObjects.Referral.InvalidReferalDataException) @@ -236,7 +254,7 @@ namespace PWAPPv2 openFileDialog.Filter = "Image files (*.jpg,*.jpeg)|*.jpg;*.jpeg"; openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); - if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) + if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { foreach (string filename in openFileDialog.FileNames) { diff --git a/PWAPPv2/Source/API/APIConnection.cs b/PWAPPv2/Source/API/APIConnection.cs index ba72ecc..4768258 100644 --- a/PWAPPv2/Source/API/APIConnection.cs +++ b/PWAPPv2/Source/API/APIConnection.cs @@ -100,6 +100,7 @@ namespace PWAPPv2.Source.API Client.DefaultRequestHeaders.Add("ApiID", Credentials.APIid); StringContent content = new StringContent(PostData, Encoding.UTF8, "application/json"); + string conts = content.ToString(); var response = Client.PostAsync(apiUri, content).Result; diff --git a/PWAPPv2/Source/API/PWApiConnection.cs b/PWAPPv2/Source/API/PWApiConnection.cs index c188642..18bd815 100644 --- a/PWAPPv2/Source/API/PWApiConnection.cs +++ b/PWAPPv2/Source/API/PWApiConnection.cs @@ -12,6 +12,12 @@ namespace PWAPPv2.Source.API private APIConnection BaseConnection; private APIConnection UpdateConnection; + private APIConnection AttachementConnection; + private APIConnection ReferralConnection; + private APIConnection ReferToConnection; + private APIConnection ReferFromConnection; + private APIConnection ReferTypesConnection; + private APICredentials Credentials; private string AppVersion; @@ -20,6 +26,11 @@ namespace PWAPPv2.Source.API Credentials = new APICredentials(practiceConfig); BaseConnection = new APIConnection(universalConfig.Get("PWBaseURI"), Credentials); UpdateConnection = new APIConnection(universalConfig.Get("PWUpdateURI"), Credentials); + AttachementConnection = new APIConnection(universalConfig.Get("PWAttachmentURI"), Credentials); + ReferralConnection = new APIConnection(universalConfig.Get("PWReferralURI"), Credentials); + ReferToConnection = new APIConnection(universalConfig.Get("PWReferToURI"), Credentials); + ReferFromConnection = new APIConnection(universalConfig.Get("PWReferFromURI"), Credentials); + ReferTypesConnection = new APIConnection(universalConfig.Get("PWReferTypesURI"), Credentials); AppVersion = universalConfig.Get("PWAppVersion"); @@ -46,5 +57,25 @@ namespace PWAPPv2.Source.API return false; } + public string GetReferalTypes() + { + return ReferTypesConnection.SendPostWithCredsInHeader("", ""); + } + + public string GetReferTo() + { + return ReferToConnection.SendPostWithCredsInHeader("", ""); + } + + public string GetReferFrom() + { + return ReferFromConnection.SendPostWithCredsInHeader("", ""); + } + + public string SendReferral(string JsonData) + { + return ReferralConnection.SendPostWithCredsInHeader("", JsonData); + } + } } diff --git a/PWAppUpdater/App.xaml b/PWAppUpdater/App.xaml new file mode 100644 index 0000000..c91f125 --- /dev/null +++ b/PWAppUpdater/App.xaml @@ -0,0 +1,9 @@ + + + + + diff --git a/PWAppUpdater/App.xaml.cs b/PWAppUpdater/App.xaml.cs new file mode 100644 index 0000000..0ba6512 --- /dev/null +++ b/PWAppUpdater/App.xaml.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace PWAppUpdater +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } +} diff --git a/PWAppUpdater/AssemblyInfo.cs b/PWAppUpdater/AssemblyInfo.cs new file mode 100644 index 0000000..8b5504e --- /dev/null +++ b/PWAppUpdater/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly: ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/PWAppUpdater/FTP/FTPManager.cs b/PWAppUpdater/FTP/FTPManager.cs new file mode 100644 index 0000000..e478cbd --- /dev/null +++ b/PWAppUpdater/FTP/FTPManager.cs @@ -0,0 +1,45 @@ +using System; +using System.IO; +using System.Net; + + +namespace PWAppUpdater.FTP +{ + class FTPManager + { + private string BaseUri; + private NetworkCredential credential; + + public FTPManager(string baseUri, string username, string password) + { + BaseUri = baseUri; + credential = new NetworkCredential(username, password); + } + + public void DownloadFile(string FTPPath, string localPath) + { + try + { + FtpWebRequest request = (FtpWebRequest)WebRequest.Create(BaseUri + FTPPath); + request.Method = WebRequestMethods.Ftp.DownloadFile; + request.Credentials = credential; + request.KeepAlive = false; + request.UseBinary = true; + request.UsePassive = true; + + FtpWebResponse response = (FtpWebResponse)request.GetResponse(); + Stream responseStream = response.GetResponseStream(); + + using (Stream s = File.Create(localPath)) + { + responseStream.CopyTo(s); + } + response.Close(); + } + catch (Exception e) + { + Console.WriteLine(e.Message.ToString()); + } + } + } +} diff --git a/PWAppUpdater/MainWindow.xaml b/PWAppUpdater/MainWindow.xaml new file mode 100644 index 0000000..bb52683 --- /dev/null +++ b/PWAppUpdater/MainWindow.xaml @@ -0,0 +1,16 @@ + + + +