public ActionResult Edit(int id)
        {
            LostStuff entity = db.LostStuffs.Find(id);

            //get all images for entity and send to view
            string directoryPath = Server.MapPath("~/Content/UploadedFiles/" + id + "/");

            List <string> imageFullPathList = new List <string>();
            List <string> imageNameList     = new List <string>();

            var imagePathList = Directory.GetFiles(directoryPath).ToList();

            foreach (var image in imagePathList)
            {
                var imageName = Path.GetFileName(image);
                imageFullPathList.Add("~/Content/UploadedFiles/" + id + "/" + imageName);
                imageNameList.Add(imageName);
            }
            ViewData["ImagePathList"] = imageFullPathList;
            ViewData["ImageNameList"] = imageNameList;
            if (user.Id == entity.UserId)
            {
                return(View(entity));
            }
            else
            {
                return(RedirectToAction("Error", new { controller = "Account" }));
            }
        }
        public ActionResult Create(LostStuff lostStuff, IEnumerable <HttpPostedFileBase> files, HttpPostedFileBase mainImage)
        {
            if (ModelState.IsValid)
            {
                //create entity
                lostStuff.CreatedAt = DateTime.Now;
                lostStuff.UpdatedAt = DateTime.Now;
                lostStuff.UserId    = user.Id;
                lostStuff.UserName  = user.UserName;

                db.LostStuffs.Add(lostStuff);
                db.SaveChanges();

                //create directory for entity
                string directoryPath = Server.MapPath(string.Format("~/Content/UploadedFiles/" + lostStuff.Id.ToString() + "/"));
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                //save main image
                if (mainImage == null)
                {
                    lostStuff.ImageName = "default.jpg";
                    lostStuff.ImagePath = "~/Content/UploadedFiles/default.jpg";
                    db.SaveChanges();
                }
                else
                {
                    lostStuff.ImageName = mainImage.FileName;
                    lostStuff.ImagePath = "~/Content/UploadedFiles/" + lostStuff.Id.ToString() + "/" + mainImage.FileName;
                    db.SaveChanges();
                    var mainFileName = Path.GetFileName(mainImage.FileName);
                    var path         = "~/Content/UploadedFiles/" + lostStuff.Id.ToString() + "/";
                    mainImage.SaveAs(Server.MapPath(path + mainFileName));
                }

                var otherImagespath = "~/Content/UploadedFiles/" + lostStuff.Id.ToString() + "/";
                //save other images

                foreach (var file in files)
                {
                    if (file != null)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        file.SaveAs(Server.MapPath(otherImagespath + fileName));
                    }
                    else
                    {
                        continue;
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(View(lostStuff));
        }
        public ActionResult ChangeMainImage(string imageName, int id)
        {
            LostStuff entity = db.LostStuffs.Find(id);

            entity.ImageName       = imageName;
            entity.ImagePath       = "~/Content/UploadedFiles/" + entity.Id + "/" + imageName;
            db.Entry(entity).State = EntityState.Modified;
            db.SaveChanges();

            return(RedirectToAction("Edit/" + id));
        }
        public ActionResult Details(int?id)
        {
            LostStuff lostStuff     = db.LostStuffs.Find(id);
            string    directoryPath = Server.MapPath("~/Content/UploadedFiles/" + id + "/");

            //get all comments for an ad and send them to view
            List <Comment> allComments = new List <Comment>();

            allComments.AddRange(db.Comments.Where(x => x.LostStuffId == id));
            ViewData["AllComments"] = allComments;

            //get user name an send it do wiev



            List <string> imageNamesList = new List <string>();

            var imagePathList = Directory.GetFiles(directoryPath).ToList();

            foreach (var image in imagePathList)
            {
                var imageName = Path.GetFileName(image);
                imageNamesList.Add("~/Content/UploadedFiles/" + id + "/" + imageName);
            }
            ViewData["ImagePathList"] = imageNamesList;

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }



            if (lostStuff == null)
            {
                return(HttpNotFound());
            }
            if (user != null)
            {
                ViewData["CurrentUserId"] = user.Id;
            }

            //if (user.Id== lostStuff.UserId)
            //{
            //    return View(lostStuff);
            //}
            //else
            //{
            //    return RedirectToAction("Error", new { controller = "Account" });
            //}
            return(View(lostStuff));
        }
Esempio n. 5
0
        public IHttpActionResult GetLostStuff(int id)
        {
            LostStuff lostStuff = repository.GetById(id);

            if (lostStuff == null)
            {
                return(this.ResponseMessage(new HttpResponseMessage(HttpStatusCode.BadRequest)));
            }

            return(this.Ok(new
            {
                name = lostStuff.Name,
                description = lostStuff.Description,
                price = lostStuff.Price,
                userId = lostStuff.UserId,
                userName = lostStuff.UserName
            }));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            LostStuff lostStuff     = db.LostStuffs.Find(id);
            string    directoryPath = Server.MapPath(string.Format("~/Content/UploadedFiles/" + lostStuff.Id.ToString() + "/"));

            string[] files = Directory.GetFiles(directoryPath);
            foreach (string pathFile in files)
            {
                FileInfo file = new FileInfo(pathFile);
                if (file.Exists)//check file exsit or not
                {
                    file.Delete();
                }
            }
            Directory.Delete(directoryPath);

            db.LostStuffs.Remove(lostStuff);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public IHttpActionResult DeleteLostStuff(int id)
        {
            LostStuff lostStuff = repository.GetById(id);

            if (lostStuff == null)
            {
                return(NotFound());
            }

            string directoryPath = string.Format("~/Content/UploadedFiles/" + lostStuff.Id.ToString() + "/");

            try
            {
                Directory.Delete(HttpContext.Current.Server.MapPath(directoryPath));
            }catch (Exception ex)
            {
            }
            repository.Delete(lostStuff);
            return(Ok(lostStuff));
        }
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            LostStuff lostStuff = db.LostStuffs.Find(id);

            if (lostStuff == null)
            {
                return(HttpNotFound());
            }
            if (user.Id == lostStuff.UserId)
            {
                return(View(lostStuff));
            }
            else
            {
                return(RedirectToAction("Error", new { controller = "Account" }));
            }
        }
        public ActionResult Edit(LostStuff entity)
        {
            //get all images for entity and send to view
            string directoryPath = Server.MapPath("~/Content/UploadedFiles/" + entity.Id + "/");

            List <string> imageNamesList = new List <string>();

            var imagePathList = Directory.GetFiles(directoryPath).ToList();

            foreach (var image in imagePathList)
            {
                var imageName = Path.GetFileName(image);
                imageNamesList.Add("~/Content/UploadedFiles/" + entity.Id + "/" + imageName);
            }
            ViewData["ImagePathList"] = imageNamesList;


            //update entity
            entity.UpdatedAt       = DateTime.Now;
            db.Entry(entity).State = EntityState.Modified;
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
        public IHttpActionResult PostLostStuff(LostStuffRequestModel lostStuffRequest)
        {
            var userData = GetCurrentUser(db);

            LostStuff lostStuff = new LostStuff();

            var httpRequest = HttpContext.Current.Request;

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            lostStuff.CreatedAt   = DateTime.Now;
            lostStuff.UpdatedAt   = DateTime.Now;
            lostStuff.UserId      = userData.Id;
            lostStuff.UserName    = userData.UserName;
            lostStuff.Name        = lostStuffRequest.Name;
            lostStuff.Description = lostStuffRequest.Description;
            lostStuff.Price       = lostStuffRequest.Price;
            lostStuff.PhoneNumber = lostStuffRequest.PhoneNumber;
            db.LostStuffs.Add(lostStuff);
            db.SaveChanges();

            ////create directory for entity
            string directoryPath = string.Format("~/Content/UploadedFiles/" + lostStuff.Id.ToString() + "/");

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(directoryPath));
            }

            if (httpRequest.Files.Count > 0)
            {
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];

                    if (file.Equals("mainImage"))
                    {
                        if (httpRequest.Files[file] != null)
                        {
                            lostStuff.ImageName = postedFile.FileName;
                            lostStuff.ImagePath = directoryPath + postedFile.FileName;
                            postedFile.SaveAs(HttpContext.Current.Server.MapPath(directoryPath + postedFile.FileName));
                            db.SaveChanges();
                        }
                        else
                        {
                            lostStuff.ImageName = "default.jpg";
                            lostStuff.ImagePath = "~/Content/UploadedFiles/default.jpg";
                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        if (httpRequest.Files[file] != null)
                        {
                            postedFile.SaveAs(HttpContext.Current.Server.MapPath(directoryPath + postedFile.FileName));
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
            else
            {
                lostStuff.ImageName = "default.jpg";
                lostStuff.ImagePath = "~/Content/UploadedFiles/default.jpg";
                db.SaveChanges();
            }
            return(Ok());
        }