public async Task <IActionResult> Create(BrewsViewModel brew) { if (brew == null) { NotFound(); } string name = brew.Name; brew.IsNew = true; brew.IsValid = false; if (!ModelState.IsValid) { return(View(brew)); } if (!ValidateBrew(brew) || !await brewService.FindIfNameIsUniqueAsync(name)) { return(View(brew)); } else { BrewModel newBrew = mapper.BrewsViewModelToBrew(brew); newBrew.Recipes = brew.Recipes; brewService.AddBrewAndRecipesAsync(newBrew); brew.IsValid = true; return(RedirectToAction(RoutingConstants.BrewsManagementIndex)); } }
private async Task <BrewsViewModel> GetMappedBrewViewModel(Guid id) { var resources = await resourceService.GetAllAsync(); BrewModel brewModel = await brewService.GetByIdAsync(id, true); BrewsViewModel brew = mapper.BrewToBrewsViewModel(brewModel, false); foreach (var recipe in brewModel.Recipes) { brew.Recipes.Add(new RecipeModel() { Amount = recipe.Amount, Unit = recipe.Resource.Unit, IsNew = false, ResourceName = recipe.Resource.Name, Description = recipe.Description }); } brew.ResoucesSelect = resources.Select(x => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem() { Value = x.Unit, Text = x.Name }).ToList(); if (!brew.Recipes.Any()) { brew.Recipes.Add(new RecipeModel() { Unit = resources.FirstOrDefault().Unit, IsNew = true, ResourceName = resources.FirstOrDefault().Name }); } return(brew); }
public async Task <IActionResult> Edit(BrewsViewModel brew) { if (!ModelState.IsValid && !ValidateBrew(brew)) { brew.IsValid = false; return(View(brew)); } else { BrewModel updatedBrew = brewService.Update(mapper.BrewsViewModelToBrew(brew)); if (updatedBrew == null) { NotFound(); } else { await recipeService.AddOrUpdateAsync(brew.Recipes, brew.BrewId); } brew.IsValid = true; } if (brew.IsValid.Value) { return(RedirectToAction(RoutingConstants.BrewsManagementIndex)); } else { return(View(brew)); } }
public BrewModel BrewsViewModelToBrew(BrewsViewModel brew) { List <RecipeModel> recipes = brew.Recipes; return(new BrewModel() { BrewId = brew.BrewId, Name = brew.Name, Description = brew.Description, Link = brew.Link, Recipes = recipes != null?brew.Recipes.Select(x => new RecipeModel() { Amount = x.Amount, Description = x.Description, Name = x.ResourceName }).ToList() : new List <RecipeModel>() }); }
public async Task <IActionResult> Create() { var resources = await resourceService.GetAllAsync(); List <Microsoft.AspNetCore.Mvc.Rendering.SelectListItem> resourcesSelect = resources.Select(x => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem() { Value = x.Unit, Text = x.Name }).ToList(); var brew = new BrewsViewModel() { IsNew = true, ShowAlert = false, ResoucesSelect = resourcesSelect, }; brew.Recipes.Add(new RecipeModel() { IsNew = true }); return(View(brew)); }
public BrewsViewModel BrewToBrewsViewModel(BrewModel brew, bool includeRecipes) { BrewsViewModel viewModel = new BrewsViewModel(); viewModel.ShowAlert = false; viewModel.BrewId = brew.BrewId; viewModel.Name = brew.Name; viewModel.Description = brew.Description; viewModel.Link = brew.Link; if (includeRecipes && brew.Recipes.Any()) { viewModel.Recipes = new List <RecipeModel>(); viewModel.Recipes = brew.Recipes.Select(x => new RecipeModel() { Amount = x.Amount, Description = x.Description, Resource = x.Resource ?? new ResourceModel() }).ToList(); } return(viewModel); }
private bool ValidateBrew(BrewsViewModel brew) { return(brew != null && !string.IsNullOrEmpty(brew.Name) && brew.Name.Trim().Length < NumericConstants.BrewNameMaxLength && (!string.IsNullOrEmpty(brew.Description))); }