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)); } }
public async Task <IActionResult> Create(Guid id, int amount) { bool success = false; if (Guid.Empty == id) { throw new ArgumentNullException(); } BrewModel brew = await brewService.GetByIdAsync(id, true); if (brew == null) { throw new ArgumentNullException(); } ResourceModel model = new ResourceModel(); if (amount > 0) { foreach (var recipe in brew.Recipes) { ResourceModel resource = await resourceService.GetByIdAsync(recipe.ResourceId); resource.AmountInStock -= (amount * (int)recipe.Amount); if (model != null) { model = resourceService.UpdateResourceAmount(resource.ResourceId, resource.AmountInStock); } } if (model != null) { DateTime today = DateTime.Today; ProducedModel producedModel = new ProducedModel() { Amount = amount, BrewId = brew.BrewId, CreateDate = today.Date, ExprireDate = today.AddMonths(6) }; ProducedModel produced = await producedGoodsService.InsertAsync(producedModel); success = produced != null; } else { throw new ArgumentNullException(nameof(model)); } } ProducedModel[] producedModels = producedGoodsService.GetProducedGoods().ToArray(); return(Json(new { success = success, producedGoods = producedModels })); }
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 Update(BrewModel brew) { if (brew != null) { brew = brewRepository.Update(brew); brewRepository.SaveAsync(); } return(brew); }
public async Task <BrewModel> GetByIdAsync(Guid id, bool getRelated) { BrewModel model = null; if (id != Guid.Empty) { model = getRelated ? await brewRepository.GetBrewAndRelatedEntitiesAsync(id) : await brewRepository.GetByIdAsync(id); } return(model); }
public async Task AddBrewAndRecipesAsync(BrewModel brew) { BrewModel newBrew = await brewRepository.AddAsync(brew); ICollection <RecipeModel> newRecipes = brew.Recipes.Select(x => new RecipeModel() { BrewId = newBrew.BrewId, Name = x.ResourceName, Description = x.Description, Amount = x.Amount }).ToList(); recipeRepository.InsertRangeAsync(newRecipes); }
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); }