private ActionResult PartialMashStepDelete(DetailRecipeViewModel vm, int uid, bool isNewRecipe) { var removal = vm.Mash.Steps.FirstOrDefault(u => u.UID == uid); vm.Mash.Steps.Remove(removal); ModelState.Clear(); return isNewRecipe ? View("Create", vm) : View("Update", vm); }
private ActionResult PartialMashProfileDelete(DetailRecipeViewModel vm, bool isNewRecipe) { vm.Mash = null; ModelState.Clear(); return isNewRecipe ? View("Create",vm) : View("Update", vm); }
private ActionResult PartialMashStepAdd(DetailRecipeViewModel vm, bool isNewRecipe) { if ( vm.Mash != null) { vm.Mash.Steps.Add(new MashStepViewModel()); } return isNewRecipe ? View("Create", vm) : View("Update", vm); }
private ActionResult PartialHopAdd(DetailRecipeViewModel vm, ModelsContext context, bool isNewRecipe) { if (vm.HopToAdd != null) { var removedHop = vm.RemovedHops.FirstOrDefault(u => u == vm.HopToAdd.Name); if (removedHop != null) vm.RemovedHops.Remove(removedHop); var addition = context.Hops.Find(vm.HopToAdd.Name); if (addition != null) { vm.HopsUsed.Add(new HopViewModel() { Alpha = addition.Alpha, Beta = addition.Beta, Name = addition.Name, PercentCaryophyllene = addition.Caryophyllene, PercentHumulene = addition.Humulene, PercentMyrcene = addition.Myrcene, PercentCohumulone = addition.Cohumulone, Stability = addition.HSI, UsageTimeMin = vm.HopToAdd.UsageTimeMin, UsedDuring = vm.HopToAdd.UsedDuring, Form = vm.HopToAdd.Form, AmountKg = vm.HopToAdd.AmountKg, }); } } return isNewRecipe ? View("Create", vm) : View("Update", vm); }
private ActionResult PartialHopDelete(DetailRecipeViewModel vm, string name, bool isNewRecipe) { if (name != null) { var removal = vm.HopsUsed.FirstOrDefault(u => u.Name == name); vm.RemovedHops.Add(name); vm.HopsUsed.Remove(removal); } return isNewRecipe ? View("Create", vm) : View("Update", vm); }
public ActionResult UpdateRating(DetailRecipeViewModel vm) { using (var context = new Models.ModelsContext()) { int userID = WebSecurity.GetUserId(User.Identity.Name); var rating = context.Ratings.Where(x => x.Recipe_Name == vm.BeerName && x.UserProfile_UserID == userID).Select(x => x); if (rating.Count() == 0) { Models.Rating newRating = new Rating(); newRating.Recipe_Name = vm.BeerName; newRating.UserProfile_UserID = WebSecurity.GetUserId(User.Identity.Name); newRating.Rating_Score = vm.Rating; context.Ratings.Add(newRating); } else { rating.Single().Rating_Score = vm.Rating; } context.SaveChanges(); } return RedirectToAction("Show", new { name = vm.BeerName, tab = "details" }); }
private ActionResult PartialFermentableAdd(DetailRecipeViewModel vm, ModelsContext context, bool isNewRecipe) { if (vm.FermentableToAdd != null) { var removedFermentable = vm.RemovedFermentables.FirstOrDefault(u => u == vm.FermentableToAdd.Name); if (removedFermentable != null) vm.RemovedHops.Remove(removedFermentable); var addition = context.Fermentables.Find(vm.FermentableToAdd.Name); vm.FermentablesUsed.Add(new FermentableViewModel() { Name = addition.Name, CourseGrainYield = addition.Yield + addition.CoarseFineDiff, Color = addition.Color, DiastaticPower = addition.DiastaticPower, Yield = addition.Yield, IsAddedAfterBoiling = vm.FermentableToAdd.IsAddedAfterBoiling, Amount = vm.FermentableToAdd.Amount, }); } return isNewRecipe ? View("Create", vm) : View("Update", vm); }
public ActionResult Update(DetailRecipeViewModel vm, string submission = "Apply") { return HandleRecipeEditor(vm, false, submission); }
public ActionResult Create(DetailRecipeViewModel viewModel, string submission = "Apply") { return HandleRecipeEditor(viewModel, true, submission); }
public ActionResult HandleRecipeEditor(DetailRecipeViewModel vm, bool isNewRecipe, string submission = "Apply") { try { using (var context = new ModelsContext()) { Recipe recipeModel; recipeModel = !isNewRecipe ? context.Recipes.Find(vm.BeerName) : new Recipe() {Name = vm.BeerName, Date = DateTime.Now}; var styles = ((from s in context.Styles select new SelectListItem {Text = s.Name, Value = s.Name}).ToList()); var types = ((from s in context.RecipieTypes select new SelectListItem { Text = s.Name, Value = s.Name }).ToList()); vm.HopsUsed = vm.HopsUsed ?? GetHopsUsed(recipeModel); vm.FermentablesUsed = vm.FermentablesUsed ?? GetFermentablesUsed(recipeModel); vm.RemovedHops = vm.RemovedHops ?? new List<string>(); vm.RemovedFermentables = vm.RemovedFermentables ?? new List<string>(); vm.PostedDate = isNewRecipe ? DateTime.Now : recipeModel.Date; vm.Styles = styles; vm.RecipeTypes = types; var styleModel = context.Styles.Find(vm.Style); var typeModel = context.RecipieTypes.Find(vm.RecipeType); if (ModelState.IsValid && submission == "Apply") { recipeModel.Style_Name = styleModel.Name ?? context.Styles.FirstOrDefault().Name; recipeModel.RecipieType_Name = typeModel.Name ?? context.RecipieTypes.FirstOrDefault().Name; recipeModel.Carbonation = vm.Carbonation; recipeModel.FG = vm.FinalGravity; recipeModel.OG = vm.OriginalGravity; recipeModel.Date = vm.PostedDate; if (vm.File != null && vm.File.ContentLength > 0) { BinaryReader reader = new BinaryReader(vm.File.InputStream); vm.File.InputStream.Seek(0, SeekOrigin.Begin); var bytes = reader.ReadBytes((int) vm.File.InputStream.Length); recipeModel.Image = bytes; } //replace mash profile if (recipeModel.Mash != null) { List<MashStep> toRemove = recipeModel.Mash.Steps.ToList(); foreach (var step in toRemove) { context.MashSteps.Remove(step); } context.MashProfiles.Remove(recipeModel.Mash); } if (vm.Mash != null) { recipeModel.Mash = new MashProfile(); recipeModel.Mash.EquipAdjust = vm.Mash.EquipAdjust; recipeModel.Mash.Name = vm.Mash.Name; recipeModel.Mash.GrainTemp = vm.Mash.GrainTemp; recipeModel.Mash.PH = vm.Mash.PH; recipeModel.Mash.SpargeTemp = vm.Mash.SpargeTemp; recipeModel.Mash.TunSpecificHeat = vm.Mash.TunSpecificHeat; recipeModel.Mash.TunTemp = vm.Mash.TunTemp; recipeModel.Mash.Notes = vm.Mash.Notes; recipeModel.Mash.TunWeight = vm.Mash.TunWeight; vm.Mash.Steps = vm.Mash.Steps ?? new List<MashStepViewModel>(); for (int i = 0; i < vm.Mash.Steps.Count; i++) { var stepVm = vm.Mash.Steps[i]; var stepModel = new MashStep { Name = stepVm.Name, EndTemp = stepVm.EndTempCel, DecoctionAmount = stepVm.DecoctionAmount, InfuseAmount = stepVm.InfuseAmountLiters, InfuseTemp = stepVm.InfuseTempCel, MashStepType_Name = stepVm.Type, RampTime = (float) stepVm.RampTimeMin.TotalMinutes, SequenceNumber = stepVm.SequenceNumber, StepTemp = stepVm.StepTempCel, StepTime = (float) stepVm.StepTimeMin.TotalMinutes, }; recipeModel.Mash.Steps.Add(stepModel); } context.MashProfiles.Add(recipeModel.Mash); } if (isNewRecipe) { context.Recipes.Add(recipeModel); } //add new hops foreach (var hopVm in vm.HopsUsed) { var hopModel = context.Hops.Find(hopVm.Name); var recipeHop = context.RecipeHops.Find(recipeModel.Name, hopVm.Name); if (recipeHop == null) { RecipeHop rh = new RecipeHop(); rh.Recipe_Name = recipeModel.Name; rh.Hop = hopModel; rh.Amount = (float) hopVm.AmountKg; rh.Hop_Name = hopModel.Name; rh.HopUses_Name = hopVm.UsedDuring ?? context.HopUses.FirstOrDefault().Name; //must add to context, not the recipe context.RecipeHops.Add(rh); } } //add new fermentables foreach (var fermentableVm in vm.FermentablesUsed) { var fermentableModel = context.Fermentables.Find(fermentableVm.Name); var recipeFermentable = context.RecipeFermentables.Find(recipeModel.Name, fermentableVm.Name); if (recipeFermentable == null) { RecipeFermentable rf = new RecipeFermentable(); rf.Recipe_Name = recipeModel.Name; rf.Fermentable_Name = fermentableVm.Name; rf.Amount = fermentableVm.Amount; rf.AddAfterBoil = fermentableVm.IsAddedAfterBoiling; rf.IsMashed = fermentableVm.IsMashed; //must add to context, not the recipe context.RecipeFermentables.Add(rf); } } //remove hops foreach (var hop in vm.RemovedHops) { var toRemove = context.RecipeHops.Find(recipeModel.Name, hop); if (toRemove != null) context.RecipeHops.Remove(toRemove); } //remove fermentables foreach (var fermentable in vm.RemovedFermentables) { var toRemove = context.RecipeFermentables.Find(recipeModel.Name, fermentable); if (toRemove != null) context.RecipeFermentables.Remove(toRemove); } context.SaveChanges(); return RedirectToAction("Show", new {name = vm.BeerName}); } var errors = ModelState.Where(x => x.Value.Errors.Count > 0) .Select(x => new {x.Key, x.Value.Errors}) .ToArray(); if (submission.StartsWith("Delete Hop")) { return PartialHopDelete(vm, submission.Substring(11), isNewRecipe); } if (submission == "Add Hop") { return PartialHopAdd(vm, context, isNewRecipe); } if (submission.StartsWith("Delete Fermentable")) { return PartialFermentableDelete(vm, submission.Substring(19), isNewRecipe); } if (submission == "Add Fermentable") { return PartialFermentableAdd(vm, context, isNewRecipe); } if (submission == "Add Mash Step") { return PartialMashStepAdd(vm, isNewRecipe); } if (submission.StartsWith("Delete Mash Step")) { return PartialMashStepDelete(vm, int.Parse(submission.Substring(17)), isNewRecipe); } if (submission == "Delete Mash Profile") { return PartialMashProfileDelete(vm, isNewRecipe); } if (submission == "Add Mash Profile") { return PartialMashProfileAdd(vm, isNewRecipe); } return isNewRecipe ? View("Create", vm) : View("Update", vm); } } catch (Exception e) { return RedirectToAction("Update", new {name = vm.BeerName}); } }
public ActionResult Delete(DetailRecipeViewModel vm) { using (var context = new Models.ModelsContext()) { var removal = context.Recipes.Find(vm.BeerName); context.Recipes.Remove(removal); context.SaveChanges(); } return RedirectToAction("List"); }
public DetailRecipeViewModel CreateDetailRecipeViewModel(string name) { using (var context = new Models.ModelsContext()) { var siteAverages = Utilities.StatisticsUtils.GetSiteAvg(); var localAverages = Utilities.StatisticsUtils.GetLocalAvg(); var flavorCounts = from c in context.Comments where c.Recipe_Name == name group c by c.FlavorProfile.Name into g select new { Key = g.Key, Count = g.LongCount() }; var recipeComments = from c in context.Comments where c.Recipe_Name == name select c.Timestamp; var recipeModel = context.Recipes.Include("Style").Include("RecipieType").Where(r => r.Name == name).FirstOrDefault(); var styles = ((from s in context.Styles select new SelectListItem { Text = s.Name, Value = s.Name }).ToList()); var types = ((from s in context.RecipieTypes select new SelectListItem { Text = s.Name, Value = s.Name }).ToList()); var styleModel = recipeModel.Style; var typeModel = recipeModel.RecipieType; int ratingScore = 0; if (WebSecurity.IsAuthenticated) { int userID = WebSecurity.GetUserId(User.Identity.Name); var rating = context.Ratings.Where(x => x.Recipe_Name == name && x.UserProfile_UserID == userID).Select(x => x); ratingScore = rating.Count() != 0 ? rating.FirstOrDefault().Rating_Score : 0; } if (recipeModel == null) return null; double siteRating = 0; siteAverages.TryGetValue(name, out siteRating); double avgRating = 0; localAverages.TryGetValue(name, out avgRating); var styleString = styleModel == null ? context.Styles.FirstOrDefault().Name : styleModel.Name; var typeString = typeModel == null ? context.RecipieTypes.FirstOrDefault().Name : typeModel.Name; var recipeViewModel = new DetailRecipeViewModel { Rating = ratingScore, AvgRating = Math.Round(avgRating, 2), BeerName = name, Carbonation = recipeModel.Carbonation, Creators = recipeModel.Brewers.Select(b => b.UserName).ToList(), FinalGravity = recipeModel.FG, OriginalGravity = recipeModel.OG, PostedDate = recipeModel.Date, SiteRating = Math.Round(siteRating, 2), FlavorCounts = flavorCounts.ToDictionary(g => g.Key, g => g.Count), CommentsCount = recipeComments.Count(), FermentablesUsed = GetFermentablesUsed(recipeModel), RemovedFermentables = new List<string>(), RemovedHops = new List<string>(), HopsUsed = GetHopsUsed(recipeModel), HopToAdd = new HopViewModel(), FermentableToAdd = new FermentableViewModel(), Styles = styles, RecipeTypes = types, Style = styleString, RecipeType = typeString }; if (recipeModel.Mash != null) { MashProfileViewModel mash = new MashProfileViewModel { Name = recipeModel.Mash.Name, GrainTemp = recipeModel.Mash.GrainTemp, UID = recipeModel.Mash.UID, EquipAdjust = recipeModel.Mash.EquipAdjust, Notes = recipeModel.Mash.Notes, PH = recipeModel.Mash.PH, SpargeTemp = recipeModel.Mash.SpargeTemp, TunSpecificHeat = recipeModel.Mash.TunSpecificHeat, TunTemp = recipeModel.Mash.TunTemp, TunWeight = recipeModel.Mash.TunWeight, Steps = recipeModel.Mash.Steps.Select(s => new MashStepViewModel { UID = s.UID, DecoctionAmount = s.DecoctionAmount, EndTempCel = s.EndTemp, InfuseAmountLiters = s.InfuseAmount, SequenceNumber = s.SequenceNumber, StepTempCel = s.StepTemp, StepTimeMin = TimeSpan.FromMinutes(s.StepTime), RampTimeMin = TimeSpan.FromMinutes(s.RampTime), InfuseTempCel = s.InfuseTemp, Name = s.Name, Type = s.MashStepType_Name }).OrderBy(s => s.SequenceNumber).ToList() }; recipeViewModel.Mash = mash; } return recipeViewModel; } }