Esempio n. 1
0
        public async Task <IActionResult> ChangeMeal(Guid id, MealCreate updMeal)
        {
            if (!ModelState.IsValid)           //Validation mechanism invoke
            {
                return(BadRequest(ModelState));
            }

            try
            {
                Meal forUpdateMeal = new Meal()
                {
                    Id             = id,
                    Name           = updMeal.Name,
                    Picture        = updMeal.Picture,
                    Amount         = updMeal.Amount,
                    Price          = updMeal.Price,
                    MealCategoryId = updMeal.MealCategoryId,
                };
                await _mealRepository.UpdateRec(forUpdateMeal.Id, forUpdateMeal);

                return(NoContent());
            }
            catch
            {
                return(BadRequest("Some problems occcured during update action!"));
            }
        }
Esempio n. 2
0
        //Create meal
        public bool CreateMeal(MealCreate model)
        {
            //Error handling
            using (var ctx = new ApplicationDbContext())
            {
                var mealPlanIds = ctx.MealPlans.Where(m => m.OwnerId == _userId).Select(m => m.MealPlanId);
                //var ownerIds = ctx.MealPlans.Where(m => m.OwnerId == _userId).Select(o => o.OwnerId);

                if (mealPlanIds.Contains(model.MealPlanId))
                {
                    var entity =
                        new Meal()
                    {
                        Title      = model.Title,
                        OwnerId    = _userId,
                        MealPlanId = model.MealPlanId
                    };

                    ctx.Meals.Add(entity);

                    return(ctx.SaveChanges() == 1);
                }
                else
                {
                    return(false);
                }
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> CreateNewMeal(MealCreate newMeal)
        {
            if (!ModelState.IsValid)           //Validation mechanism invoke
            {
                return(BadRequest(ModelState));
            }

            try
            {
                Meal mObj = new Meal()
                {
                    Id             = Guid.NewGuid(),
                    Name           = newMeal.Name,
                    Picture        = newMeal.Picture,
                    Amount         = newMeal.Amount,
                    Price          = newMeal.Price,
                    MealCategoryId = newMeal.MealCategoryId,
                };
                await _mealRepository.CreateRec(mObj);

                return(StatusCode(201));
            }
            catch
            {
                return(BadRequest("Some problems occured during Meals creation action!"));
            }
        }
Esempio n. 4
0
        public bool CreateMeal(MealCreate model)
        {
            var entity =
                new Meal()
            {
                UserId        = _userId,
                MealName      = model.MealName,
                Category      = model.Category,
                ListOfFoodIds = model.ListOfFoodIds,
                CreatedUtc    = DateTimeOffset.Now.Date
            };

            using (var ctx = new ApplicationDbContext())
            {
                foreach (int i in entity.ListOfFoodIds)
                {
                    var foodMealEntity =
                        new FoodMeal()
                    {
                        MealId = entity.MealId,
                        FoodId = i
                    };
                    ctx.FoodMeals.Add(foodMealEntity);
                    entity.ListOfFoods.Add(ctx.FoodItems.Find(i));
                }
                ctx.DailyMeals.Add(entity);
                return(ctx.SaveChanges() > 0);
            }
        }
Esempio n. 5
0
        public ActionResult Create(MealCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateMealService();

            service.CreateMeal(model);

            return(RedirectToAction("Index"));
        }
Esempio n. 6
0
        public IHttpActionResult Post(MealCreate meal)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var service = CreateMealService();

            if (!service.CreateMeal(meal))
            {
                return(InternalServerError());
            }

            return(Ok());
        }
Esempio n. 7
0
        public bool CreateMeal(MealCreate model)
        {
            var entity =
                new Meal()
            {
                UserID          = _userId,
                MealName        = model.MealName,
                MealDescription = model.MealDescription,
                //  Recipes = model.Recipes,
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Meals.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }
        public bool CreateMeal(MealCreate model)
        {
            var entity =
                new Meal()
            {
                UserId     = _userId,
                Time       = model.Time,
                Food       = model.Food,
                Calories   = model.Calories,
                CreatedUtc = DateTimeOffset.Now
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Meals.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }
Esempio n. 9
0
        public ActionResult Create(MealCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateMealService();

            if (service.CreateMeal(model))
            {
                TempData["SaveResult"] = "Meal successfully created.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Unable to create meal.");
            return(View(model));
        }
Esempio n. 10
0
        public ActionResult Create(MealCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateMealService();

            if (service.CreateMeal(model))
            {
                TempData["SaveResult"] = "Your meal was successfully created";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", "Meal could not be created.");

            return(View(model));
        }
Esempio n. 11
0
        public ActionResult Create(MealCreate meal)
        {
            meal.MealID = 1;
            if (!ModelState.IsValid)
            {
                return(View(meal));
            }
            var service = CreateMealService();

            if (service.CreateMeal(meal))

            {
                TempData["SaveResult"] = "Woot! Meal Tracking is AWESOME!";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", "Super Lame! Not, you,..Well..No Meal was created. Don't Give up!");

            return(View(meal));
        }
Esempio n. 12
0
        public bool CreateMeal(MealCreate model)
        {
            var entity = new Meal()
            {
                OwnerId      = _userId,
                Name         = model.Name,
                MoodBefore   = model.MoodBefore,
                MoodAfter    = model.MoodAfter,
                HungerBefore = model.HungerBefore,
                HungerAfter  = model.HungerAfter,
                Location     = model.Location,
                WhoWith      = model.WhoWith,
                Notes        = model.Notes
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Meals.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }
Esempio n. 13
0
        protected void ValidateMealCreate(MealCreate mealCreate)
        {
            try
            {
                mealCreate.Name   = mealCreate.Name.Trim();
                mealCreate.NameAr = mealCreate.NameAr.Trim();
                ValidateNames(mealCreate.Name, mealCreate.NameAr);
                if (mealCreate.MealTypeId < 1)
                {
                    ThrowError("wrong meal type id", ErrorNumber.EmptyRequiredField);
                }

                if (mealCreate.MealImages == null || mealCreate.MealImages.Count < 1)
                {
                    ThrowError("empty images", ErrorNumber.EmptyRequiredField);
                }

                var hasDefault = false;
                foreach (var image in mealCreate.MealImages)
                {
                    if (image.IsDefualt)
                    {
                        hasDefault = true;
                        break;
                    }
                }
                if (!hasDefault)
                {
                    ThrowError("no default image is specified", ErrorNumber.EmptyRequiredField);
                }
            }
            catch (RestaurantException ex)
            {
                throw ex;
            }
        }