コード例 #1
0
ファイル: RecipesController.cs プロジェクト: nsipid/Brew
        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" });
        }
コード例 #2
0
ファイル: RecipesController.cs プロジェクト: nsipid/Brew
        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;
            }
        }