From 28c8f5e1f6d583afcbd2c53327dca2c5779be6d6 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Sat, 27 May 2017 18:16:41 +0200 Subject: Added managing residents functionality for board member, fixed birth dates. --- .../ApartmentManager/Handler/BmHandler.cs | 71 ++++++++- .../ApartmentManager/Handler/LoginHandler.cs | 1 + .../ApartmentManager/Model/Resident.cs | 16 +-- .../ApartmentManager/Singletons/BmSingleton.cs | 5 +- .../ApartmentManager/View/BmResidentsPage.xaml | 158 ++++++++++++++++++++- .../ApartmentManager/ViewModel/BmViewModel.cs | 25 +++- 6 files changed, 255 insertions(+), 21 deletions(-) diff --git a/ApartmentManager/ApartmentManager/Handler/BmHandler.cs b/ApartmentManager/ApartmentManager/Handler/BmHandler.cs index 2b6b5b0..27dc827 100644 --- a/ApartmentManager/ApartmentManager/Handler/BmHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/BmHandler.cs @@ -9,7 +9,7 @@ using ApartmentManager.Singletons; namespace ApartmentManager.Handler { - public class BmHandler + public class BmHandler { private BmViewModel _vm; @@ -18,9 +18,12 @@ namespace ApartmentManager.Handler _vm = vm; } + + #region APARTMENTS + public void GetApartments() { - BmSingleton.Instance.Apartments = JsonConvert.DeserializeObject> (ApiClient.GetData("api/Apartments/")); + BmSingleton.Instance.Apartments = JsonConvert.DeserializeObject>(ApiClient.GetData("api/Apartments/")); } public void CreateApartment() @@ -71,5 +74,69 @@ namespace ApartmentManager.Handler { _vm.ApartmentTemplate = new Apartment(); } + #endregion + + #region RESIDENTS + + public void GetResidents() + { + var residents = JsonConvert.DeserializeObject>(ApiClient.GetData("api/Residents/")); + BmSingleton.Instance.Residents.Clear(); + foreach (var resident in residents) BmSingleton.Instance.Residents.Add(resident); + } + + public void CreateResident() + { + try + { + ApiClient.PostData("api/Residents/", _vm.ResidentTemplate); + GetResidents(); + _vm.ResidentTemplate = new Resident(); + } + catch (Exception e) + { + var msg = new MessageDialog(e.Message).ShowAsync(); + } + } + public void UpdateResident() + { + try + { + ApiClient.PutData("api/Residents/" + _vm.ResidentTemplate.ResidentId, _vm.ResidentTemplate); + GetResidents(); + } + catch (Exception e) + { + var msg = new MessageDialog(e.Message).ShowAsync(); + } + } + public void DeleteResident() + { + try + { + ApiClient.DeleteData("api/Residents/" + _vm.ResidentTemplate.ResidentId); + BmSingleton.Instance.Residents.Remove(_vm.ResidentTemplate); + GetResidents(); + } + catch (Exception e) + { + var msg = new MessageDialog(e.Message).ShowAsync(); + } + } + + public async void UploadResidentPhoto() + { + _vm.ResidentTemplate.Picture = await ImgurPhotoUploader.UploadPhotoAsync(); + var tmp = _vm.ResidentTemplate; + _vm.ResidentTemplate = new Resident(); + _vm.ResidentTemplate = tmp; + } + + public void ClearResidentTemplate() + { + _vm.ResidentTemplate = new Resident(); + } + #endregion + } } diff --git a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs index d0088d9..e41f5ed 100644 --- a/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/LoginHandler.cs @@ -77,6 +77,7 @@ namespace ApartmentManager.Handler { BmViewModel bvm = new BmViewModel(); bvm.BmHandler.GetApartments(); + bvm.BmHandler.GetResidents(); appShell.AppFrame.Navigate(typeof(BmMainPage)); } else diff --git a/ApartmentManager/ApartmentManager/Model/Resident.cs b/ApartmentManager/ApartmentManager/Model/Resident.cs index ac20ffa..aaa7af4 100644 --- a/ApartmentManager/ApartmentManager/Model/Resident.cs +++ b/ApartmentManager/ApartmentManager/Model/Resident.cs @@ -8,7 +8,7 @@ namespace ApartmentManager.Model public int ApartmentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } - public DateTime BirthDate { get; set; } + public DateTimeOffset BirthDate { get; set; } public string Phone { get; set; } public string Email { get; set; } public string Picture { get; set; } @@ -17,19 +17,5 @@ namespace ApartmentManager.Model { BirthDate = DateTime.Now; } - public Resident(int residentId, int apartmentId, string firstName, string lastName, DateTime birthDate, string phone, string email) - { - ResidentId = residentId; - ApartmentId = apartmentId; - FirstName = firstName; - LastName = lastName; - BirthDate = birthDate; - Phone = phone; - Email = email; - } - public override string ToString() - { - return string.Format($"First name: {FirstName}, Last name: {LastName}, Birth date: {BirthDate}, Phone: {Phone}, Email: {Email} "); - } } } diff --git a/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs b/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs index 966390c..14c1100 100644 --- a/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs +++ b/ApartmentManager/ApartmentManager/Singletons/BmSingleton.cs @@ -13,6 +13,9 @@ namespace ApartmentManager.Singletons public ObservableCollection Residents { get; set; } public ObservableCollection Defects { get; set; } - private BmSingleton() { } + private BmSingleton() + { + Residents = new ObservableCollection(); + } } } diff --git a/ApartmentManager/ApartmentManager/View/BmResidentsPage.xaml b/ApartmentManager/ApartmentManager/View/BmResidentsPage.xaml index 7607d86..4bcb6b0 100644 --- a/ApartmentManager/ApartmentManager/View/BmResidentsPage.xaml +++ b/ApartmentManager/ApartmentManager/View/BmResidentsPage.xaml @@ -5,7 +5,163 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="using:ApartmentManager.View" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:vm="using:ApartmentManager.ViewModel" mc:Ignorable="d"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +