Exemple #1
0
        public ActionResult Create([Bind(Include = "ID,Year,Value,Mileage,Colour,Transmission,MakeName,ModelName")] Car car)
        {
            if (ModelState.IsValid)
            {
                db.Car.Add(car);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(car));
        }
 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> 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> 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));
     }
 }
Exemple #5
0
 public static void Initialize(ListContext context)
 {
     if (!context.Tasks.Any())
     {
         context.Tasks.AddRange(
             new TaskItem
         {
             Title       = "Hello0",
             Text        = "my first task0",
             ReleaseDate = DateTime.Now
         },
             new TaskItem
         {
             Title       = "Hello1",
             Text        = "my first task2",
             ReleaseDate = DateTime.Now
         },
             new TaskItem
         {
             Title       = "Hello2",
             Text        = "my first task2",
             ReleaseDate = DateTime.Now
         }
             );
         context.SaveChanges();
     }
 }
Exemple #6
0
        public IActionResult Create([FromBody] ListItem item)
        {
            if (item == null)
            {
                return(BadRequest());
            }

            var owner = ((ClaimsIdentity)User.Identity).FindFirst(ClaimTypes.NameIdentifier).Value;

            item.Owner = owner;

            _context.ListItems.Add(item);
            _context.SaveChanges();

            return(CreatedAtRoute("GetList", new { id = item.ListItemId }, item));
        }
Exemple #7
0
 public void OnPost(int groceriesId, string updatedText)
 {
     Groceries = db.Groceries.ToList();
     foreach (var item in Groceries)
     {
         if (item.Id == groceriesId)
         {
             item.text = updatedText;
             break;
         }
     }
     db.SaveChanges();
 }
 public IResult <object> AddToDb(User user)
 {
     using var context = new ListContext();
     try
     {
         context.Users.Add(user);
         context.SaveChanges();
         return(new Result($"User {user.Username} successfully added", ResultType.Success));
     }
     catch (Exception ex)
     {
         return(new Result($"The User could not be added because of an error: \n {ex.Message}", ResultType.Error));
     }
 }
Exemple #9
0
        public IEnumerable <ConnectionModel> GetConnectionList()
        {
            if (_context.ConnectionList.Count() == 0)
            {
                var conList = ReadFromFile();
                foreach (var connection in conList)
                {
                    _context.ConnectionList.Add(connection);
                }
            }
            _context.SaveChanges();

            return(_context.ConnectionList);
        }
        public HelloWorldController(ListContext context)
        {
            _context = context;

            if (_context.ListItems.Count() == 0)
            {
                // Create a new ListItem if collection is empty,
                // which means you can't delete all ListItems.
                _context.ListItems.Add(new ListItem {
                    Text = "Hello World!"
                });
                _context.SaveChanges();
            }
        }
 private IResult <ShoppingList> AddToDb(ShoppingList list)
 {
     using var context = new ListContext();
     try
     {
         var newEntry = context.ShoppingList.Add(list);
         context.SaveChanges();
         return(new Result <ShoppingList>($"List {list.Listname} successfully added", ResultType.Success, newEntry.Entity));
     }
     catch (Exception ex)
     {
         return(new Result <ShoppingList>($"The List could not be added because of an error: \n {ex.Message}", ResultType.Error));
     }
 }
Exemple #12
0
        public static void Initialize(ListContext context)
        {
            if (!context.Lists.Any())
            {
                context.Lists.AddRange(
                    new List
                {
                    Name        = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
                    Time_start  = DateTime.Now,
                    Time_end    = DateTime.Parse("25/10/2018"),
                    category_id = 0
                },
                    new List
                {
                    Name        = "Aliquam sagittis neque ac dui mollis, nec sagittis sem varius.",
                    Time_start  = DateTime.Now,
                    Time_end    = DateTime.Parse("25/10/2018"),
                    category_id = 0
                },
                    new List
                {
                    Name        = "Maecenas finibus nisi vel mi dignissim rhoncus.",
                    Time_start  = DateTime.Now,
                    Time_end    = DateTime.Parse("25/10/2018"),
                    category_id = 0
                }
                    );
                context.SaveChanges();
            }

            /*  if (!context.Categories.Any())
             * {
             *    context.Categories.AddRange(
             *        new Category
             *        {
             *            Name = "Lorem",
             *        },
             *        new Category
             *        {
             *            Name = "Aliquam",
             *        },
             *        new Category
             *        {
             *            Name = "Maecenas",
             *        }
             *    );
             *    context.SaveChanges();
             * }*/
        }
Exemple #13
0
        public void OnPost(string text)
        {
            if (text.Length > 50)
            {
                text = text.Substring(0, 50);
            }

            var item = new GroceriesItem {
                text = text
            };

            db.Groceries.Add(item);
            db.SaveChanges();
            Response.Redirect("List");
        }
Exemple #14
0
        public ActionResult Create(List list)
        {
            list.bid = TempData["bid"] as string;
            if (ModelState.IsValid)
            {
                list.listPos = db.Lists.Where <List>(l => l.bid == list.bid).Count() + 1;
                db.Lists.Add(list);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.bid = list.bid;
            return(View(list));
        }
 public IResult <object> DeleteList(int listId, User user)
 {
     using var context = new ListContext();
     try
     {
         var list = context?.ShoppingList?.FirstOrDefault(list => list.Id == listId && list.UserId == user.Id);
         if (list != null)
         {
             context.ShoppingList.Remove(list);
             context.SaveChanges();
             return(new Result("list deleted", ResultType.Success));
         }
         return(new Result("No List for this Id and User was found", ResultType.Error));
     }
     catch (Exception ex)
     {
         return(new Result($"An Error in Deleting the List 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> AddItemToList(int listId, ListItem item)
 {
     using var context = new ListContext();
     try
     {
         var list = context?.ShoppingList?.FirstOrDefault(list => list.Id == listId);
         if (list != null)
         {
             context.ListItem?.Add(item);
             context.SaveChanges();
             list = context?.ShoppingList?.Include(list => list.ListItems).FirstOrDefault(list => list.Id == listId);
             return(new Result <ShoppingList>("item added", ResultType.Success, list));
         }
         return(new Result <ShoppingList>("No List for this Id was found", ResultType.Error));
     }
     catch (Exception ex)
     {
         return(new Result <ShoppingList>($"An Error in getting the Lists for the user 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));
     }
 }
 public void AddRow(Bus bus)
 {
     listContext.List.Add(bus);
     listContext.SaveChanges();
 }