Пример #1
0
        public async Task <IActionResult> CreateWinery(CreateWineViewModel newWine)
        {
            var user = await GetCurrentUserAsync();

            // If the user is already in the ModelState
            // Remove user from model state
            ModelState.Remove("wine.User");
            ModelState.Remove("newWine.Wine.User");

            // If model state is valid
            if (ModelState.IsValid)
            {
                //If a user enters a new winery name
                if (newWine.Winery.Name != null)
                {
                    //Add that winery to the database
                    _context.Add(newWine.Winery);

                    await _context.SaveChangesAsync();
                }
                // Redirect to details view with id of product made using new object
                return(RedirectToAction(nameof(Create)));
            }
            return(View(newWine));
        }
Пример #2
0
        public async Task <IActionResult> Create(CreateWineViewModel newWine)
        {
            var user = await GetCurrentUserAsync();

            // If the user is already in the ModelState
            // Remove user from model state
            ModelState.Remove("wine.User");
            ModelState.Remove("newWine.Wine.User");

            //Checks to see if the newly added wine is already in the database
            List <Wine> ExistingWine = await _context.Wine
                                       .Include(w => w.Variety)
                                       .Include(w => w.Winery)
                                       .Where(w => w.Name == newWine.Wine.Name && w.WineryId == newWine.Wine.WineryId && w.VarietyId == newWine.Wine.VarietyId && w.Year == newWine.Wine.Year)
                                       .Where(w => w.ApplicationUserId == user.Id)
                                       .ToListAsync();

            //If the wine already exists, this code allows the user to to update their current inventory with the amount of bottles they were going to add
            if (ExistingWine.Count > 0)
            {
                UpdateBottleCountViewModel viewModel = new UpdateBottleCountViewModel();

                viewModel.existingWine = ExistingWine;

                viewModel.newWine = newWine;

                return(View("WineExists", viewModel));
            }

            //If the wine doesn't exist, add it to the database
            if (ExistingWine.Count == 0)
            {
                // If model state is valid
                if (ModelState.IsValid)
                {
                    // Add the user back
                    newWine.Wine.ApplicationUserId = user.Id;

                    // Add the wine
                    _context.Add(newWine.Wine);

                    // Save changes to database
                    await _context.SaveChangesAsync();

                    // Redirect to details view with id of product made using new object
                    return(RedirectToAction(nameof(Details), new { id = newWine.Wine.WineId.ToString() }));
                }
            }

            return(View(newWine));
        }
Пример #3
0
        public async Task <IActionResult> Create(CreateWineViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                Wine wine = viewModel.Wine;
                _context.Add(wine);

                List <IdCheckBox> idCheckBoxes = viewModel.IdCheckBoxes;
                foreach (var idCheckBox in idCheckBoxes)
                {
                    if (idCheckBox.IsSelected)
                    {
                        _context.Add(new WineGrape()
                        {
                            ApplicationUserId = viewModel.Wine.ApplicationUserId,
                            WineId            = wine.Id,
                            GrapeId           = idCheckBox.Id
                        });
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            // Fill in the view model
            var grapes = _context.Grape.ToList();
            var createWineViewModel = new CreateWineViewModel();

            for (int i = 0; i < grapes.Count(); i++)
            {
                // Fill out the check boxes to select the grapes
                createWineViewModel.IdCheckBoxes.Add(new IdCheckBox()
                {
                    Id = grapes[i].Id, Name = grapes[i].Name, IsSelected = false
                });
            }

            ViewBag.Appellations = new SelectList(_context.Appellation
                                                  .Where(w => w.ApplicationUserId == viewModel.Wine.ApplicationUserId), "Id", "Name", viewModel.Wine.AppellationId);
            ViewBag.Regions = new SelectList(_context.Region
                                             .Where(w => w.ApplicationUserId == viewModel.Wine.ApplicationUserId), "Id", "Name", viewModel.Wine.RegionId);
            ViewBag.Drynesses = new SelectList(_context.Dryness
                                               .Where(w => w.ApplicationUserId == viewModel.Wine.ApplicationUserId), "Id", "Name", viewModel.Wine.DrynessId);
            ViewBag.Categories = new SelectList(_context.Category
                                                .Where(w => w.ApplicationUserId == viewModel.Wine.ApplicationUserId), "Id", "Name", viewModel.Wine.CategoryId);
            return(View(viewModel));
        }
Пример #4
0
        // GET: Wines/Create
        public async Task <IActionResult> Create()
        {
            // Lists to choose
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            ViewBag.Appellations = new SelectList(_context.Appellation
                                                  .Where(w => w.ApplicationUserId == userId), "Id", "Name");
            ViewBag.Regions = new SelectList(_context.Region
                                             .Where(w => w.ApplicationUserId == userId), "Id", "Name");
            ViewBag.Drynesses = new SelectList(_context.Dryness
                                               .Where(w => w.ApplicationUserId == userId), "Id", "Name");
            ViewBag.Categories = new SelectList(_context.Category
                                                .Where(w => w.ApplicationUserId == userId), "Id", "Name");

            // Fill in the view model
            var grapes = await _context.Grape
                         .Where(g => g.ApplicationUserId == userId)
                         .ToListAsync();

            var createWineViewModel = new CreateWineViewModel()
            {
                Wine = new Wine()
                {
                    ApplicationUserId = userId
                }
            };

            for (int i = 0; i < grapes.Count(); i++)
            {
                // Fill out the check boxes to select the grapes
                createWineViewModel.IdCheckBoxes.Add(new IdCheckBox()
                {
                    Id         = grapes[i].Id,
                    Name       = grapes[i].Name,
                    IsSelected = false
                });
            }
            return(View(createWineViewModel));
        }
Пример #5
0
        public async Task <IActionResult> Create()
        {
            //Gets current user and assigns them to a variable
            ApplicationUser user = await GetCurrentUserAsync();

            //Get all of the varieties from the database
            var AllVarieties = _context.Variety;

            //Get all of the wineries from the database
            var AllWineries = _context.Winery.OrderBy(w => w.Name);

            //Create a select list of varieties
            List <SelectListItem> Varieties = new List <SelectListItem>();

            //Create a select list of wineries
            List <SelectListItem> Wineries = new List <SelectListItem>();

            //Create a view model
            CreateWineViewModel viewModel = new CreateWineViewModel();

            viewModel.User = user;

            // Loop over all of the varieties from the database
            foreach (var v in AllVarieties)
            {
                // Create a new select list item of li
                SelectListItem li = new SelectListItem
                {
                    // Give a value to li
                    Value = v.VarietyId.ToString(),
                    // Provide text to li
                    Text = v.Name
                };
                // Add li to Varieties list
                Varieties.Add(li);
            }

            //Attach the select list items to view model
            viewModel.Varieties = Varieties;

            // Loop over all of the wineries from the database
            foreach (var w in AllWineries)
            {
                // Create a new select list item of li
                SelectListItem li = new SelectListItem
                {
                    // Give a value to li
                    Value = w.WineryId.ToString(),
                    // Provide text to li
                    Text = w.Name
                };
                // Add li to Varieties list
                Wineries.Add(li);
            }

            // Insert starting position into varieties list
            Wineries.Insert(0, new SelectListItem
            {
                Text  = "Select an existing winery...",
                Value = ""
            });

            // Insert starting position into varieties list
            Varieties.Insert(0, new SelectListItem
            {
                Text  = "Assign a variety...",
                Value = ""
            });

            //Insert Wine categories to make options easier to read
            Varieties.Insert(1, new SelectListItem
            {
                Text  = "--- Dry Whites ---",
                Value = ""
            });

            Varieties.Insert(7, new SelectListItem
            {
                Text  = "--- Sweet Whites ---",
                Value = ""
            });

            Varieties.Insert(13, new SelectListItem
            {
                Text  = "--- Rich Whites ---",
                Value = ""
            });

            Varieties.Insert(18, new SelectListItem
            {
                Text  = "--- Sparkling ---",
                Value = ""
            });

            Varieties.Insert(23, new SelectListItem
            {
                Text  = "--- Light Reds ---",
                Value = ""
            });

            Varieties.Insert(28, new SelectListItem
            {
                Text  = "--- Medium Reds ---",
                Value = ""
            });

            Varieties.Insert(35, new SelectListItem
            {
                Text  = "--- Bold Reds ---",
                Value = ""
            });

            Varieties.Insert(41, new SelectListItem
            {
                Text  = "--- Dessert ---",
                Value = ""
            });


            //Attach the select list items to view model
            viewModel.Wineries = Wineries;

            ViewData["User"] = new SelectList(_context.Users, "Id", "Id");

            //Pass view model to view
            return(View(viewModel));
        }