public ActionResult Detail(string id)
        {
            ViewBag.PlacesPicklist = this.GetPlacesPicklist();

            if (id == null)
            {
                return(RedirectToAction("Index", "Place"));
            }

            string token = Session["token"].ToString();

            var client = connector.Initial();

            client.Timeout = -1;
            var request = new RestRequest("admin/tour-infos/" + id, Method.GET);

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

            TourInfoModel         tourInfo = new TourInfoModel();
            TourInfoEditViewModel model;

            if (response.IsSuccessful)
            {
                var settings = new JsonSerializerSettings
                {
                    NullValueHandling     = NullValueHandling.Ignore,
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };
                TourInfoApiResultModel content = JsonConvert.DeserializeObject <TourInfoApiResultModel>(response.Content, settings);
                tourInfo = content.Data[0];
                model    = new TourInfoEditViewModel()
                {
                    Id             = tourInfo.Id,
                    Name           = tourInfo.Name,
                    Images         = tourInfo.Images,
                    Rating         = tourInfo.Rating,
                    StartPlace     = tourInfo.StartPlace.Id,
                    DestinatePlace = tourInfo.DestinatePlace.Id,
                    CreateBy       = tourInfo.CreateBy.Name
                };
                return(View(model));
            }
            else
            {
                return(RedirectToAction("Index", "Place"));
            }
        }
Esempio n. 2
0
        public ActionResult GetTourInfos()
        {
            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 data
                string token  = Session["token"].ToString();
                var    client = connector.Initial();
                client.Timeout = -1;
                var request = new RestRequest("admin/tour-infos", 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
                    };
                    TourInfoApiResultModel content   = JsonConvert.DeserializeObject <TourInfoApiResultModel>(response.Content, settings);
                    List <TourInfoModel>   tourInfos = new List <TourInfoModel>();
                    tourInfos.AddRange(content.Data);
                    totalRecords = content.Pagination.TotalElement;
                    int recFilter = totalRecords;

                    // handle search
                    if (!string.IsNullOrEmpty(searchValue) && !string.IsNullOrWhiteSpace(searchValue))
                    {
                        tourInfos = tourInfos.Where(
                            p => p.Name.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.Rating.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.StartPlace.Name.ToString().ToLower().Contains(searchValue.ToLower()) ||
                            p.DestinatePlace.Name.ToString().ToLower().Contains(searchValue.ToLower())
                            ).ToList();

                        totalRecords = tourInfos.Count;
                        recFilter    = tourInfos.Count;
                    }

                    // handle sort by column
                    if (sortColumnName == "Name")
                    {
                        tourInfos = (sortDirection == "asc") ? tourInfos.OrderBy(t => t.Name).ToList() : tourInfos.OrderByDescending(t => t.Name).ToList();
                    }
                    else if (sortColumnName == "Rating")
                    {
                        tourInfos = (sortDirection == "asc") ? tourInfos.OrderBy(t => t.Rating).ToList() : tourInfos.OrderByDescending(t => t.Rating).ToList();
                    }
                    else if (sortColumnName == "StartPlace")
                    {
                        tourInfos = (sortDirection == "asc") ? tourInfos.OrderBy(t => t.StartPlace.Name).ToList() : tourInfos.OrderByDescending(t => t.StartPlace.Name).ToList();
                    }
                    else if (sortColumnName == "DestinatePlace")
                    {
                        tourInfos = (sortDirection == "asc") ? tourInfos.OrderBy(t => t.DestinatePlace.Name).ToList() : tourInfos.OrderByDescending(t => t.DestinatePlace.Name).ToList();
                    }

                    result = Json(new
                    {
                        draw            = Convert.ToInt32(draw),
                        recordsTotal    = totalRecords,
                        recordsFiltered = totalRecords,
                        data            = tourInfos
                    }, JsonRequestBehavior.AllowGet);;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(result);
        }