public IActionResult Create(WishFormModel model) { //TODO: need to do user checks if (ModelState.IsValid) { try { model.Wish.DateAdded = DateTime.UtcNow; this._service.Add(model.Wish); } catch (Exception e) { 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"); ShowStatusMessage(MessageTypeEnum.error, e.Message, "Duplicate Wish"); return(View(model)); } ShowStatusMessage(MessageTypeEnum.success, "New Wish Added Successfully.", "Add Successful"); return(RedirectToAction("Index", "Wish")); } 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)); }
public IActionResult Edit(int id) { ViewBag.Title = "Edit"; var wish = _service.GetByID(id, _user.Id); if (wish.UserID != _user.Id) { ShowStatusMessage(MessageTypeEnum.warning, "This wish cannot be edited by another user.", "Edit Failure"); return(RedirectToAction("Index", "Wish")); } var model = new WishFormModel { 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"), //_service.GetAll(_user.Id).Where(x => !string.IsNullOrWhiteSpace(x.Category)).Select(y => new SelectListItem //{ // Group = new SelectListGroup { Name = y.ItemType.ToString() }, // Text = y.Category, // Value = y.Category, // Selected = wish.Category == y.Category //}).OrderBy(z => z.Group.Name).ToList(), Wish = wish }; return(View(model)); }
public IActionResult Create() { var model = new WishFormModel { Wish = new Wish { UserID = _user.Id }, 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") }; ViewBag.Title = "Create"; return(View(model)); }
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")); }