Example #1
0
        /// <summary>
        /// Create a dish by giving a new dish instance.
        /// </summary>
        /// <param name="newDish"> the new dish to be saved.</param>
        /// <returns>returns the current dish or null.</returns>
        public Dish CreateADish(Dish newDish)
        {
            var result  = _dishes.Collection.Insert(newDish);
            if (result.Ok)
                return newDish;

            return null;
        }
        public void CreateDishTest()
        {
            var dish = new Dish()
            {
                Availability = DateTime.Now,
                Description = "lalal",
                Dishtype = new DishType(){ Name = "entry" },
                Food = "lala",
                Name = "PASTY",
                Picture = "test.png",
                Price = 1.0,
                Seller = new NestedUser()
            };

            dishMock.Setup(action => action.CreateADish(dish)).Returns(dish);
        }
        public ActionResult New(DishVM newDish)
        {
            //Check if the model is valid ( newDish depending on property validations
            if (ModelState.IsValid)
            {
                //Get the current user
                var currentUser = UserContext.Current.CurrentUser;
                var user = UserSvc.GetUserById(currentUser.id);

                var dishType = DishTypeSvc.GetDishTypeById(newDish.SelectedDishType);

                //Create a new dish object instance.
                var dish = new Dish()
                {
                    Availability = DateTime.Now.AddMonths(1),
                    Description = newDish.Description,
                    Dishtype = dishType,
                    Food = newDish.Food,
                    Name = newDish.Name,
                    Price = newDish.Price,
                    Seller = new NestedUser()
                    {
                        _id = user._id,
                        Email = user.Email,
                        Username = user.Username
                    }
                };

                //Add the creation to the database
              var createdDish =  DishSvc.CreateADish(dish);

                //Check for picture file
              var filename = ImageSvc.SaveImage(newDish.Picture, createdDish._id.ToString(), Server.MapPath(ConfigurationManager.AppSettings["dishdirpicture"]));
               createdDish.Picture = filename;

                //Update the above dish with generated picture filename.
              DishSvc.UpdateDish(createdDish);

                // notification.
                UserContext.Current.Notify = " Dish Added !";
                UserContext.Current.NotifyType = notificationType.success.Value();

                return RedirectToAction("Index", "Account");

            }

            return View(newDish);
        }
        public ActionResult Edit(EditDishVM editDishViewModel)
        {
            try
            {
                var currentUser = UserContext.Current.CurrentUser;
                var currentDish = DishSvc.GetDishById(editDishViewModel.id);
                 var dishType = DishTypeSvc.GetDishTypeById(editDishViewModel.SelectedDishType);
                var user = UserSvc.GetUserById(currentUser.id);

                var editedDish = new Dish()
                {
                    _id = currentDish._id,
                    Availability = editDishViewModel.Availability,
                    Description = editDishViewModel.Description,
                    Dishtype = dishType,
                    Food = editDishViewModel.Food,
                    Name = editDishViewModel.Name,
                    Price = editDishViewModel.Price,
                    Seller = new NestedUser()
                    {
                        _id = user._id,
                        Email = user.Email,
                        Username = user.Username
                    },
                    Picture = currentDish.Picture
                };

                var filename = ImageSvc.UpdateImage(editDishViewModel.Picture, currentDish._id.ToString(), Server.MapPath(ConfigurationManager.AppSettings["dishdirpicture"]), currentDish.Picture);

                if(filename != null)
                    editedDish.Picture = filename;

                DishSvc.UpdateDish(editedDish);

                // TODO: Add update logic here

                return RedirectToAction("Index", "Account");
            }
            catch
            {
                return RedirectToAction("Index", "Account");
            }
        }