From 4751ab7e29caa28a06cfb773d891c99bfbdcdb59 Mon Sep 17 00:00:00 2001 From: Donatas Adamonis Date: Fri, 5 May 2017 16:55:22 +0200 Subject: Resident add / remove/update --- .../ApartmentManager/Handler/ResidentsHandler.cs | 62 ++++++++++++---------- .../ApartmentManager/Model/Resident.cs | 2 +- .../ApartmentManager/Persistency/ApiClient.cs | 1 + .../ApartmentManager/View/ResidentsPage.xaml | 2 +- .../Controllers/ResidentsController.cs | 16 +++++- ApartmentManager/HousingWebApi/DataModel.cs | 24 ++++----- .../HousingWebApi/HousingWebAPI.csproj | 4 +- ApartmentManager/HousingWebApi/Models/Resident.cs | 5 -- ApartmentManager/HousingWebApi/Web.config | 8 ++- 9 files changed, 68 insertions(+), 56 deletions(-) diff --git a/ApartmentManager/ApartmentManager/Handler/ResidentsHandler.cs b/ApartmentManager/ApartmentManager/Handler/ResidentsHandler.cs index c04007f..d406880 100644 --- a/ApartmentManager/ApartmentManager/Handler/ResidentsHandler.cs +++ b/ApartmentManager/ApartmentManager/Handler/ResidentsHandler.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -7,6 +8,7 @@ using Windows.UI.Popups; using ApartmentManager.Model; using ApartmentManager.Persistency; using ApartmentManager.ViewModel; +using Newtonsoft.Json; namespace ApartmentManager.Handler { @@ -22,14 +24,17 @@ namespace ApartmentManager.Handler { Resident resident = new Resident(); resident.ApartmentNr = ApartmentViewModel.ApartmentNumber; + + var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentNr); + IEnumerable residentlist = JsonConvert.DeserializeObject>(residentsFromDatabase); - var residentlist = new PersistenceFacade().GetApartmentResidents(resident); ApartmentViewModel.CatalogSingleton.Residents.Clear(); - + ApartmentViewModel.NewResident = new Resident(); foreach (var resident2 in residentlist) { ApartmentViewModel.CatalogSingleton.Residents.Add(resident2); } + } public void CreateResident() @@ -37,8 +42,7 @@ namespace ApartmentManager.Handler try { Resident resident = new Resident(); - resident.ResidentNr = ApartmentViewModel.CatalogSingleton.Residents.Count; - resident.ResidentNr++; + resident.ApartmentNr = ApartmentViewModel.ApartmentNumber; resident.FirstName = ApartmentViewModel.NewResident.FirstName; resident.LastName = ApartmentViewModel.NewResident.LastName; @@ -47,13 +51,14 @@ namespace ApartmentManager.Handler resident.Picture = ApartmentViewModel.NewResident.Picture; resident.Phone = ApartmentViewModel.NewResident.Phone; - new PersistenceFacade().CreateResident(resident); + ApiClient.PostData("api/residents/", resident); - - var residentsFromDatabase = new PersistenceFacade().GetApartmentResidents(resident); - ApartmentViewModel.CatalogSingleton.Residents.Clear(); + var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentNr); + IEnumerable residentlist = JsonConvert.DeserializeObject>(residentsFromDatabase); - foreach (var resident2 in residentsFromDatabase) + ApartmentViewModel.CatalogSingleton.Residents.Clear(); + ApartmentViewModel.NewResident = new Resident(); + foreach (var resident2 in residentlist) { ApartmentViewModel.CatalogSingleton.Residents.Add(resident2); } @@ -69,6 +74,7 @@ namespace ApartmentManager.Handler try { Resident resident = new Resident(); + resident.ResidentNr = ApartmentViewModel.NewResident.ResidentNr; resident.ApartmentNr = ApartmentViewModel.ApartmentNumber; resident.FirstName = ApartmentViewModel.NewResident.FirstName; resident.LastName = ApartmentViewModel.NewResident.LastName; @@ -77,17 +83,17 @@ namespace ApartmentManager.Handler resident.Picture = ApartmentViewModel.NewResident.Picture; resident.Phone = ApartmentViewModel.NewResident.Phone; - //new PersistenceFacade().CreateHotel(hotel); + ApiClient.DeleteData("api/residents/" + resident.ResidentNr); - ////HotelViewModel.Hotels.Hotels.Add(hotel); - //var hotelsFromDatabase = new PersistenceFacade().GetHotels(); + var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentNr); + IEnumerable residentlist = JsonConvert.DeserializeObject>(residentsFromDatabase); - //HotelViewModel.HotelCatalogSingleton.Hotels.Clear(); - //foreach (var hotel1 in hotelsFromDatabase) - //{ - // ApartmentViewModel.HotelCatalogSingleton.Hotels.Add(hotel1); - - //} + ApartmentViewModel.CatalogSingleton.Residents.Clear(); + ApartmentViewModel.NewResident = new Resident(); + foreach (var resident2 in residentlist) + { + ApartmentViewModel.CatalogSingleton.Residents.Add(resident2); + } } catch (Exception e) { @@ -99,6 +105,7 @@ namespace ApartmentManager.Handler try { Resident resident = new Resident(); + resident.ResidentNr = ApartmentViewModel.NewResident.ResidentNr; resident.ApartmentNr = ApartmentViewModel.ApartmentNumber; resident.FirstName = ApartmentViewModel.NewResident.FirstName; resident.LastName = ApartmentViewModel.NewResident.LastName; @@ -107,17 +114,16 @@ namespace ApartmentManager.Handler resident.Picture = ApartmentViewModel.NewResident.Picture; resident.Phone = ApartmentViewModel.NewResident.Phone; - //new PersistenceFacade().CreateHotel(hotel); - - ////HotelViewModel.Hotels.Hotels.Add(hotel); - //var hotelsFromDatabase = new PersistenceFacade().GetHotels(); - - //HotelViewModel.HotelCatalogSingleton.Hotels.Clear(); - //foreach (var hotel1 in hotelsFromDatabase) - //{ - // ApartmentViewModel.HotelCatalogSingleton.Hotels.Add(hotel1); + ApiClient.PutData("api/residents/" + resident.ResidentNr,resident); + var residentsFromDatabase = ApiClient.GetData("api/ApartmentResidents/" + resident.ApartmentNr); + IEnumerable residentlist = JsonConvert.DeserializeObject>(residentsFromDatabase); - //} + ApartmentViewModel.CatalogSingleton.Residents.Clear(); + ApartmentViewModel.NewResident = new Resident(); + foreach (var resident2 in residentlist) + { + ApartmentViewModel.CatalogSingleton.Residents.Add(resident2); + } } catch (Exception e) { diff --git a/ApartmentManager/ApartmentManager/Model/Resident.cs b/ApartmentManager/ApartmentManager/Model/Resident.cs index f9cede7..e9f39cd 100644 --- a/ApartmentManager/ApartmentManager/Model/Resident.cs +++ b/ApartmentManager/ApartmentManager/Model/Resident.cs @@ -19,7 +19,7 @@ namespace ApartmentManager.Model public int ResidentNr { get; set; } public Resident() { } - public Resident(string FirstName, string LastName, int Phone, DateTime BirthDate, string Email, int ApartmentNr, int ResidentNr) + public Resident(string FirstName, string LastName, int Phone, DateTime BirthDate, string Email, int ApartmentNr ,int ResidentNr) { this.FirstName = FirstName; this.LastName = LastName; diff --git a/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs b/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs index e803005..2faa07a 100644 --- a/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs +++ b/ApartmentManager/ApartmentManager/Persistency/ApiClient.cs @@ -24,6 +24,7 @@ namespace ApartmentManager.Persistency if (response.IsSuccessStatusCode) { return response.Content.ReadAsStringAsync().Result; + } else return null; } diff --git a/ApartmentManager/ApartmentManager/View/ResidentsPage.xaml b/ApartmentManager/ApartmentManager/View/ResidentsPage.xaml index 8c8d053..c68b1bc 100644 --- a/ApartmentManager/ApartmentManager/View/ResidentsPage.xaml +++ b/ApartmentManager/ApartmentManager/View/ResidentsPage.xaml @@ -29,7 +29,7 @@