Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Description,CreatedDate")] TheList theList)
        {
            if (id != theList.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(theList);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TheListExists(theList.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(theList));
        }
 public IResult <ShoppingList> ChangeItemName(int listId, User user, int itemId, string itemName)
 {
     using var context = new ListContext();
     try
     {
         var list = context?.ShoppingList?.Include(list => list.ListItems).FirstOrDefault(list => list.Id == listId && list.UserId == user.Id);
         if (list != null)
         {
             var item = list.ListItems.FirstOrDefault(xItem => xItem.Id == itemId);
             if (item == null)
             {
                 return(new Result <ShoppingList>("No Item with this Id was found", ResultType.Error));
             }
             item.Itemname = itemName;
             context.Update(item);
             context.SaveChanges();
             return(new Result <ShoppingList>($"itemname was changed to {itemName}", ResultType.Success, list));
         }
         return(new Result <ShoppingList>("No List for this Id and User was found", ResultType.Error));
     }
     catch (Exception ex)
     {
         return(new Result <ShoppingList>($"An Error in renaming the item occured: \n {ex.Message}", ResultType.Error));
     }
 }
 public IResult <ShoppingList> ChangeItemIsChecked(int listId, User user, int itemId, bool isChecked)
 {
     using var context = new ListContext();
     try
     {
         var list = context?.ShoppingList?.Include(list => list.ListItems).FirstOrDefault(list => list.Id == listId && list.UserId == user.Id);
         if (list != null)
         {
             var item = list.ListItems.FirstOrDefault(xItem => xItem.Id == itemId);
             if (item == null)
             {
                 return(new Result <ShoppingList>("No Item with this Id was found", ResultType.Error));
             }
             item.IsChecked = isChecked;
             context.Update(item);
             context.SaveChanges();
             return(new Result <ShoppingList>($"item checked state was set to {isChecked}", ResultType.Success, list));
         }
         return(new Result <ShoppingList>("No List for this Id and User was found", ResultType.Error));
     }
     catch (Exception ex)
     {
         return(new Result <ShoppingList>($"An Error in checking the state to {isChecked} occured: \n {ex.Message}", ResultType.Error));
     }
 }
 public IResult <ShoppingList> DeleteItemFromList(int listId, User user, int itemId)
 {
     using var context = new ListContext();
     try
     {
         var list = context?.ShoppingList?.Include(list => list.ListItems).FirstOrDefault(list => list.Id == listId && list.UserId == user.Id);
         if (list != null)
         {
             var item = list.ListItems.FirstOrDefault(item => item.Id == itemId);
             if (item != null)
             {
                 list.ListItems.Remove(item);
                 context.Update(list);
                 context.SaveChanges();
                 return(new Result <ShoppingList>("item deleted", ResultType.Success, list));
             }
             return(new Result <ShoppingList>("No Item with this Id on the List found", ResultType.Error));
         }
         return(new Result <ShoppingList>("No List for this Id and User was found", ResultType.Error));
     }
     catch (Exception ex)
     {
         return(new Result <ShoppingList>($"An Error in Deleting the Item occured: \n {ex.Message}", ResultType.Error));
     }
 }
 public IResult <ShoppingList> ChangeListIsFavourite(int listId, User user, bool isFavourite)
 {
     using var context = new ListContext();
     try
     {
         var list = context?.ShoppingList?.Include(list => list.ListItems).FirstOrDefault(list => list.Id == listId && list.UserId == user.Id);
         if (list != null)
         {
             list.IsFavourite = isFavourite;
             context.Update(list);
             context.SaveChanges();
             return(new Result <ShoppingList>($"list isFavourite state was set to {isFavourite}", ResultType.Success, list));
         }
         return(new Result <ShoppingList>("No List for this Id and User was found", ResultType.Error));
     }
     catch (Exception ex)
     {
         return(new Result <ShoppingList>($"An Error in checking the state to {isFavourite} occured: \n {ex.Message}", ResultType.Error));
     }
 }
 public IResult <ShoppingList> RenameList(int listId, User user, string listName)
 {
     using var context = new ListContext();
     try
     {
         var list = context?.ShoppingList?.Include(list => list.ListItems).FirstOrDefault(list => list.Id == listId && list.UserId == user.Id);
         if (list != null)
         {
             list.Listname = listName;
             context.Update(list);
             context.SaveChanges();
             return(new Result <ShoppingList>($"item Renamed to {listName}", ResultType.Success, list));
         }
         return(new Result <ShoppingList>("No List for this Id and User was found", ResultType.Error));
     }
     catch (Exception ex)
     {
         return(new Result <ShoppingList>($"An Error in Renaming the List occured: \n {ex.Message}", ResultType.Error));
     }
 }