private async Task Login() { try { dynamic param = new { username = InputUser.Username, password = InputUser.Password, branchName = SelectedBranch }; RootUserObject Response = await ObjAuthService.PostAPI("login", param, _UserPath); if (Response.Status != "ok") { IsLoading = false; MessageBox.Show(Response.Msg, "UPO$$"); } else { Properties.Settings.Default.CurrentUsername = Response.Data[0].Username; Properties.Settings.Default.CurrentBranch = SelectedBranch; Properties.Settings.Default.CurrentUserRole = Response.Data[0].Role; Properties.Settings.Default.Save(); IsLoading = false; //change viewModel to Dashboard screen OnLoginCompleted(EventArgs.Empty); } } catch (Exception e) { MessageBox.Show(e.Message.ToString(), "UPO$$"); } }
private async Task Search() { try { var currentPage = Pagination.CurrentPage; dynamic param = new { page = currentPage, username = InputUser.Username }; RootUserObject Response = await ObjUserService.PostAPI("getUserList", param, _Path); if (Response.Status != "ok") { IsLoading = false; MessageBox.Show(Response.Msg, "UPO$$"); UserList = null; Pagination = new Pagination { CurrentPage = 1, CurrentRecord = "0 - 0", TotalPage = 1, TotalRecord = 0 }; } else { //record section var totalRecord = Response.Total; var fromRecord = currentPage == 0 ? 1 : (currentPage * 70) - 69; var toRecord = currentPage == 0 ? totalRecord : (fromRecord + 69 < totalRecord ? fromRecord + 69 : totalRecord); //page section var totalPage = currentPage == 0 ? 1 : Convert.ToInt32(Math.Ceiling((double)totalRecord / 70)); Pagination = new Pagination { CurrentRecord = fromRecord.ToString() + " ~ " + toRecord.ToString(), TotalRecord = totalRecord, CurrentPage = currentPage == 0 ? 1 : currentPage, TotalPage = totalPage }; //datagrid UserList = new ObservableCollection <User>(Response.Data.OrderBy(property => property.Id)); } } catch (Exception e) { MessageBox.Show(e.Message.ToString(), "UPO$$"); UserList = null; Pagination = new Pagination { CurrentPage = 1, CurrentRecord = "0 - 0", TotalPage = 1, TotalRecord = 0 }; } RefreshTextBox(); }
private async void LV_ExitBtn(object sender, MouseButtonEventArgs e) { bool logoutFail = false; var msgBoxResult = MessageBox.Show("Do you want to exit UPO$$ ?", "UPO$$", MessageBoxButton.YesNo, MessageBoxImage.Question); if (msgBoxResult == MessageBoxResult.Yes) { try { dynamic param = new { username = Properties.Settings.Default.CurrentUsername }; RootUserObject Response = await ObjLogoutService.PostAPI("logout", param, "user"); if (Response.Status == "ok") { Properties.Settings.Default.CurrentUsername = ""; Properties.Settings.Default.CurrentUserRole = ""; Properties.Settings.Default.Save(); // shut down Application.Current.Shutdown(); } else { logoutFail = true; } } catch (Exception error) { logoutFail = true; } } if (logoutFail) { var exitResult = MessageBox.Show("Logout error, there might be internet connection problem, do you still want to exit UPO$$ ?" + "\b(Note: Logout like this may cause some server issues, please contact IT support)", "UPO$$", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (exitResult == MessageBoxResult.Yes) { Properties.Settings.Default.CurrentUsername = ""; Properties.Settings.Default.CurrentUserRole = ""; Properties.Settings.Default.Save(); // shut down Application.Current.Shutdown(); } } }
public async Task <RootUserObject> UserPostAPI(string apiCommand, object param) { dynamic requestBody = new { command = apiCommand, @params = param }; var content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json"); try { var response = await _request.PostAsync("" + _Path, content); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); RootUserObject responseObj = JsonConvert.DeserializeObject <RootUserObject>(responseString); if (responseObj.Data != null) { foreach (User user in responseObj.Data) { switch (user.Role.ToString()) { case "1": user.Role = "Super Admin"; break; case "2": user.Role = "Admin"; break; case "3": user.Role = "Staff"; break; default: user.Role = "Unknown"; break; } } } return(responseObj); } catch (HttpRequestException e) { Console.WriteLine("\nException Caught!"); Console.WriteLine("Message :{0} ", e.Message); return(new RootUserObject { Status = "error", Msg = e.Message, Data = null }); } }
private async Task Add() { try { UserInputDialog _defaultInputDialog = new UserInputDialog("Please fill in the details of new account", mode: "add"); if (_defaultInputDialog.ShowDialog() == true) { if (_defaultInputDialog.Result is null || _defaultInputDialog.Result.Username == "") { IsLoading = false; MessageBox.Show("New username can't be empty", "UPO$$"); } else if (_defaultInputDialog.Result.Password == "") { IsLoading = false; MessageBox.Show("New password can't be empty", "UPO$$"); } else { dynamic param = new { username = _defaultInputDialog.Result.Username, password = _defaultInputDialog.Result.Password, role = _defaultInputDialog.Result.Role, branchName = _defaultInputDialog.Result.Branch_name }; RootUserObject Response = await ObjUserService.PostAPI("addUser", param, _Path); MessageBox.Show(Response.Msg, "UPO$$"); if (Response.Status is "ok") { RefreshTextBox(); await Search(); } } } }