public ActionResult CreateComment(CommentsRecipeViewModel vm) { using (var context = new ModelsContext()) { try { Models.Comment comment = new Comment(); comment.Recipe_Name = vm.BeerName; comment.Timestamp = DateTime.Now; comment.UserProfile_UserID = WebSecurity.GetUserId(User.Identity.Name); comment.Text = vm.Text; comment.FlavorProfile_Name = vm.Flavor; context.Comments.Add(comment); context.SaveChanges(); } catch (Exception e) { return RedirectToAction("Show", new { name = vm.BeerName, tab = "comments" }); } } return RedirectToAction("Show", new { name = vm.BeerName, tab="comments" }); }
public SimpleMembershipInitializer() { Database.SetInitializer<ModelsContext>(null); try { using (var context = new ModelsContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
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 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 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}); } }