示例#1
0
        // GET: Items/Edit/5
        public ActionResult Edit(short?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Item item = db.Items.Find(id);

            if (item == null)
            {
                return(HttpNotFound());
            }

            short infoid = item.InfoId;
            Info  info   = db.Infos.Find(infoid);
            //make editVM
            ItemEditVM model = new ItemEditVM(item, info);

            List <short> selectedTags = db.InfoTags.Where(t => t.InfoId == infoid).Select(t => t.TagId).ToList();

            ViewBag.Selected = selectedTags;
            ViewBag.Tags     = db.Tags.ToList();

            return(View(model));
        }
        public ActionResult Delete(Guid listId, Guid itemId, ItemEditVM vm)
        {
            ShoppingList list = lists.Get(GetUser(), listId);

            if (list == null)
            {
                Message("Can't find list requested");
                return(RedirectToRoute(Names.ListIndex, Names.ListHash));
            }

            Item item = list.Items.FirstOrDefault(i => i.ID == itemId);

            if (item == null)
            {
                Message("Can't find item requested");
                return(RedirectToRoute(Names.ListDetails, new { listId = list.ID }, Names.ItemsHash));
            }
            string itemName = item.Name;

            lists.RemoveItem(list, item);

            Message("Deleted {0} from {1}", itemName, list.Name);
            return(RedirectToRoute(Names.ListDetails, new { listId = list.ID }, Names.ItemsHash));
        }
        public async Task <IActionResult> EditItem(Guid listId, Guid itemId, ItemEditVM vm)
        {
            ShoppingList list = lists.Get(GetUser(), listId);

            if (list == null)
            {
                Message("Can't find the list you asked for");
                return(RedirectToRoute(Names.ListIndex, Names.ListHash));
            }

            Item item = lists.GetItem(list, itemId);

            if (item == null)
            {
                Message("Can't find the item you asked for");
                return(RedirectToRoute(Names.ListDetails, new { listId = list.ID }, Names.ItemsHash));
            }

            string oldName = item.Name;
            await lists.ChangeItemName(list, item, vm.Name);

            Message("Changed {0} to {1}", oldName, vm.Name);
            return(RedirectToRoute(Names.ListDetails, new { listId = list.ID }, Names.ItemsHash));
        }
示例#4
0
        public ActionResult Edit([Bind(Include = "ItemId,InfoId,Name,Blurb,PictureFileName,DescriptionText,PropertyText,HistoryText, Artist, IsPublished, IsSecret")] ItemEditPostVM item,
                                 HttpPostedFileBase picture,
                                 List <short> tags, string submit)
        {
            #region Pre-model picture check
            if (picture != null)
            {
                string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" };
                string   imgName  = picture.FileName;

                var    length = picture.ContentLength;
                string ext    = imgName.Substring(imgName.LastIndexOf('.'));

                if (!goodExts.Contains(ext.ToLower()))
                {
                    ModelState.AddModelError("PictureFileName", "You have submitted a incorrect file type for your portrait. Please use either: .jpg, .jpeg, .gif, or .png");
                }

                if (item.Artist == null)
                {
                    ModelState.AddModelError("Artist", "Katherine, you're trying to submit something with a picture without an artist. That's a no-no! But seriously, if something came up that means you need to change this rule, you know who to call.");
                }
            }
            else if (item.PictureFileName != "default.jpg" && item.Artist == null)
            {
                ModelState.AddModelError("Artist", "Yo bud, you tired? Seems you deleted the artist by accident. Why don't ya fix that?");
            }
            #endregion

            if (ModelState.IsValid)
            {
                #region Save or Publish?
                switch (submit)
                {
                case "Save Progress":
                case "Un-Publish":
                    item.IsPublished = false;
                    break;

                case "Publish":
                case "Save":
                    item.IsPublished = true;
                    break;
                }
                #endregion

                var infoid = item.InfoId;
                #region Info Update
                //Info info = db.Infos.Find(infoid);
                Info info = db.Infos.Where(i => i.InfoId == infoid).FirstOrDefault();
                info.Name        = item.Name;
                info.Blurb       = item.Blurb;
                info.IsPublished = item.IsPublished;
                info.IsSecret    = item.IsSecret;
                #endregion

                #region Update tags
                List <short> currentTagIds = db.InfoTags.Where(x => x.InfoId == infoid).Select(x => x.TagId).ToList();

                if (tags != null)
                {
                    foreach (short tag in tags)
                    {
                        //if this is an already existing tag
                        if (currentTagIds.Contains(tag))
                        {
                            currentTagIds.Remove(tag);
                        }
                        //if this is a newly added tag
                        else
                        {
                            InfoTag newTag = new InfoTag {
                                InfoId = infoid, TagId = tag
                            };
                            db.InfoTags.Add(newTag);
                        }
                    }
                }

                if (currentTagIds.Count != 0)
                {
                    foreach (short id in currentTagIds)
                    {
                        InfoTag gone = db.InfoTags.Where(x => x.InfoId == infoid & x.TagId == id).FirstOrDefault();
                        db.InfoTags.Remove(gone);
                    }
                }

                #endregion


                #region Image update/upload
                string[] goodExts = { ".jpg", ".jpeg", ".gif", ".png" };

                #region Image Upload - picture
                if (picture != null)
                {
                    string imgName = picture.FileName;

                    string ext = imgName.Substring(imgName.LastIndexOf('.'));

                    if (goodExts.Contains(ext.ToLower()))
                    {
                        imgName = "item-" + infoid + ext;

                        picture.SaveAs(Server.MapPath("~/Content/img/item/" + imgName));
                    }
                    //remove old picture if it had a different extension (and thus would not be overridden)
                    string oldName = item.PictureFileName;
                    string oldExt  = oldName.Substring(oldName.LastIndexOf('.'));
                    if (oldName != "default.jpg" && oldExt != ext)
                    {
                        string fullPath = Request.MapPath("~/Content/img/item/" + oldName);
                        if (System.IO.File.Exists(fullPath))
                        {
                            System.IO.File.Delete(fullPath);
                        }
                    }
                    //assign new PictureFileName
                    item.PictureFileName = imgName;
                }
                #endregion
                #endregion

                #region Update Item
                Item daItem = new Item
                {
                    ItemId          = item.ItemId,
                    InfoId          = item.InfoId,
                    Name            = item.Name,
                    PictureFileName = item.PictureFileName,
                    DescriptionText = item.DescriptionText,
                    PropertyText    = item.PropertyText,
                    HistoryText     = item.HistoryText,
                    IsPublished     = item.IsPublished,
                    Artist          = item.Artist
                };
                db.Entry(daItem).State = EntityState.Modified;
                db.Entry(info).State   = EntityState.Modified;
                db.SaveChanges();
                #endregion

                return(RedirectToAction("Details", new { id = item.ItemId }));
            }

            ViewBag.Tags = db.Tags.ToList();
            if (tags != null)
            {
                ViewBag.Selected = tags;
            }
            else
            {
                ViewBag.Selected = new List <short>();
            }
            if (picture != null)
            {
                ModelState.AddModelError("PictureFileName", "Hey, there was some error, so you have to re-upload the picture");
            }
            ModelState.AddModelError("", "Something has gone wrong. Look for red text to see where is went wrong");
            ItemEditVM aItem = new ItemEditVM(item);
            return(View(aItem));
        }