Пример #1
0
        public ActionResult Edit(int?id)
        {
            if (!id.HasValue)
            {
                return(RedirectToAction(nameof(ShapesController.Index)));
            }
            if (!ModelState.IsValid)
            {
                return(View());
            }


            var userId = User.Identity.GetUserId();
            var shape  = db.Shape.FirstOrDefault(
                p => p.Id == id && p.UserId == userId);

            if (shape == null)
            {
                return(RedirectToAction(nameof(ShapesController.Index)));
            }
            PopulateViewBag();
            var model = new CreateEditShapesViewModel();

            model.Category    = shape.Category;
            model.ShapeType   = shape.Type;
            model.Description = shape.Description;
            return(View(model));
        }
Пример #2
0
        public ActionResult SaveShapes(int?id, CreateEditShapesViewModel model)
        {
            if (!ModelState.IsValid)
            {
                PopulateViewBag();
                return(View());
            }
            var userId = User.Identity.GetUserId();

            if (db.Shape.Any(p => p.UserId == userId &&
                             p.Type == model.ShapeType &&
                             (!id.HasValue || p.Id != id.Value)))
            {
                ModelState.AddModelError(nameof(CreateEditShapesViewModel.ShapeType),
                                         "Shape should be very special");

                PopulateViewBag();
                return(View());
            }
            string fileExtension;

            if (model.Media != null)
            {
                fileExtension = Path.GetExtension(model.Media.FileName);

                if (!Constants.AllowedFileExtensions.Contains(fileExtension))
                {
                    ModelState.AddModelError("", "File extension is not allowed.");
                    PopulateViewBag();
                    return(View());
                }
            }
            Shapes shape;

            if (!id.HasValue)
            {
                shape        = new Shapes();
                shape.UserId = userId;
                db.Shape.Add(shape);
            }
            else
            {
                shape = db.Shape.FirstOrDefault(p => p.Id == id && p.UserId == userId);

                if (shape == null)
                {
                    return(RedirectToAction(nameof(ShapesController.Index)));
                }
            }
            shape.Type        = model.ShapeType;
            shape.Category    = model.Category;
            shape.Description = model.Description;
            if (model.Media != null)
            {
                if (!Directory.Exists(Constants.MappedUploadFolder))
                {
                    Directory.CreateDirectory(Constants.MappedUploadFolder);
                }

                var fileName         = model.Media.FileName;
                var fullPathWithName = Constants.MappedUploadFolder + fileName;

                model.Media.SaveAs(fullPathWithName);

                shape.MediaUrl = Constants.UploadFolder + fileName;
            }
            db.SaveChanges();
            return(RedirectToAction(nameof(ShapesController.Index)));
        }
Пример #3
0
 public ActionResult Create(CreateEditShapesViewModel model)
 {
     return(SaveShapes(null, model));
 }
Пример #4
0
 public ActionResult Edit(int id, CreateEditShapesViewModel model)
 {
     return(SaveShapes(id, model));
 }