public IActionResult Create()
        {
            var place = new TouristPlaceViewModel
            {
                Rating = 1
            };

            ViewBag.Types = new SelectList(touristPlaceTypeService.GetTouristPlaceTypes(), "Id", "TypeName");
            return(View(place));
        }
        public IActionResult Delete(int id)
        {
            dynamic place = touristPlaceService.GetTouristPlace(id);

            if (place == null)
            {
                return(NotFound());
            }
            TouristPlaceViewModel model = _mapper.Map <TouristPlaceViewModel>(place);

            model.TypeName = touristPlaceTypeService.GetTouristPlaceType(model.TypeId).TypeName;
            return(View(model));
        }
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TouristPlaceViewModel touristPlace = ch.FindData(id.GetValueOrDefault());

            if (touristPlace == null)
            {
                return(HttpNotFound());
            }
            return(View(touristPlace));
        }
        public IActionResult Edit(int id)
        {
            var place = touristPlaceService.GetTouristPlace(id);

            if (place == null)
            {
                return(NotFound());
            }
            ViewBag.Types = new SelectList(touristPlaceTypeService.GetTouristPlaceTypes(), "Id", "TypeName");
            TouristPlaceViewModel model = _mapper.Map <TouristPlaceViewModel>(place);

            model.TypeName = touristPlaceTypeService.GetTouristPlaceType(model.TypeId).TypeName;
            return(View(model));
        }
        public void EditData(TouristPlaceViewModel touristPlace, HttpPostedFileBase ImageUpload)
        {
            if (ImageUpload != null)
            {
                string path = HostingEnvironment.MapPath("~/ImageFiles/" + ImageUpload.FileName);
                ImageUpload.SaveAs(path);
                touristPlace.Image = "~/ImageFiles/" + ImageUpload.FileName;
            }

            TouristPlaceEntityModel touristPlaceEntityModel = ConvertViewToEntityModel(touristPlace);

            db.Entry(touristPlaceEntityModel).State = EntityState.Modified;
            db.SaveChanges();
        }
 public ActionResult Edit(TouristPlaceViewModel touristPlace, string CurrentName, HttpPostedFileBase ImageUpload)
 {
     if (ModelState.IsValid)
     {
         if (ch.CheckIfNameExist(touristPlace.Name, CurrentName))
         {
             ModelState.AddModelError("Name", "Name already exist");
             return(View(touristPlace));
         }
         ch.EditData(touristPlace, ImageUpload);
         return(RedirectToAction("Index"));
     }
     return(View(touristPlace));
 }
        private TouristPlaceEntityModel ConvertViewToEntityModel(TouristPlaceViewModel touristPlaceViewModel)
        {
            TouristPlaceEntityModel touristPlaceEntityModel = new TouristPlaceEntityModel()
            {
                ID      = touristPlaceViewModel.ID,
                Name    = touristPlaceViewModel.Name,
                Address = touristPlaceViewModel.Address,
                Rating  = touristPlaceViewModel.Rating,
                Type    = touristPlaceViewModel.Type,
                Image   = touristPlaceViewModel.Image
            };

            return(touristPlaceEntityModel);
        }
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TouristPlaceViewModel touristPlace = ch.FindData(id.GetValueOrDefault());

            if (touristPlace == null)
            {
                return(HttpNotFound());
            }
            ViewBag.imgSrc = Url.Content(touristPlace.Image);
            return(View(touristPlace));
        }
        public ActionResult Create(TouristPlaceViewModel touristPlace, HttpPostedFileBase Image)
        {
            if (ModelState.IsValid)
            {
                if (ch.CheckIfNameExist(touristPlace.Name, ""))
                {
                    ModelState.AddModelError("Name", "Name already exist");
                    return(View(touristPlace));
                }
                ch.SaveData(touristPlace, Image);
                return(RedirectToAction("Index"));
            }

            return(View(touristPlace));
        }
        public void SaveData(TouristPlaceViewModel touristPlace, HttpPostedFileBase Image)
        {
            if (Image != null)
            {
                string path = HostingEnvironment.MapPath("~/ImageFiles/" + Image.FileName);
                Image.SaveAs(path);
                touristPlace.Image = "~/ImageFiles/" + Image.FileName;
            }
            else
            {
                return;
            }

            db.TouristPlacesEntityModel.Add(ConvertViewToEntityModel(touristPlace));
            db.SaveChanges();
        }
        public IActionResult Index(string search, string sort)
        {
            dynamic touristPlaces = touristPlaceService.GetTouristPlaces();

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

            foreach (dynamic place in touristPlaces)
            {
                TouristPlaceViewModel p = _mapper.Map <TouristPlaceViewModel>(place);
                p.TypeName = touristPlaceTypeService.GetTouristPlaceType(place.TypeId).TypeName;
                places.Add(p);
            }

            TempData["search"] = search;
            TempData["sort"]   = sort;

            if (search == null)
            {
                if (sort != null && sort == "asc")
                {
                    var sorted = places.OrderBy(place => place.Rating);
                    return(View(sorted));
                }
                else if (sort != null && sort == "desc")
                {
                    var sorted = places.OrderByDescending(place => place.Rating);
                    return(View(sorted));
                }
                return(View(places));
            }
            else
            {
                var filtered = places.Where(place => place.Name.ToLower().Contains(search.ToLower()));
                if (sort != null && sort == "asc")
                {
                    var sorted = filtered.OrderBy(place => place.Rating);
                    return(View(sorted));
                }
                else if (sort != null && sort == "desc")
                {
                    var sorted = filtered.OrderByDescending(place => place.Rating);
                    return(View(sorted));
                }
                return(View(filtered.ToList()));
            }
        }
        public IActionResult Edit(int id, [Bind("Id,Name,Address,Rating,TypeId,ImageFile,Image")] TouristPlaceViewModel model)
        {
            dynamic place = touristPlaceService.GetTouristPlace(id);

            if (place.Id != model.Id)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                if (model.ImageFile != null)
                {
                    string wwwRootPath = _hostEnvironment.WebRootPath;
                    if (model.Image != null)
                    {
                        string placePrevImgLocation = Path.Combine(wwwRootPath, "image", model.Image);
                        if (System.IO.File.Exists(placePrevImgLocation))
                        {
                            System.IO.File.Delete(placePrevImgLocation);
                        }
                    }
                    string fileName  = Path.GetFileNameWithoutExtension(model.ImageFile.FileName);
                    string extension = Path.GetExtension(model.ImageFile.FileName);
                    model.Image = fileName = fileName + Guid.NewGuid() + extension;
                    string path = Path.Combine(wwwRootPath + "/image/", fileName);
                    using var fileStream = new FileStream(path, FileMode.Create);
                    model.ImageFile.CopyTo(fileStream);
                }
                place.Id      = model.Id;
                place.Name    = model.Name;
                place.Address = model.Address;
                place.TypeId  = model.TypeId;
                place.Rating  = model.Rating;
                place.Image   = model.Image;

                touristPlaceService.UpdateTouristPlace(place);
                return(Redirect(GetRedirectUrl()));
            }
            return(View(model));
        }
 public IActionResult Create([Bind("Name,Address,TypeId,Rating,ImageFile")] TouristPlaceViewModel model)
 {
     if (ModelState.IsValid)
     {
         if (model.ImageFile != null)
         {
             string wwwRootPath = _hostEnvironment.WebRootPath;
             string fileName    = Path.GetFileNameWithoutExtension(model.ImageFile.FileName);
             string extension   = Path.GetExtension(model.ImageFile.FileName);
             model.Image = fileName = fileName + Guid.NewGuid() + extension;
             string path = Path.Combine(wwwRootPath + "/image/", fileName);
             using var fileStream = new FileStream(path, FileMode.Create);
             model.ImageFile.CopyTo(fileStream);
         }
         TouristPlace entity = _mapper.Map <TouristPlace>(model);
         touristPlaceService.InsertTouristPlace(entity);
         if (entity.Id > 0)
         {
             return(Redirect(GetRedirectUrl()));
         }
     }
     ViewBag.Types = new SelectList(touristPlaceTypeService.GetTouristPlaceTypes(), "Id", "TypeName");
     return(View(model));
 }
        public TouristPlaceViewModel ShowDetails(int id)
        {
            TouristPlaceViewModel touristPlace = ConvertEnityToViewModel(db.TouristPlacesEntityModel.Find(id));

            return(touristPlace);
        }