Exemple #1
0
        public ActionResult CreateHop(HopViewModel vm)
        {
            using (var context = new Models.ModelsContext())
            {
                try
                {
                    Hop newHop = new Hop();

                    newHop.Name = vm.Name;
                    newHop.Alpha = (float) vm.Alpha;
                    newHop.Beta = (float) vm.Beta;
                    newHop.Caryophyllene = (float) vm.PercentCaryophyllene;
                    newHop.Cohumulone = (float) vm.PercentCohumulone;
                    newHop.HSI = (float) vm.Stability;
                    newHop.Humulene = (float) vm.PercentHumulene;

                    context.Hops.Add(newHop);
                    context.SaveChanges();
                    if (Request.IsAjaxRequest())
                    {
                        return PartialView("~/Views/Ingredients/DisplayTemplates/HopViewModel.cshtml", vm);
                    }
                    else
                    {
                        return RedirectToAction("Show", new {name = vm.Name, type = "hop"});
                    }
                }
                catch (Exception e)
                {
                    return PartialView("~/Views/Ingredients/EditorTemplates/HopViewModel.cshtml", vm);
                }
            }
        }
Exemple #2
0
 public ActionResult CreateFermentable(FermentableViewModel vm)
 {
     try
     {
         using (var context = new Models.ModelsContext())
         {
             Fermentable newFermentable = new Fermentable();
             newFermentable.CoarseFineDiff = (float) (vm.CourseGrainYield - vm.Yield);
             newFermentable.Color = vm.Color;
             newFermentable.DiastaticPower = (float) vm.DiastaticPower;
             newFermentable.Name = vm.Name;
             newFermentable.Yield = (float) vm.Yield;
             newFermentable.IBUs = (float) vm.IBU;
             context.Fermentables.Add(newFermentable);
             context.SaveChanges();
             if (Request.IsAjaxRequest())
             {
                 return PartialView("~/Views/Ingredients/DisplayTemplates/FermentableViewModel.cshtml", vm);
             }
             else
             {
                 return RedirectToAction("Show", new {name = vm.Name, type = "fermentable"});
             }
         }
     }
     catch (Exception e)
     {
         return PartialView("~/Views/Ingredients/EditorTemplates/HopViewModel.cshtml", vm);
     }
 }
Exemple #3
0
        public static Dictionary<string, double> GetLocalAvg()
        {
            using (var context = new Models.ModelsContext())
            {
                //retrieve table of all ratings scores for this recipe
                var vRatingScores = from r in context.Ratings
                                    group r by r.Recipe_Name into g
                                    select new { Name = g.Key, RatingSum = g.Sum(s => s.Rating_Score), Count = g.Count(), RatingAverage = g.Average(s => s.Rating_Score) };

                return vRatingScores.ToList().ToDictionary(g => g.Name, g => g.RatingAverage);
            }
        }
Exemple #4
0
        public ActionResult Create()
        {
            using (var context = new Models.ModelsContext())
            {
                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());

                return View(new DetailRecipeViewModel { IsNewRecipe = true, Styles = styles, RecipeTypes = types, Style = styles.FirstOrDefault().Value, RecipeType = types.FirstOrDefault().Value });
            }
        }
Exemple #5
0
        // GET: /BeerImage/
        public FileResult Index(string name)
        {
            using (var context = new Models.ModelsContext())
            {
                var recipe = context.Recipes.Find(name);
                if (recipe != null)
                {
                    if (recipe.Image != null)
                    {
                        return new FileContentResult(recipe.Image, "image/png");
                    }
                }
            }

            return new FilePathResult("/Images/no_beer_pic.png", "image/png");
        }
Exemple #6
0
        public static Dictionary<string, double> GetSiteAvg()
        {
            Dictionary<string, long> oFlavorCounts = new Dictionary<string, long>();
            using (var context = new Models.ModelsContext())
            {
                //retrieve table of all ratings scores for this recipe
                var vRatingScores = from r in context.Ratings
                                    group r by r.Recipe_Name into g
                                    select new { Name = g.Key, RatingSum = g.Sum(s => s.Rating_Score), Count = g.Count(), RatingAverage = g.Average(s=>s.Rating_Score) };

                double dRecipesAvgRatingsSum = vRatingScores.Sum(s => s.RatingAverage);

                double dRecipesAvgRating = dRecipesAvgRatingsSum / vRatingScores.Count();

                //determine an appropriate constant value for Bayesian average
                double dPropConstant = 10; //just use 10 for now, tweak if necessary

                //return Bayesian average using derived parameters
                return vRatingScores.ToList().ToDictionary(g => g.Name, g => GetBayesianAvg(dRecipesAvgRating, g.RatingSum, dPropConstant, vRatingScores.Count()));
            }
        }
Exemple #7
0
        public CommentsRecipeViewModel CreateCommentsRecipeViewModel(string name)
        {
            using (var context = new Models.ModelsContext())
            {
                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.Find(name);

                var siteAverages = Utilities.StatisticsUtils.GetSiteAvg();
                var localAverages = Utilities.StatisticsUtils.GetLocalAvg();

                double siteRating = 0;
                siteAverages.TryGetValue(name, out siteRating);

                double avgRating = 0;
                localAverages.TryGetValue(name, out avgRating);

                CommentsRecipeViewModel viewModel = new CommentsRecipeViewModel
                {
                    BeerName = recipeModel.Name,
                    AvgRating = Math.Round(avgRating, 2),
                    SiteRating = Math.Round(siteRating, 2),
                    FlavorCounts = flavorCounts.ToDictionary(g => g.Key, g => g.Count),
                    CommentsCount = recipeComments.Count()
                };

                viewModel.Comments = context.Comments.Where(c => c.Recipe_Name == name).Select(c => new CommentViewModel
                {
                    Flavor = c.FlavorProfile.Name,
                    Text = c.Text,
                    Poster = c.UserProfile.UserName
                }).ToList();

                viewModel.Flavors = context.FlavorProfiles.Select(c => c.Name).ToList();
                viewModel.Top3Flavors = GetTop3Flavors(viewModel.FlavorCounts);

                return viewModel;
            }
        }
Exemple #8
0
        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;
            }
        }
Exemple #9
0
        public ActionResult Show(string name, string type = "hop")
        {
            using(var context = new Models.ModelsContext())
            {
                if (type == "hop")
                {
                    var hopModel = context.Hops.Find(name);

                    if (hopModel == null) return HttpNotFound();

                    var hopViewModel = new HopViewModel
                        {
                            Alpha = hopModel.Alpha,
                            Beta = hopModel.Beta,
                            Name = hopModel.Name,
                            Type = hopModel.HopType_Name,
                            PercentCaryophyllene = hopModel.Caryophyllene,
                            PercentCohumulone = hopModel.Cohumulone,
                            PercentHumulene = hopModel.Humulene,
                            PercentMyrcene = hopModel.Myrcene,
                            Form = hopModel.HopForm_Name,
                            Stability = hopModel.HSI,
                        };

                    return View(hopViewModel);
                }
                else if (type == "fermentable")
                {
                    var fermentableModel = context.Fermentables.Find(name);

                    if (fermentableModel == null) return HttpNotFound();

                    var fermentableViewModel = new FermentableViewModel
                        {
                            Color = fermentableModel.Color,
                            DiastaticPower = fermentableModel.DiastaticPower,
                            CourseGrainYield = fermentableModel.Yield + fermentableModel.CoarseFineDiff,
                            Name = fermentableModel.Name,
                            Yield = fermentableModel.Yield,
                            IBU = fermentableModel.IBUs,
                        };

                    return View(fermentableViewModel);
                }
                else
                {
                    return HttpNotFound();
                }
            }
        }
Exemple #10
0
        public ActionResult ListJson(string type = "hop", string Term = "")
        {
            using (var context = new Models.ModelsContext())
            {
                if (type == "hop")
                {
                    var hopNames = from hop in context.Hops where hop.Name.StartsWith(Term) select hop.Name;
                    var hopArray = hopNames.ToArray();
                    return Json(hopArray, JsonRequestBehavior.AllowGet);
                }
                else if (type == "fermentable")
                {
                    var fermentableNames = from f in context.Fermentables where f.Name.StartsWith(Term) select f.Name;
                    return Json(fermentableNames.ToArray(), JsonRequestBehavior.AllowGet);
                }
            }

            return Json(new string[0], JsonRequestBehavior.AllowGet);
        }
Exemple #11
0
        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" });
        }
Exemple #12
0
        public ActionResult List(string sortby = "hoppin", int pageno = 0)
        {
            using (var context = new Models.ModelsContext())
            {
                var siteAverages = Utilities.StatisticsUtils.GetSiteAvg();
                var localAverages = Utilities.StatisticsUtils.GetLocalAvg();

                var random = new Random();
                IOrderedEnumerable<RecipeListItemViewModel> recipes;

                List<RecipeListItemViewModel> allRecipes = new List<RecipeListItemViewModel>();
                foreach (var r in context.Recipes.Include("Style").Include("Brewers").Include("RecipeFermentables").ToList())
                {
                    var vm = new RecipeListItemViewModel
                        {
                            Name = r.Name,
                            Style = r.Style == null ? "Unknown" : r.Style.StyleType_Name,
                            User = r.Brewers.Count == 0 ? "Unknown" : r.Brewers.FirstOrDefault().UserName,
                            OG = r.OG,
                            FG = r.FG,
                            Date = r.Date
                        };

                    double siteRating = 0;
                    siteAverages.TryGetValue(r.Name, out siteRating);

                    double avgRating = 0;
                    localAverages.TryGetValue(r.Name, out avgRating);

                    vm.SiteRating = Math.Round(siteRating, 2);
                    vm.AvgRating = Math.Round(avgRating, 2);
                    vm.Color = Math.Round(Utilities.BrewCharacteristicAlgs.CalculateColor(GetFermentablesUsed(r) ?? new List<FermentableViewModel>()),2);
                    allRecipes.Add(vm);
                }

                if (sortby == "hoppin")
                {
                    recipes = allRecipes.OrderByDescending(r => r.SiteRating);
                }
                else if (sortby == "fresh")
                {
                    recipes = allRecipes.OrderByDescending(r => r.Date);
                }
                else
                {
                    return HttpNotFound();
                }

                var recipePage = new PagedList<RecipeListItemViewModel>(recipes, pageno);

                var viewModel = new RecipeListViewModel
                    {
                        Beers = recipePage,
                        SortBy = sortby
                    };

                return View(viewModel);
            }
        }
Exemple #13
0
        public Models.Fermentable ParseFermentable(XmlTextReader reader, ref bool AddAfterBoil, ref bool IsMashed, ref float Amount)
        {
            Models.Fermentable fermentable = new Models.Fermentable();
            var currentNodeName = "";
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element: // The node is an element.
                        currentNodeName = reader.Name;
                        break;
                    case XmlNodeType.Text:
                         switch (currentNodeName)
                        {
                            case "NAME":
                                fermentable.Name = reader.Value;
                                break;
                            case "TYPE":
                                Models.FermentableType fermentableType = new Models.FermentableType() { Name = reader.Value};
                                using (var context = new Models.ModelsContext())
                                {
                                    if (context.FermentableTypes.Find(fermentableType.Name) == null)
                                    {
                                        context.FermentableTypes.Add(fermentableType);
                                        context.SaveChanges();
                                    }
                                }

                                fermentable.FermentableType_Name = reader.Value;
                                break;
                            case "AMOUNT":
                                Amount = float.Parse(reader.Value);
                                break;
                            case "YIELD":
                                fermentable.Yield = float.Parse(reader.Value);
                                break;
                            case "COLOR":
                                fermentable.Color = float.Parse(reader.Value);
                                break;
                            case "ADD_AFTER_BOIL":
                                AddAfterBoil = bool.Parse(reader.Value);
                                break;
                            case "ORIGIN":
                                fermentable.Origin = reader.Value;
                                break;
                            case "COARSE_FINE_DIFF":
                                fermentable.CoarseFineDiff = float.Parse(reader.Value);
                                break;
                            case "MOISTURE":
                                fermentable.Moisture = float.Parse(reader.Value);
                                break;
                            case "PROTEIN":
                                fermentable.Protein = float.Parse(reader.Value);
                                break;
                           case "IS_MASHED":
                                IsMashed = bool.Parse(reader.Value);
                                break;
                            case "IBU_GAL_PER_LB":
                                fermentable.IBUs = float.Parse(reader.Value);
                                break;
                        }
                        break;
                    case XmlNodeType.EndElement:
                        if (reader.Name == "FERMENTABLE")
                        {
                            return fermentable;
                        }
                        break;
                }
            }
            return fermentable;
        }
Exemple #14
0
 //
 // GET: /DB/
 public Models.Yeast ParseYeasts(XmlTextReader reader, ref bool AddToSecondary, ref float Amount, ref bool AmoutIsWeight)
 {
     Models.Yeast yeast = new Models.Yeast();
     var currentNodeName = "";
     while (reader.Read())
     {
         switch (reader.NodeType)
         {
             case XmlNodeType.Element: // The node is an element.
                 currentNodeName = reader.Name;
                 break;
             case XmlNodeType.Text:
                 switch (currentNodeName)
                 {
                     case "NAME":
                         yeast.Name = reader.Value;
                         break;
                     case "TYPE":
                         Models.YeastType yeastType = new Models.YeastType() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.YeastTypes.Find(yeastType.Name) == null)
                             {
                                 context.YeastTypes.Add(yeastType);
                                 context.SaveChanges();
                             }
                         }
                         yeast.YeastType_Name = reader.Value;
                         break;
                     case "FORM":
                         Models.YeastForm yeastForm = new Models.YeastForm() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.YeastForms.Find(yeastForm.Name) == null)
                             {
                                 context.YeastForms.Add(yeastForm);
                                 context.SaveChanges();
                             }
                         }
                         yeast.YeastForm_Name = reader.Value;
                         break;
                     case "AMOUNT":
                         Amount = float.Parse(reader.Value);
                         break;
                     case "AMOUNT_IS_WEIGHT":
                         AmoutIsWeight = bool.Parse(reader.Value);
                         break;
                     case "LABORATORY":
                         yeast.Laboratory = reader.Value;
                         break;
                     case "PRODUCT_ID":
                         yeast.ProductID = int.Parse(reader.Value);
                         break;
                     case "MIN_TEMPERATURE":
                         yeast.MinTemperature = float.Parse(reader.Value);
                         break;
                     case "MAX_TEMPERATURE":
                         yeast.MaxTemperature = float.Parse(reader.Value);
                         break;
                     case "FLOCCULATION":
                         Models.YeastFlocculation focculation = new Models.YeastFlocculation() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.YeastFlocculations.Find(focculation.Name) == null)
                             {
                                 context.YeastFlocculations.Add(focculation);
                                 context.SaveChanges();
                             }
                         }
                         yeast.YeastFlocculation_Name = reader.Value;
                         break;
                     case "ATTENUATION":
                         yeast.Attenuation = float.Parse(reader.Value);
                         break;
                     case "TIMES_CULTURED":
                         yeast.TimesCultured = int.Parse(reader.Value);
                         break;
                    case "ADD_TO_SECONDARY":
                         AddToSecondary = bool.Parse(reader.Value);
                         break;
                 }
                 break;
             case XmlNodeType.EndElement:
                 if (reader.Name == "YEAST")
                 {
                     return yeast;
                 }
                 break;
         }
     }
     return yeast;
 }
Exemple #15
0
 public Models.Style ParseStyle(XmlTextReader reader)
 {
     Models.Style style = new Models.Style();
     var currentNodeName = "";
     while (reader.Read())
     {
         switch (reader.NodeType)
         {
             case XmlNodeType.Element: // The node is an element.
                 currentNodeName = reader.Name;
                 break;
             case XmlNodeType.Text:
                 switch (currentNodeName)
                 {
                     case "NAME":
                         style.Name = reader.Value;
                         break;
                     case "CATEGORY":
                         style.Category = reader.Value;
                         break;
                     case "CATEGORY_NUMBER":
                         style.CategoryNumber = reader.Value;
                         break;
                     case "STYLE_LETTER":
                         style.StyleLetter = reader.Value;
                         break;
                     case "STYLE_GUIDE":
                         style.StyleGuide = reader.Value;
                         break;
                     case "TYPE":
                         Models.StyleType styleType = new Models.StyleType() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.StyleTypes.Find(styleType.Name) == null)
                             {
                                 context.StyleTypes.Add(styleType);
                                 context.SaveChanges();
                             }
                         }
                         style.StyleType_Name = reader.Value;
                         break;
                     case "OG_MIN":
                         style.OGMin = float.Parse(reader.Value);
                         break;
                     case "OG_MAX":
                         style.OGMax = float.Parse(reader.Value);
                         break;
                     case "FG_MIN":
                         style.FGMin = float.Parse(reader.Value);
                         break;
                     case "FG_MAX":
                         style.FGMax = float.Parse(reader.Value);
                         break;
                     case "IBU_MIN":
                         style.IBUMin = float.Parse(reader.Value);
                         break;
                     case "IBU_MAX":
                         style.IBUMax = float.Parse(reader.Value);
                         break;
                     case "COLOR_MIN":
                         style.ColorMin = float.Parse(reader.Value);
                         break;
                     case "COLOR_MAX":
                         style.ColorMax = float.Parse(reader.Value);
                         break;
                     case "ABV_MIN":
                         style.ABVMin = float.Parse(reader.Value);
                         break;
                     case "ABV_MAX":
                         style.ABVMax = float.Parse(reader.Value);
                         break;
                     case "CARB_MIN":
                         style.CRABMin = float.Parse(reader.Value);
                         break;
                     case "CARB_MAX":
                         style.CRABMax = float.Parse(reader.Value);
                         break;
                     case "NOTES":
                         style.Notes = reader.Value;
                         break;
                     case "PROFILE":
                         style.Profile = reader.Value;
                         break;
                     case "INGREDIENTS":
                         style.Ingredients = reader.Value;
                         break;
                     case "EXAMPLES":
                         style.Eamples = reader.Value;
                         break;
                 }
                 break;
             case XmlNodeType.EndElement:
                 if (reader.Name == "STYLE")
                 {
                     return style;
                 }
                 break;
         }
     }
     return style;
 }
Exemple #16
0
 public Models.MashStep ParseMashStep(XmlTextReader reader)
 {
     Models.MashStep mashStep = new Models.MashStep();
     var currentNodeName = "";
     while (reader.Read())
     {
         switch (reader.NodeType)
         {
             case XmlNodeType.Element: // The node is an element.
                 currentNodeName = reader.Name;
                 break;
             case XmlNodeType.Text:
                 switch (currentNodeName)
                 {
                     case "NAME":
                         mashStep.Name = reader.Value;
                         break;
                     case "TYPE":
                         Models.MashStepType mashStepType = new Models.MashStepType() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.MashStepTypes.Find(mashStepType.Name) == null)
                             {
                                 context.MashStepTypes.Add(mashStepType);
                                 context.SaveChanges();
                             }
                         }
                         mashStep.MashStepType_Name = reader.Value;
                         break;
                     case "INFUSE_AMOUNT":
                         mashStep.InfuseAmount = float.Parse(reader.Value);
                         break;
                     case "STEP_TEMP":
                         mashStep.StepTemp = float.Parse(reader.Value);
                         break;
                     case "STEP_TIME":
                         mashStep.StepTime = float.Parse(reader.Value);
                         break;
                     case "RAMP_TIME":
                         mashStep.RampTime = float.Parse(reader.Value);
                         break;
                     case "END_TEMP":
                         mashStep.EndTemp = float.Parse(reader.Value);
                         break;
                     case "INFUSE_TEMP":
                         mashStep.InfuseTemp = float.Parse(reader.Value);
                         break;
                     case "DECOCTION_AMOUNT":
                         mashStep.DecoctionAmount = float.Parse(reader.Value);
                         break;
                 }
                 break;
             case XmlNodeType.EndElement:
                 if (reader.Name == "MASH_STEP")
                 {
                     return mashStep;
                 }
                 break;
         }
     }
     return mashStep;
 }
Exemple #17
0
        public void ParseBeerXML(string fileLocation)
        {
            XmlTextReader reader = new XmlTextReader(fileLocation);
            var currentNodeName = "";

            Models.Recipe recipe = null;
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        currentNodeName = reader.Name;
                        switch (currentNodeName)
                        {
                            case "RECIPE":
                                recipe = new Models.Recipe();
                                break;
                            case "STYLE":
                                Models.Style style = ParseStyle(reader);
                                using (var context = new Models.ModelsContext())
                                {
                                    if (context.Styles.Find(style.Name) == null)
                                    {
                                        context.Styles.Add(style);
                                        context.SaveChanges();
                                    }
                                }
                                recipe.Style = style;
                                break;
                             case "HOP":
                                string HopUses_Name = "";
                                float Amount = 0;
                                float Time = 0;
                                Models.Hop hop = ParseHop(reader, ref HopUses_Name, ref Amount, ref Time);
                                Models.RecipeHop recipeHop = new Models.RecipeHop();
                                recipeHop.Recipe_Name = recipe.Name;
                                recipeHop.Hop_Name = hop.Name;
                                recipeHop.HopUses_Name = HopUses_Name;
                                recipeHop.Amount = Amount;
                                recipeHop.Time = Time;

                                using (var context = new Models.ModelsContext())
                                {
                                    if (context.Hops.Find(hop.Name) == null)
                                    {
                                        context.Hops.Add(hop);
                                        context.SaveChanges();
                                    }
                                }
                                if (!recipe.RecipeHops.Contains(recipeHop))
                                {
                                    recipe.RecipeHops.Add(recipeHop);
                                }
                                break;
                             case "FERMENTABLE":
                                bool AddAfterBoil = false;
                                bool IsMashed = false;
                                Amount = 0;
                                Models.Fermentable fermentable = ParseFermentable(reader, ref AddAfterBoil, ref IsMashed, ref Amount);

                                Models.RecipeFermentable recipeFermentable = new Models.RecipeFermentable();
                                recipeFermentable.AddAfterBoil = AddAfterBoil;
                                recipeFermentable.IsMashed = IsMashed;
                                recipeFermentable.Fermentable_Name = fermentable.Name;
                                recipeFermentable.Recipe_Name = recipe.Name;
                                recipeFermentable.Amount = Amount;

                                using (var context = new Models.ModelsContext())
                                {
                                    if (context.Fermentables.Find(fermentable.Name) == null)
                                    {
                                        context.Fermentables.Add(fermentable);
                                        context.SaveChanges();
                                    }
                                }

                                if (!recipe.RecipeFermentables.Contains(recipeFermentable))
                                {
                                    recipe.RecipeFermentables.Add(recipeFermentable);
                                }
                                break;
                             case "YEAST":
                                bool AddToSecondary = false;
                                bool AmountIsWeight = false;
                                Amount = 0;
                                Models.Yeast yeast = ParseYeasts(reader, ref AddToSecondary, ref Amount, ref AmountIsWeight);

                                Models.RecipeYeast recipeYeast = new Models.RecipeYeast();
                                recipeYeast.AddToSecondary = AddToSecondary;
                                recipeYeast.Recipe_Name = recipe.Name;
                                recipeYeast.Yeast_Name = yeast.Name;
                                recipeYeast.AmountIsWeight = AmountIsWeight;
                                recipeYeast.Amount = Amount;

                                using (var context = new Models.ModelsContext())
                                {
                                    if (context.Yeasts.Find(yeast.Name) == null)
                                    {
                                        context.Yeasts.Add(yeast);
                                        context.SaveChanges();
                                    }
                                }
                                if (!recipe.RecipeYeasts.Contains(recipeYeast))
                                {
                                    recipe.RecipeYeasts.Add(recipeYeast);
                                }
                                break;
                             case "MASH":
                                Models.MashProfile mash = ParseMash(reader);
                                recipe.Mash = mash;
                                break;
                            case "EQUIPMENT":
                                ParseEquipment(reader);
                                break;
                        }
                        break;
                    case XmlNodeType.Text:
                        if (recipe == null)
                        {
                            break;
                        }
                        switch (currentNodeName)
                        {
                            case "NAME":
                                recipe.Name = reader.Value;
                                break;
                            case "TYPE":
                                Models.RecipieType recipieType = new Models.RecipieType() { Name = reader.Value };
                                using (var context = new Models.ModelsContext())
                                {
                                    if (context.RecipieTypes.Find(recipieType.Name) == null)
                                    {
                                        context.RecipieTypes.Add(recipieType);
                                        context.SaveChanges();
                                    }
                                }
                                recipe.RecipieType_Name = reader.Value;
                                break;
                            case "BATCH_SIZE":
                                recipe.BatchSize = float.Parse(reader.Value);
                                break;
                            case "BOIL_SIZE":
                                recipe.BoilSize = float.Parse(reader.Value);
                                break;
                            case "BOIL_TIME":
                                recipe.BoilTime = float.Parse(reader.Value);
                                break;
                            case "EFFICIENCY":
                                recipe.Efficiency = float.Parse(reader.Value);
                                break;
                            case "NOTES":
                                recipe.Notes = reader.Value;
                                break;
                            case "TASTE_NOTES":
                                recipe.TasteNotes = reader.Value;
                                break;
                            case "TASTE_RATING":
                                recipe.TasteRating = float.Parse(reader.Value);
                                break;
                            case "OG":
                                recipe.OG = float.Parse(reader.Value);
                                break;
                            case "FG":
                                recipe.FG = float.Parse(reader.Value);
                                break;
                            case "FERMENTATION_STAGES":
                                recipe.FermentationStages = int.Parse(reader.Value);
                                break;
                            case "PRIMARY_AGE":
                                recipe.PrimayAge = float.Parse(reader.Value);
                                break;
                            case "PRIMARY_TEMP":
                                recipe.PrimayTemp = float.Parse(reader.Value);
                                break;
                            case "SECONDARY_AGE":
                                recipe.SecondaryAge = float.Parse(reader.Value);
                                break;
                            case "SECONDARY_TEMP":
                                recipe.SecondaryTemp = float.Parse(reader.Value);
                                break;
                            case "TERTIARY_AGE":
                                recipe.TertiaryAge = float.Parse(reader.Value);
                                break;
                            case "TERTIARY_TEMP":
                                recipe.TertiaryTemp = float.Parse(reader.Value);
                                break;
                            case "AGE":
                                recipe.Age = float.Parse(reader.Value);
                                break;
                            case "AGE_TEMP":
                                recipe.AgeTemp = float.Parse(reader.Value);
                                break;
                            case "DATE":
                                DateTime maxDt = new DateTime(2000,1,1,17,0,0);
                                TimeSpan timeSpan = DateTime.Today - maxDt;
                                var randomTest = new Random();
                                TimeSpan newSpan = new TimeSpan(0, randomTest.Next(0, (int)timeSpan.TotalMinutes), 0);
                                DateTime newDate = maxDt + newSpan;
                                recipe.Date = DateTime.Today;
                                break;
                            case "CARBONATION":
                                recipe.Carbonation = float.Parse(reader.Value);
                                break;
                            case "FORCED_CARBONATION":
                                recipe.ForcedCarbonation = bool.Parse(reader.Value);
                                break;
                            case "PRIMING_SUGAR_NAME":
                                recipe.PrimingSugarName = reader.Value;
                                break;
                            case "CARBONATION_TEMP":
                                recipe.CarbonationTemp = float.Parse(reader.Value);
                                break;
                            case "PRIMING_SUGAR_EQUIV":
                                recipe.PrimingSugarEquiv = float.Parse(reader.Value);
                                break;
                            case "KEG_PRIMING_FACTOR":
                                recipe.KegPrimingFactor = float.Parse(reader.Value);
                                break;
                        }
                        break;
                    case XmlNodeType.EndElement:
                        if (reader.Name == "RECIPE" && recipe != null)
                        {
                            using (var context = new Models.ModelsContext())
                            {
                                if (context.Recipes.Find(recipe.Name) == null)
                                {
                                    //foreach (Models.Yeast yeast in recipe.Yeasts)
                                    //{
                                    //    context.Yeasts.Attach(yeast);
                                    //    yeast.UsingRecipes.Add(recipe);
                                    //}

                                    //foreach (Models.Fermentable fermentable in recipe.Fermentables)
                                    //{
                                    //    context.Fermentables.Attach(fermentable);
                                    //    fermentable.UsingRecipes.Add(recipe);
                                    //}

                                    //foreach (Models.Hop hop in recipe.Hops)
                                    //{
                                    //    context.Hops.Attach(hop);
                                    //    hop.UsingRecipes.Add(recipe);
                                    //}

                                    context.Styles.Attach(recipe.Style);
                                    recipe.Style.UsingRecipes.Add(recipe);

                                    context.MashProfiles.Add(recipe.Mash);

                                    context.Recipes.Add(recipe);
                                    context.SaveChanges();
                                }
                            }
                            recipe = null;
                        }
                        break;
                }
            }
        }
Exemple #18
0
 public Models.Hop ParseHop(XmlTextReader reader, ref string HopUses_Name, ref float Amount, ref float Time)
 {
     Models.Hop hop = new Models.Hop();
     var currentNodeName = "";
     while (reader.Read())
     {
         switch (reader.NodeType)
         {
             case XmlNodeType.Element: // The node is an element.
                 currentNodeName = reader.Name;
                 break;
             case XmlNodeType.Text:
                 switch (currentNodeName)
                 {
                     case "NAME":
                         hop.Name = reader.Value;
                         break;
                     case "ALPHA":
                         hop.Alpha = float.Parse(reader.Value);
                         break;
                     case "AMOUNT":
                         Amount = float.Parse(reader.Value);
                         break;
                     case "USE":
                         Models.HopUse hopUse = new Models.HopUse() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.HopUses.Find(hopUse.Name) == null)
                             {
                                 context.HopUses.Add(hopUse);
                                 context.SaveChanges();
                             }
                         }
                         HopUses_Name = reader.Value;
                         break;
                     case "TIME":
                         Time = float.Parse(reader.Value);
                         break;
                    case "TYPE":
                         Models.HopType hopType = new Models.HopType() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.HopTypes.Find(hopType.Name) == null)
                             {
                                 context.HopTypes.Add(hopType);
                                 context.SaveChanges();
                             }
                         }
                         hop.HopType_Name = reader.Value;
                         break;
                     case "FORM":
                         Models.HopForm hopForm = new Models.HopForm() { Name = reader.Value };
                         using (var context = new Models.ModelsContext())
                         {
                             if (context.HopForms.Find(hopForm.Name) == null)
                             {
                                 context.HopForms.Add(hopForm);
                                 context.SaveChanges();
                             }
                         }
                         hop.HopForm_Name = reader.Value;
                         break;
                     case "BETA":
                         hop.Beta = float.Parse(reader.Value);
                         break;
                     case "HSI":
                         hop.HSI = float.Parse(reader.Value);
                         break;
                     case "ORIGIN":
                         hop.Origin = reader.Value;
                         break;
                     case "SUBSTITUTES":
                         //hop.Substitutes = reader.Value;
                         break;
                     case "HUMULENE":
                         hop.Humulene = float.Parse(reader.Value);
                         break;
                     case "CARYOPHYLLENE":
                         hop.Caryophyllene = float.Parse(reader.Value);
                         break;
                     case "COHUMULONE":
                         hop.Cohumulone = float.Parse(reader.Value);
                         break;
                     case "MYRCENE":
                         hop.Myrcene = float.Parse(reader.Value);
                         break;
                 }
                 break;
             case XmlNodeType.EndElement:
                 if (reader.Name == "HOP")
                 {
                     return hop;
                 }
                 break;
         }
     }
     return hop;
 }
Exemple #19
0
        public ActionResult List(string type = "hop", int pageno = 0)
        {
            IngredientListViewModel vm = new IngredientListViewModel { Type = type };

            using (var context = new Models.ModelsContext())
            {
                if (type == "hop")
                {
                    var hopsModel = context.Hops.Select(p => new HopListItemViewModel
                        {
                            Alpha = p.Alpha,
                            Beta = p.Beta,
                            Name = p.Name,
                            Type = p.HopType_Name
                        }).OrderBy(p => p.Name);

                    vm.Hops = new ViewModels.PagedList<HopListItemViewModel>(hopsModel, pageno);
                }
                else if (type == "fermentable")
                {
                    var fermentablesModel = context.Fermentables.Select(p => new FermentableListItemViewModel
                        {
                            Name = p.Name,
                            Color = p.Color,
                            IBUs = p.IBUs
                        }).OrderBy(p => p.Name);

                    vm.Fermentables = new ViewModels.PagedList<FermentableListItemViewModel>(fermentablesModel, pageno);
                }
                else
                {
                    return HttpNotFound();
                }
            }

            return View(vm);
        }
Exemple #20
0
        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");
        }