public ActionResult CreatePost(PlaceModel model)
        {
            if (ModelState.IsValid)
            {
                if (Session["token"] == null)
                {
                    return(RedirectToAction("Index", "Login"));
                }
                string token = Session["token"].ToString();
                // store img to IMGUR first
                List <string> imagesUrl = new List <string>();
                if (model.FileImages.Count() > 0)
                {
                    imagesUrl = AdminHelper.Instance.StoreImagur(model.FileImages);
                }

                var client = connector.Initial();
                client.Timeout = -1;
                var request = new RestRequest("admin/places", Method.POST);
                request.AddHeader("Authorization", "Bearer " + token);
                request.AddHeader("Content-Type", "application/json");
                request.AddJsonBody(new
                {
                    Name        = model.Name,
                    Images      = JsonConvert.SerializeObject(imagesUrl),
                    Description = model.Description,
                    TypeId      = 1
                });

                IRestResponse response = client.Execute(request);
                if (response.IsSuccessful)
                {
                    APIResultModel result = JsonConvert.DeserializeObject <APIResultModel>(response.Content);
                    if (result.ErrorCode != 0)
                    {
                        ViewBag.error = result.Message;
                        return(View());
                    }

                    return(RedirectToAction("Index", "Place"));
                }
                else
                {
                    return(RedirectToAction("Create", "Place"));
                }
            }
            return(View());
        }
        public ActionResult CreatePost(TourModel model)
        {
            if (ModelState.IsValid)
            {
                if (Session["token"] == null)
                {
                    return(RedirectToAction("Index", "Login"));
                }
                string token = Session["token"].ToString();

                var client = connector.Initial();
                client.Timeout = -1;
                var request = new RestRequest("admin/tour-infos/" + model.TourInfoId + "/tours", Method.POST);
                request.AddHeader("Authorization", "Bearer " + token);
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("id", model.TourInfoId);
                request.AddJsonBody(new
                {
                    Name       = model.Name,
                    StartDay   = model.StartDay,
                    EndDay     = model.EndDay,
                    TotalDay   = model.TotalDay,
                    TotalNight = model.TotalNight,
                    MaxMember  = model.MaxMember,
                    TourInfoId = model.TourInfoId,
                    Price      = model.Price,
                });

                IRestResponse response = client.Execute(request);
                if (response.IsSuccessful)
                {
                    APIResultModel result = JsonConvert.DeserializeObject <APIResultModel>(response.Content);
                    if (result.ErrorCode != 0)
                    {
                        ViewBag.error = result.Message;
                        return(View());
                    }

                    return(RedirectToAction("Index", "Place"));
                }
                else
                {
                    return(RedirectToAction("Create", "Place"));
                }
            }
            return(View(model));
        }
        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 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());
        }
        private SelectList GetPlacesPicklist()
        {
            // Call API to get Places
            string token  = Session["token"].ToString();
            var    client = connector.Initial();

            client.Timeout = -1;
            var request = new RestRequest("admin/places", Method.GET);

            request.AddHeader("Authorization", "Bearer " + token);
            request.AddParameter("page", 0);
            request.AddParameter("pageSize", 0);
            IRestResponse response = client.Execute(request);

            List <PlaceModel> places = new List <PlaceModel>();

            if (response.IsSuccessful)
            {
                PlacesApiResultModel content = JsonConvert.DeserializeObject <PlacesApiResultModel>(response.Content);
                places = content.Data;
            }

            return(new SelectList(places, "Id", "Name"));
        }
        public ActionResult GetPosts()
        {
            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/posts", 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)
                {
                    var settings = new JsonSerializerSettings
                    {
                        NullValueHandling     = NullValueHandling.Ignore,
                        MissingMemberHandling = MissingMemberHandling.Ignore
                    };
                    PostsApiResultModel content = JsonConvert.DeserializeObject <PostsApiResultModel>(response.Content, settings);
                    List <PostModel>    posts   = new List <PostModel>();
                    posts.AddRange(content.Data);
                    totalRecords = content.Pagination.TotalElement;
                    int recFilter = totalRecords;

                    // handle search
                    if (!string.IsNullOrEmpty(searchValue) && !string.IsNullOrWhiteSpace(searchValue))
                    {
                        posts = posts.Where(
                            p => p.Id.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.Content.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.Author.Name.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.Rating.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.TotalLike.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.TotalComment.ToString().ToLower().Contains(searchValue.ToLower())
                            ).ToList();

                        totalRecords = posts.Count;
                        recFilter    = posts.Count;
                    }

                    // handle sort by column
                    if (sortColumnName == "Id")
                    {
                        posts = (sortDirection == "asc") ? posts.OrderBy(u => u.Id).ToList() : posts.OrderByDescending(u => u.Id).ToList();
                    }
                    else if (sortColumnName == "Content")
                    {
                        posts = (sortDirection == "asc") ? posts.OrderBy(u => u.Content).ToList() : posts.OrderByDescending(u => u.Content).ToList();
                    }
                    else if (sortColumnName == "Author")
                    {
                        posts = (sortDirection == "asc") ? posts.OrderBy(u => u.Author.Name).ToList() : posts.OrderByDescending(u => u.Author.Name).ToList();
                    }
                    else if (sortColumnName == "Rating")
                    {
                        posts = (sortDirection == "asc") ? posts.OrderBy(u => u.Rating).ToList() : posts.OrderByDescending(u => u.Rating).ToList();
                    }
                    else if (sortColumnName == "TotalLike")
                    {
                        posts = (sortDirection == "asc") ? posts.OrderBy(u => u.TotalLike).ToList() : posts.OrderByDescending(u => u.TotalLike).ToList();
                    }
                    else if (sortColumnName == "TotalComment")
                    {
                        posts = (sortDirection == "asc") ? posts.OrderBy(u => u.TotalComment).ToList() : posts.OrderByDescending(u => u.TotalComment).ToList();
                    }

                    result = Json(new
                    {
                        draw            = Convert.ToInt32(draw),
                        recordsTotal    = totalRecords,
                        recordsFiltered = totalRecords,
                        data            = posts
                    }, JsonRequestBehavior.AllowGet);;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(result);
        }
        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);
        }