Esempio n. 1
0
        public async Task <IActionResult> Create(MealIngredientViewModel mealIngredientViewModel)
        {
            var validator = new MealIngredientValidator();
            var result    = validator.Validate(mealIngredientViewModel.MealIngredient);
            List <MealIngredient> mealIngredientsCheck = new List <MealIngredient>(_context.MealIngredients
                                                                                   .Include(x => x.Ingredient)
                                                                                   .Where(x => x.MealId == mealIngredientViewModel.MealIngredient.MealId));

            if (mealIngredientsCheck.Where(x => x.Ingredient.IngredientId == mealIngredientViewModel.MealIngredient.IngredientId).Count() > 0)
            {
                ModelState.AddModelError(string.Empty, "The meal already contains the meal ingredient");
            }
            if (result.IsValid && ModelState.IsValid)
            {
                _context.Add(mealIngredientViewModel.MealIngredient);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Create", mealIngredientViewModel.MealId));
            }
            else
            {
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, item.ErrorMessage);
                }
            }
            mealIngredientViewModel.MealIngredients = new List <MealIngredient>(_context.MealIngredients.Where(x => x.MealId == mealIngredientViewModel.MealIngredient.MealId).Include(x => x.Meal));
            mealIngredientViewModel.Ingredients     = new SelectList(_context.Ingredients, "IngredientId", "Name", mealIngredientViewModel.MealIngredient.IngredientId);
            mealIngredientViewModel.Units           = new SelectList(_context.Units, "UnitId", "Name", mealIngredientViewModel.MealIngredient.UnitId);
            return(View(mealIngredientViewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([Bind("EndUserId,Name,LastName,UserName,Email,PasswordHash,Avatar,ApplicationUserId")] EndUser endUser)
        {
            if (ModelState.IsValid)
            {
                _context.Add(endUser);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(endUser));
        }
Esempio n. 3
0
        public async Task <IActionResult> Create([Bind("UnitId,Name")] Unit unit)
        {
            if (ModelState.IsValid)
            {
                _context.Add(unit);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(unit));
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("LevelPreperationId,Level")] LevelPreperation levelPreperation)
        {
            if (ModelState.IsValid)
            {
                _context.Add(levelPreperation);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(levelPreperation));
        }
        public async Task <IActionResult> Create([Bind("PreperationTimeId,RangeMinutes")] PreperationTime preperationTime)
        {
            if (ModelState.IsValid)
            {
                _context.Add(preperationTime);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(preperationTime));
        }
Esempio n. 6
0
        public async Task <IActionResult> Create(IngredientViewModel createIngredientViewModel)
        {
            var validator = new IngredientValidator(_context.Ingredients);
            var result    = validator.Validate(createIngredientViewModel.Ingredient);

            if (result.IsValid)
            {
                _context.Add(createIngredientViewModel.Ingredient);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, item.ErrorMessage);
                }
            }
            return(View(createIngredientViewModel));
        }
Esempio n. 7
0
        public async Task <IActionResult> Create(CategoryViewModel categoryViewModel)
        {
            var validator = new CategoryValidator(_context.Categories);
            var result    = validator.Validate(categoryViewModel.Category);

            if (result.IsValid)
            {
                _context.Add(categoryViewModel.Category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                foreach (var item in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, item.ErrorMessage);
                }
            }
            categoryViewModel.Parents = new SelectList(_context.Categories.Where(x => x.ParentId == null), "CategoryId", "Name");
            return(View(categoryViewModel));
        }
Esempio n. 8
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (Input.IsChecked == false)
            {
                ModelState.AddModelError(string.Empty, "You must accept the Terms of agreement and the Privacypolicy");
            }
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName           = Input.Email,
                    Email              = Input.Email,
                    EmailConfirmed     = true,
                    NormalizedEmail    = Input.Email.ToUpper(),
                    NormalizedUserName = Input.NickName.ToUpper(),
                    EndUser            = new EndUser
                    {
                        LastName = Input.LastName,
                        Name     = Input.Name,
                        NickName = Input.NickName
                    }
                };

                var result = await _userManager.CreateAsync(user, Input.Password);

                user.EndUser.ApplicationUserId = user.Id;
                await _context.SaveChangesAsync();

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Esempio n. 9
0
 public async Task Save()
 {
     await _context.SaveChangesAsync();
 }
Esempio n. 10
0
        public async Task <IActionResult> Create(CreateMealViewModel model)
        {
            var     userId  = _userManager.GetUserId(User);
            EndUser endUser = await _context.EndUsers.FirstOrDefaultAsync(x => x.ApplicationUserId == userId);

            string uploadedFile = UploadedFile(model.MealImage);

            if (string.IsNullOrEmpty(uploadedFile))
            {
                ModelState.AddModelError(string.Empty, "Image is not selected");
            }
            if (endUser != null && ModelState.IsValid)
            {
                model.Meal.EndUserId   = endUser.EndUserId;
                model.Meal.PictureMeal = uploadedFile;
                var validator = new MealValidator(_context.Meals);
                var result    = validator.Validate(model.Meal);
                if (result.IsValid)
                {
                    if (model.SelectedCategories != null)
                    {
                        List <MealCategory> newCategories = new List <MealCategory>();
                        foreach (int CategoryId in model.SelectedCategories)
                        {
                            newCategories.Add(new MealCategory
                            {
                                MealId     = model.Meal.MealId,
                                CategoryId = CategoryId
                            });
                        }
                        _context.Add(model.Meal);
                        await _context.SaveChangesAsync();

                        Meal meal = await _context.Meals
                                    .Include(mc => mc.MealCategories)
                                    .SingleOrDefaultAsync(meal => meal.MealId == model.Meal.MealId);

                        meal.MealCategories.AddRange(newCategories);
                        _context.Update(meal);
                        await _context.SaveChangesAsync();

                        return(RedirectToAction(nameof(Index)));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Select at least one category!");
                    }
                }
                else
                {
                    foreach (var item in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, item.ErrorMessage);
                    }
                }
            }
            model.LevelPreperations  = new SelectList(_context.LevelPreperations, "LevelPreperationId", "Level", model.Meal.LevelPreperationId);
            model.PreperationTimes   = new SelectList(_context.PreperationTimes, "PreperationTimeId", "RangeMinutes", model.Meal.PreperationTimeId);
            model.Category           = new SelectList(_context.Categories, "CategoryId", "Name");
            model.SelectedCategories = new List <int>();
            return(View(model));
        }