示例#1
0
 public ActionResult Edit(int id, Post requestPost, HttpPostedFileBase[] uploadedPhotos)
 {
     try
     {
         Post post = db.Posts.Find(id);
         if (post.UserId == User.Identity.GetUserId() || User.IsInRole("Admin"))
         {
             if (ModelState.IsValid && TryUpdateModel(post))
             {
                 post.Content   = requestPost.Content;
                 post.Date      = requestPost.Date;
                 post.ProductId = requestPost.ProductId;
                 post.isReview  = requestPost.isReview;
                 post.Rating    = requestPost.Rating;
                 db.SaveChanges();
                 UpdateProductRating(post.ProductId);
                 PhotosController.UploadPhotos(uploadedPhotos, post.PostId, false);
                 TempData["message"] = "Postarea a fost modificată.";
                 return(RedirectToAction("Show", "Products", new { id = post.ProductId }));
             }
             return(View(requestPost));
         }
         TempData["message"] = "Nu poți șterge postarea altcuiva!";
         return(RedirectToAction("Show", "Products", new { id = post.ProductId }));
     }
     catch (Exception e)
     {
         return(View(requestPost));
     }
 }
示例#2
0
        public ActionResult Edit(int id, Product requestProduct, HttpPostedFileBase[] uploadedPhotos)
        {
            requestProduct.Categ = GetAllCategories();
            try
            {
                if (ModelState.IsValid)
                {
                    Product product = db.Products.Find(id);
                    if (User.IsInRole("Collaborator") &&
                        product.UserId != User.Identity.GetUserId())
                    {
                        TempData["message"] = "Acces interzis";
                        return(RedirectToAction("Index"));
                    }
                    if (TryUpdateModel(product))
                    {
                        product.ProductName = requestProduct.ProductName;
                        product.Description = requestProduct.Description;
                        product.CategoryId  = requestProduct.CategoryId;
                        product.Category    = db.Categories.Find(product.CategoryId);

                        db.SaveChanges();
                        PhotosController.UploadPhotos(uploadedPhotos, product.ProductId, true);
                        TempData["message"] = "Produsul a fost modificat";
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        return(View(requestProduct));
                    }
                }
                else
                {
                    return(View(requestProduct));
                }
            }
            catch (Exception e)
            {
                return(View());
            }
        }
示例#3
0
        public ActionResult New(Product product, HttpPostedFileBase[] uploadedPhotos)
        {
            product.Categ  = GetAllCategories();
            product.UserId = User.Identity.GetUserId();
            product.User   = db.Users.Find(product.UserId);
            try
            {
                if (ModelState.IsValid)
                {
                    product.Rating   = 0;
                    product.Accepted = false;
                    product.Category = db.Categories.Find(product.CategoryId);
                    db.Products.Add(product);
                    db.SaveChanges();
                    PhotosController.UploadPhotos(uploadedPhotos, product.ProductId, true);

                    TempData["message"] = "Produsul este în curs de verificare de către administratori.";

                    return(RedirectToAction("Index"));
                }
                else
                {
                    product.Categ       = GetAllCategories();
                    product.Price       = 1;
                    TempData["message"] = "Produsul nu a fost adaugat!";
                    return(RedirectToAction("New"));
                }
            }
            catch (Exception e)
            {
                product.Categ       = GetAllCategories();
                product.Price       = 2;
                TempData["massage"] = "Produsul nu a fost adaugat!";
                return(RedirectToAction("New"));
            }
        }
示例#4
0
        public ActionResult New(Post post, HttpPostedFileBase[] uploadedPhotos)
        {
            post.Date   = DateTime.Now;
            post.UserId = User.Identity.GetUserId();

            try
            {
                if ((post.isReview &&
                     !db.Posts.Where(p => p.UserId == post.UserId && p.ProductId == post.ProductId && p.isReview).Any()) ||
                    !post.isReview)
                {
                    if (ModelState.IsValid)
                    {
                        db.Posts.Add(post);
                        db.SaveChanges();
                        UpdateProductRating(post.ProductId);
                        PhotosController.UploadPhotos(uploadedPhotos, post.PostId, false);
                        TempData["message"] = "Mesajul a fost postat.";
                    }
                    else
                    {
                        TempData["message"] = "Continutul este obligatoriu.";
                    }
                }
                else
                {
                    TempData["message"] = "Nu puteti lasa mai mult de o recenzie.";
                }
                return(RedirectToAction("Show", "Products", new { id = post.ProductId }));
            }
            catch (Exception e)
            {
                TempData["message"] = "Postarea nu a fost adăugată.";
                return(RedirectToAction("Show", "Products", new { id = post.ProductId }));
            }
        }