private async void ExecuteLoginCommand() { Requester requester = new Requester(); UserRequestModel requestModel = new UserRequestModel { UserName = this.UserName, Password = Password }; string requestBody = JsonConvert.SerializeObject(requestModel); HttpStringContent requestContent = new HttpStringContent(requestBody, UnicodeEncoding.Utf8, "application/json"); string response = string.Empty; try { response = await requester.PutJsonAsync("/api/users/token", requestContent); } catch (Exception) { MessageDialogNotifier.Notify("There was an error on the server. Please contact the server administrators."); } UserResponseModel user = JsonConvert.DeserializeObject<UserResponseModel>(response); if (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.Token)) { MessageDialogNotifier.Notify("Invalid username or password."); } else { Data data = new Data(); UserDatabaseModel databaseUser = new UserDatabaseModel { FirstName = user.FirstName, LastName = user.LastName, Id = user.Id, RegistrationDate = user.RegistrationDate, UserName = user.UserName, Token = user.Token }; await data.UpdateCurrentUserAsync(databaseUser); UserDatabaseModel currentUser = await data.GetCurrentUser(); MessageDialogNotifier.Notify(string.Format("Hello {0} {1}!\nYou are now logged in.", currentUser.FirstName, currentUser.LastName)); } }
private async void ExecuteSendToServerCommand() { GeolocationAccessStatus accessStatus = await Geolocator.RequestAccessAsync(); if (accessStatus == GeolocationAccessStatus.Denied) { return; } Geolocator geolocator = new Geolocator(); Geoposition position = await geolocator.GetGeopositionAsync(); double latitude = position.Coordinate.Latitude; double longitude = position.Coordinate.Longitude; this.Room.Latitude = latitude; this.Room.Longitude = longitude; RoomRequestModel roomDatabaseModel = new RoomRequestModel { Geometry = await RoomGeometryViewModel.CreateFromRoom(this.Room), Room = this.Room }; string requestBody = JsonConvert.SerializeObject(roomDatabaseModel); HttpStringContent requestContent = new HttpStringContent(requestBody, UnicodeEncoding.Utf8, "application/json"); Data data = new Data(); UserDatabaseModel currentUser = await data.GetCurrentUser(); if (currentUser == null || string.IsNullOrEmpty(currentUser.Token)) { MessageDialogNotifier.Notify("You must be logged in to send room information on the server."); return; } string token = currentUser.Token; Requester requester = new Requester(); string serverResult = string.Empty; try { serverResult = await requester.PostJsonAsync("/api/roomGeometry", requestContent, token); } catch (COMException) { MessageDialogNotifier.Notify("There was an error on the server. Please contact the server administrators.."); } RoomRequestModel result = JsonConvert.DeserializeObject<RoomRequestModel>(serverResult); if (result == null) { MessageDialogNotifier.Notify("The room information was not valid or you are not authenticated."); } else { MessageDialogNotifier.Notify("The room information was successfully saved in the database."); } }