Пример #1
0
        public IActionResult Edit(WishFormModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Categories =
                    new SelectList(
                        _service.GetAll(_user.Id)
                        .OrderBy(z => z.ItemType)
                        .GroupBy(x => new { x.ItemType, x.Category })
                        .Select(y => y.First()), "Category", "Category", string.Empty, "ItemType");
                //, string.Empty, string.Empty,

                return(View(model));
            }
            var existingWishes = _service.GetAll(_user.Id);

            if (existingWishes.Any(x => x.ID != model.Wish.ID && x.Title == model.Wish.Title && x.ItemType == model.Wish.ItemType))
            {
                ShowStatusMessage(MessageTypeEnum.error,
                                  $"An wish of Title: {model.Wish.Title} and Type: {model.Wish.ItemType.ToString()} already exists.",
                                  "Duplicate Record");
                model.Categories =
                    new SelectList(
                        _service.GetAll(_user.Id)
                        .OrderBy(z => z.ItemType)
                        .GroupBy(x => new { x.ItemType, x.Category })
                        .Select(y => y.First()), "Category", "Category", string.Empty, "ItemType");
                return(View(model));
            }

            //TODO: make sure user id is the same so as not to change other users data
            if (model.Wish.UserID != _user.Id)
            {
                ShowStatusMessage(MessageTypeEnum.warning, "This wish cannot be edited by another user.", "Edit Failure");
                return(RedirectToAction("Index", "Wish"));
            }

            model.Wish.DateModified = DateTime.UtcNow;
            _service.Edit(model.Wish);

            ShowStatusMessage(MessageTypeEnum.success, "Wish updated.", "Update Successful");
            return(RedirectToAction("Index", "Wish"));
        }