From 29ec4ef677826920fd36e20899d9bcbd18bddca6 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Tue, 30 May 2017 10:57:10 +0200 Subject: Added board member feature: managing defects. --- .../ApartmentManager/ApartmentManager.csproj | 24 ++- .../ApartmentManager/Handler/ApartmentHandler.cs | 10 +- .../ApartmentManager/Handler/BmHandler.cs | 123 ++++++++++++ .../ApartmentManager/Handler/LoginHandler.cs | 1 + ApartmentManager/ApartmentManager/Model/Defect.cs | 40 +++- .../ApartmentManager/Model/DefectComment.cs | 13 ++ .../ApartmentManager/Model/DefectComments.cs | 18 -- .../ApartmentManager/Persistency/ApiClient.cs | 2 - .../Persistency/PersistenceFacade.cs | 118 ------------ .../ApartmentManager/Singletons/BmSingleton.cs | 1 + .../Singletons/CatalogSingleton.cs | 4 +- .../ApartmentManager/View/ApartmentDefectPage.xaml | 106 ++++++++--- .../View/ApartmentDefectViewPage.xaml | 111 +++++++---- .../ApartmentManager/View/ApartmentNewDefect.xaml | 61 ++++-- .../ApartmentManager/View/ApartmentPage.xaml | 129 +++++++++---- .../View/ApartmentResidentsPage.xaml | 211 +++++++++++++++------ .../ApartmentManager/View/BmApartmentsPage.xaml | 14 +- .../ApartmentManager/View/BmApartmentsPage.xaml.cs | 2 +- .../View/BmCreateApartmentPage.xaml | 14 +- .../ApartmentManager/View/BmCreateDefectPage.xaml | 97 ++++++++++ .../View/BmCreateDefectPage.xaml.cs | 15 ++ .../ApartmentManager/View/BmDefectsPage.xaml | 132 ++++++++++++- .../ApartmentManager/View/BmDefectsPage.xaml.cs | 13 ++ .../ApartmentManager/View/BmEditApartmentPage.xaml | 4 +- .../ApartmentManager/View/BmEditDefectPage.xaml | 108 +++++++++++ .../ApartmentManager/View/BmEditDefectPage.xaml.cs | 30 +++ .../ApartmentManager/View/BmSingleDefectPage.xaml | 135 +++++++++++++ .../View/BmSingleDefectPage.xaml.cs | 30 +++ .../ApartmentManager/View/BmUsersPage.xaml | 10 +- .../ViewModel/ApartmentViewModel.cs | 6 +- .../ApartmentManager/ViewModel/BmViewModel.cs | 77 +++++++- 31 files changed, 1320 insertions(+), 339 deletions(-) create mode 100644 ApartmentManager/ApartmentManager/Model/DefectComment.cs delete mode 100644 ApartmentManager/ApartmentManager/Model/DefectComments.cs delete mode 100644 ApartmentManager/ApartmentManager/Persistency/PersistenceFacade.cs create mode 100644 ApartmentManager/ApartmentManager/View/BmCreateDefectPage.xaml create mode 100644 ApartmentManager/ApartmentManager/View/BmCreateDefectPage.xaml.cs create mode 100644 ApartmentManager/ApartmentManager/View/BmEditDefectPage.xaml create mode 100644 ApartmentManager/ApartmentManager/View/BmEditDefectPage.xaml.cs create mode 100644 ApartmentManager/ApartmentManager/View/BmSingleDefectPage.xaml create mode 100644 ApartmentManager/ApartmentManager/View/BmSingleDefectPage.xaml.cs diff --git a/ApartmentManager/ApartmentManager/ApartmentManager.csproj b/ApartmentManager/ApartmentManager/ApartmentManager.csproj index 2378b79..560357a 100644 --- a/ApartmentManager/ApartmentManager/ApartmentManager.csproj +++ b/ApartmentManager/ApartmentManager/ApartmentManager.csproj @@ -104,7 +104,7 @@ - + @@ -115,7 +115,6 @@ - @@ -136,9 +135,15 @@ BmChangesPage.xaml + + BmCreateDefectPage.xaml + BmEditApartmentPage.xaml + + BmEditDefectPage.xaml + BmResidentsPage.xaml @@ -154,6 +159,9 @@ BmCreateApartmentPage.xaml + + BmSingleDefectPage.xaml + BmUsersPage.xaml @@ -219,10 +227,18 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -243,6 +259,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs index 52849aa..ac5999e 100644 --- a/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/ApartmentHandler.cs @@ -178,7 +178,7 @@ namespace ApartmentManager.Handler foreach (var defect in defecttlist) { - var picturesFromDatabase = ApiClient.GetData("api/DefectPictures/" + defect.DefectId); + var picturesFromDatabase = ApiClient.GetData("api/DefectPicturesById/" + defect.DefectId); if (picturesFromDatabase != "[]") { ApartmentViewModel.CatalogSingleton.DefectPictures = JsonConvert.DeserializeObject>(picturesFromDatabase); @@ -254,10 +254,10 @@ namespace ApartmentManager.Handler { var defectFromDatabase = ApiClient.GetData("api/defects/" + ApartmentViewModel.NewDefect.DefectId); ApartmentViewModel.CatalogSingleton.Defect = JsonConvert.DeserializeObject(defectFromDatabase); - var picturesFromDatabase = ApiClient.GetData("api/DefectPictures/" + ApartmentViewModel.NewDefect.DefectId); + var picturesFromDatabase = ApiClient.GetData("api/DefectPicturesById/" + ApartmentViewModel.NewDefect.DefectId); ApartmentViewModel.CatalogSingleton.DefectPictures = JsonConvert.DeserializeObject>(picturesFromDatabase); var defectComments = ApiClient.GetData("api/Defectcomments/" + ApartmentViewModel.NewDefect.DefectId); - ApartmentViewModel.CatalogSingleton.DefectComments = JsonConvert.DeserializeObject>(defectComments); + ApartmentViewModel.CatalogSingleton.DefectComments = JsonConvert.DeserializeObject>(defectComments); CatalogSingleton.Instance.DefectId = ApartmentViewModel.NewDefect.DefectId; } catch (Exception e) @@ -271,7 +271,7 @@ namespace ApartmentManager.Handler { try { - DefectComments Comment = new DefectComments(); + DefectComment Comment = new DefectComment(); Comment.Comment = ApartmentViewModel.NewDefectComment.Comment; Comment.DefectId = CatalogSingleton.Instance.Defect.DefectId; Comment.Name = UserSingleton.Instance.CurrentUser.FirstName + " " + UserSingleton.Instance.CurrentUser.LastName; @@ -281,7 +281,7 @@ namespace ApartmentManager.Handler ApiClient.PostData("api/Defectcomments/", Comment); } var response = ApiClient.GetData("api/Defectcomments/" + CatalogSingleton.Instance.DefectId); - var commentlist = JsonConvert.DeserializeObject>(response); + var commentlist = JsonConvert.DeserializeObject>(response); CatalogSingleton.Instance.DefectComments.Clear(); foreach (var comment in commentlist) { diff --git a/ApartmentManager/ApartmentManager/Handler/BmHandler.cs b/ApartmentManager/ApartmentManager/Handler/BmHandler.cs index 2cbffea..8709180 100644 --- a/ApartmentManager/ApartmentManager/Handler/BmHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/BmHandler.cs @@ -6,6 +6,7 @@ using ApartmentManager.Persistency; using ApartmentManager.ViewModel; using Newtonsoft.Json; using ApartmentManager.Singletons; +using System.Collections.Generic; namespace ApartmentManager.Handler { @@ -204,5 +205,127 @@ namespace ApartmentManager.Handler } #endregion + #region DEFECTS + + public void GetDefects() + { + var defects = JsonConvert.DeserializeObject>(ApiClient.GetData("api/Defects/")); + BmSingleton.Instance.Defects.Clear(); + foreach (var defect in defects) + { + defect.Pictures = JsonConvert.DeserializeObject>(ApiClient.GetData("api/DefectPicturesById/" + defect.DefectId)); + defect.Comments = JsonConvert.DeserializeObject>(ApiClient.GetData("api/DefectComments/" + defect.DefectId)); + BmSingleton.Instance.Defects.Add(defect); + } + } + + public void CreateDefect() + { + try + { + _vm.DefectTemplate.Status = "New"; + _vm.DefectTemplate.UploadDate = DateTime.Now; + var response = JsonConvert.DeserializeObject(ApiClient.PostData("api/Defects/", _vm.DefectTemplate)); + foreach(var defectPicture in _vm.DefectTemplate.Pictures) + { + defectPicture.DefectId = response.DefectId; + ApiClient.PostData("api/DefectPictures", defectPicture); + } + GetDefects(); + _vm.DefectTemplate = new Defect(); + } + catch (Exception e) + { + var msg = new MessageDialog(e.Message).ShowAsync(); + } + } + public void UpdateDefect() + { + try + { + ApiClient.PutData("api/Defects/" + _vm.DefectTemplate.DefectId, _vm.DefectTemplate); + var deletedDefectPictures = new List(_vm.DeletedDefectPictures); + var addedDefectPictures = new List(_vm.AddedDefectPictures); + + foreach (var defectPicture in deletedDefectPictures) + { + ApiClient.DeleteData("api/DefectPictures/" + defectPicture.Pictureid); + _vm.DeletedDefectPictures.Remove(defectPicture); + } + foreach (var defectPicture in addedDefectPictures) + { + defectPicture.DefectId = _vm.DefectTemplate.DefectId; + ApiClient.PostData("api/DefectPictures", defectPicture); + addedDefectPictures.Remove(defectPicture); + } + GetDefects(); + } + catch (Exception e) + { + var msg = new MessageDialog(e.Message).ShowAsync(); + } + } + public void DeleteDefect() + { + try + { + ApiClient.DeleteData("api/Defects/" + _vm.DefectTemplate.DefectId); + BmSingleton.Instance.Defects.Remove(_vm.DefectTemplate); + GetDefects(); + } + catch (Exception e) + { + var msg = new MessageDialog(e.Message).ShowAsync(); + } + } + + public void ClearDefectTemplate() + { + _vm.DefectTemplate = new Defect(); + } + + public async void UploadDefectPicture() + { + if (_vm.DefectTemplate.Pictures == null) _vm.DefectTemplate.Pictures = new ObservableCollection(); + var picture = new DefectPicture() { Picture = await ImgurPhotoUploader.UploadPhotoAsync() }; + _vm.DefectTemplate.Pictures.Add(picture); + _vm.AddedDefectPictures.Add(picture); + } + + public void DeleteDefectPicture() + { + _vm.DefectTemplate.Pictures.Remove(_vm.SelectedDefectPicture); + } + + public void DeleteDefectPictureTemp() + { + _vm.DeletedDefectPictures.Add(_vm.SelectedDefectPicture); + _vm.DefectTemplate.Pictures.Remove(_vm.SelectedDefectPicture); + } + + public void CreateDefectComment() + { + try + { + var comment = new DefectComment() + { + Comment = _vm.NewDefectComment.Comment, + DefectId = _vm.DefectTemplate.DefectId, + Name = UserSingleton.Instance.CurrentUser.FirstName + " " + UserSingleton.Instance.CurrentUser.LastName, + Date = DateTimeOffset.Now + }; + if (!string.IsNullOrEmpty(comment.Comment)) + { + ApiClient.PostData("api/DefectComments/", comment); + _vm.DefectTemplate.Comments.Add(comment); + _vm.NewDefectComment = new DefectComment(); + } + } + catch (Exception e) + { + new MessageDialog(e.Message).ShowAsync(); + } + } + #endregion } } diff --git a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs index e4d9721..4f93cc1 100644 --- a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs @@ -79,6 +79,7 @@ namespace ApartmentManager.Handler bvm.BmHandler.GetApartments(); bvm.BmHandler.GetUsers(); bvm.BmHandler.GetResidents(); + bvm.BmHandler.GetDefects(); appShell.AppFrame.Navigate(typeof(BmMainPage)); } else diff --git a/ApartmentManager/ApartmentManager/Model/Defect.cs b/ApartmentManager/ApartmentManager/Model/Defect.cs index ea305e6..6e78e66 100644 --- a/ApartmentManager/ApartmentManager/Model/Defect.cs +++ b/ApartmentManager/ApartmentManager/Model/Defect.cs @@ -1,8 +1,12 @@ -using System; +using ApartmentManager.Annotations; +using System; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Runtime.CompilerServices; namespace ApartmentManager.Model { - public class Defect + public class Defect : INotifyPropertyChanged { public int DefectId { get; set; } public int ApartmentId { get; set; } @@ -11,6 +15,8 @@ namespace ApartmentManager.Model public string Description { get; set; } public string Status { get; set; } public string MainPicture { get; set; } + private ObservableCollection _pictures; + private ObservableCollection _comments; public Defect() { } @@ -23,9 +29,39 @@ namespace ApartmentManager.Model Description = description; Status = status; } + public override string ToString() { return string.Format($"Defect ID: {DefectId}, Apartment number: {ApartmentId}, Name: {Name}, Upload date: {UploadDate}, Description: {Description}, Status: {Status}"); } + + public ObservableCollection Pictures + { + get => _pictures; + set + { + _pictures = value; + OnPropertyChanged(nameof(Pictures)); + } + } + + public ObservableCollection Comments + { + get => _comments; + set + { + _comments = value; + OnPropertyChanged(nameof(Comments)); + } + } + + public event PropertyChangedEventHandler PropertyChanged; + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + + } } } diff --git a/ApartmentManager/ApartmentManager/Model/DefectComment.cs b/ApartmentManager/ApartmentManager/Model/DefectComment.cs new file mode 100644 index 0000000..7610e0b --- /dev/null +++ b/ApartmentManager/ApartmentManager/Model/DefectComment.cs @@ -0,0 +1,13 @@ +using System; + +namespace ApartmentManager.Model +{ + public class DefectComment + { + public int CommentId { get; set; } + public int DefectId { get; set; } + public string Comment { get; set; } + public string Name { get; set; } + public DateTimeOffset Date { get; set; } + } +} diff --git a/ApartmentManager/ApartmentManager/Model/DefectComments.cs b/ApartmentManager/ApartmentManager/Model/DefectComments.cs deleted file mode 100644 index a1e6d1e..0000000 --- a/ApartmentManager/ApartmentManager/Model/DefectComments.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ApartmentManager.Model -{ - public class DefectComments - { - - public int CommentId { get; set; } - public int DefectId { get; set; } - public string Comment { get; set; } - public string Name { get; set; } - public DateTimeOffset Date { get; set; } - } -} diff --git a/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs b/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs index b99f582..8f59a38 100644 --- a/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs +++ b/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs @@ -3,8 +3,6 @@ using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; -using ApartmentManager.Model; -using ApartmentManager.ViewModel; namespace ApartmentManager.Persistency { diff --git a/ApartmentManager/ApartmentManager/Persistency/PersistenceFacade.cs b/ApartmentManager/ApartmentManager/Persistency/PersistenceFacade.cs deleted file mode 100644 index 3a6f85e..0000000 --- a/ApartmentManager/ApartmentManager/Persistency/PersistenceFacade.cs +++ /dev/null @@ -1,118 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; -using System.Threading.Tasks; -using Windows.UI.Popups; -using ApartmentManager.Model; -using Newtonsoft.Json; - -namespace ApartmentManager.Persistency -{ - class PersistenceFacade - { - - const string ServerUrl = "http://localhost:60916"; - HttpClientHandler handler; - - public PersistenceFacade() - { - handler = new HttpClientHandler(); - handler.UseDefaultCredentials = true; - } - - // Get apartments - - public void CreateApartment(Apartment apartment) - { - using (var client = new HttpClient(handler)) - { - client.BaseAddress = new Uri(ServerUrl); - client.DefaultRequestHeaders.Clear(); - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - try - { - string postBody = JsonConvert.SerializeObject(apartment); - var response = client.PostAsync("api/apartments/", - new StringContent(postBody, Encoding.UTF8, "application/json")) - .Result; - if (response.IsSuccessStatusCode) - { - new MessageDialog("Success").ShowAsync(); - } - else - { - new MessageDialog("Error").ShowAsync(); - } - - } - catch (Exception ex) - { - new MessageDialog(ex.Message).ShowAsync(); - } - } - } - - ///Get Get Residents/// - public List GetApartmentResidents(Resident resident) - { - using (var client = new HttpClient(handler)) - { - client.BaseAddress = new Uri(ServerUrl); - client.DefaultRequestHeaders.Clear(); - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - try - { - string residentsBody = "api/residents/" + resident.ApartmentId; - var response = client.GetAsync(residentsBody).Result; - - if (response.IsSuccessStatusCode) - { - var residentList = response.Content.ReadAsAsync>().Result; - return residentList.ToList(); - } - - } - catch (Exception ex) - { - new MessageDialog(ex.Message).ShowAsync(); - } - return null; - } - } - ///Get Get Residents/// - public void CreateResident(Resident resident) - { - using (var client = new HttpClient(handler)) - { - client.BaseAddress = new Uri(ServerUrl); - client.DefaultRequestHeaders.Clear(); - client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - try - { - string postBody = JsonConvert.SerializeObject(resident); - var response = client.PostAsync("api/Residents", - new StringContent(postBody, Encoding.UTF8, "application/json")) - .Result; - if (response.IsSuccessStatusCode) - { - new MessageDialog("Success").ShowAsync(); - } - else - { - new MessageDialog("Error").ShowAsync(); - } - - } - catch (Exception ex) - { - new MessageDialog(ex.Message).ShowAsync(); - } - } - - } - - } -} diff --git a/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs b/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs index 737a1f3..5ee5019 100644 --- a/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs +++ b/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs @@ -17,6 +17,7 @@ namespace ApartmentManager.Singletons { Users = new ObservableCollection(); Residents = new ObservableCollection(); + Defects = new ObservableCollection(); } } } diff --git a/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs b/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs index 39f352e..66e081e 100644 --- a/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs +++ b/ApartmentManager/ApartmentManager/Singletons/CatalogSingleton.cs @@ -20,11 +20,11 @@ namespace ApartmentManager.Singletons public ObservableCollection DefectPictures { get; set; } public int DefectId { get; set; } - public ObservableCollection DefectComments { get; set; } + public ObservableCollection DefectComments { get; set; } public Defect Defect { get; set; } private CatalogSingleton() { - DefectComments = new ObservableCollection(); + DefectComments = new ObservableCollection(); Residents = new ObservableCollection(); Defects = new ObservableCollection(); DefectPictures = new ObservableCollection(); diff --git a/ApartmentManager/ApartmentManager/View/ApartmentDefectPage.xaml b/ApartmentManager/ApartmentManager/View/ApartmentDefectPage.xaml index 001de31..7b4a853 100644 --- a/ApartmentManager/ApartmentManager/View/ApartmentDefectPage.xaml +++ b/ApartmentManager/ApartmentManager/View/ApartmentDefectPage.xaml @@ -1,11 +1,11 @@