public ActionResult Index(LoginViewModel model) { if (ModelState.IsValid) { // Call api var client = connector.Initial(); client.Timeout = -1; var request = new RestRequest("users/authenticate", Method.POST); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(new { Password = model.Password, Email = model.Email }); IRestResponse response = client.Execute(request); if (response.IsSuccessful) { AuthenticationApiResultModel result = JsonConvert.DeserializeObject <AuthenticationApiResultModel>(response.Content); if (result.ErrorCode != 0) { ViewBag.error = result.Message; return(RedirectToAction("Index", "Login")); } // Store access token and user to session Session["token"] = result.Data[0].Token; // Call api to get authentication user info request = new RestRequest("users/me", Method.GET); request.AddHeader("Authorization", "Bearer " + result.Data[0].Token); response = client.Execute(request); if (response.IsSuccessful) { UsersApiResultModel admin = JsonConvert.DeserializeObject <UsersApiResultModel>(response.Content); Session["admin"] = result.Data[0]; } return(RedirectToAction("Index", "TourInfo")); } else { ViewBag.error = "Login failed"; return(RedirectToAction("Index", "Login")); } } return(View()); }
public ActionResult Edit(string id) { if (id == null) { return(RedirectToAction("Index", "User")); } string token = Session["token"].ToString(); var client = connector.Initial(); client.Timeout = -1; var request = new RestRequest("admin/users/" + id, Method.GET); request.AddHeader("Authorization", "Bearer " + token); IRestResponse response = client.Execute(request); UserModel user = new UserModel(); UserEditViewModel model; if (response.IsSuccessful) { UsersApiResultModel content = JsonConvert.DeserializeObject <UsersApiResultModel>(response.Content); user = content.Data[0]; model = new UserEditViewModel() { Name = user.Name, Email = user.Email, PhoneNumber = user.Name, Avatar = user.Avatar, Bio = user.Bio, Slogan = user.Slogan, Job = user.Job, Birthday = user.Birthday, Gender = user.Gender, Address = user.Address, Role = user.Role }; return(View(model)); } else { return(RedirectToAction("Index", "User")); } }
public ActionResult GetUsers() { JsonResult result = new JsonResult(); try { // server side params int start = Convert.ToInt32(Request["start"]); int length = Convert.ToInt32(Request["length"]); string draw = Request.Form.GetValues("draw")[0]; string searchValue = Request.Form.GetValues("search[value]")[0]; string sortColumnName = Request["columns[" + Request["order[0][column]"] + "][name]"]; string sortDirection = Request["order[0][dir]"]; int totalRecords = 0; int page = start / length + 1; // call api to get users data string token = Session["token"].ToString(); var client = connector.Initial(); client.Timeout = -1; var request = new RestRequest("admin/users", Method.GET); request.AddHeader("Authorization", "Bearer " + token); request.AddParameter("page", page); request.AddParameter("pageSize", length); IRestResponse response = client.Execute(request); // handle result if (response.IsSuccessful) { UsersApiResultModel content = JsonConvert.DeserializeObject <UsersApiResultModel>(response.Content); List <UserModel> users = new List <UserModel>(); users.AddRange(content.Data); totalRecords = content.Pagination.TotalElement; int recFilter = totalRecords; // handle search if (!string.IsNullOrEmpty(searchValue) && !string.IsNullOrWhiteSpace(searchValue)) { users = users.Where( p => p.Name.ToString().ToLower().Contains(searchValue.ToLower()) || p.Email.ToString().ToLower().Contains(searchValue.ToLower()) || p.PhoneNumber.ToString().ToLower().Contains(searchValue.ToLower()) || p.Birthday.ToString().ToLower().Contains(searchValue.ToLower()) || p.Gender.ToString().ToLower().Contains(searchValue.ToLower()) || p.Address.ToString().ToLower().Contains(searchValue.ToLower()) || p.Role.ToString().ToLower().Contains(searchValue.ToLower()) ).ToList(); totalRecords = users.Count; recFilter = users.Count; } // handle sort by column if (sortColumnName == "Name") { users = (sortDirection == "asc") ? users.OrderBy(u => u.Name).ToList() : users.OrderByDescending(u => u.Name).ToList(); } else if (sortColumnName == "Email") { users = (sortDirection == "asc") ? users.OrderBy(u => u.Email).ToList() : users.OrderByDescending(u => u.Email).ToList(); } else if (sortColumnName == "Phone") { users = (sortDirection == "asc") ? users.OrderBy(u => u.PhoneNumber).ToList() : users.OrderByDescending(u => u.PhoneNumber).ToList(); } else if (sortColumnName == "Birthday") { users = (sortDirection == "asc") ? users.OrderBy(u => u.Birthday).ToList() : users.OrderByDescending(u => u.Birthday).ToList(); } else if (sortColumnName == "Gender") { users = (sortDirection == "asc") ? users.OrderBy(u => u.Gender).ToList() : users.OrderByDescending(u => u.Gender).ToList(); } else if (sortColumnName == "Address") { users = (sortDirection == "asc") ? users.OrderBy(u => u.Address).ToList() : users.OrderByDescending(u => u.Address).ToList(); } else if (sortColumnName == "Role") { users = (sortDirection == "asc") ? users.OrderBy(u => u.Role).ToList() : users.OrderByDescending(u => u.Role).ToList(); } result = Json(new { draw = Convert.ToInt32(draw), recordsTotal = totalRecords, recordsFiltered = totalRecords, data = users }, JsonRequestBehavior.AllowGet);; } } catch (Exception ex) { Console.WriteLine(ex); } return(result); }