public decimal UpdateRating(decimal rating, int?ratingFlowerId)
        {
            var existRatingFlower = DbContext.RatingFlowers.Find(ratingFlowerId);
            var oldRating         = existRatingFlower.Rating;

            existRatingFlower.Rating = rating;
            DbContext.RatingFlowers.AddOrUpdate(existRatingFlower);

            var existRatingCount = DbContext.RatingCounts.Find(existRatingFlower.FlowerCode);

            if (existRatingCount == null)
            {
                var ratingCount = new RatingCount()
                {
                    Code           = existRatingFlower.FlowerCode,
                    NumberOfRating = DbContext.RatingFlowers.Where(s => s.FlowerCode == existRatingFlower.FlowerCode).Count()
                };

                DbContext.RatingCounts.Add(ratingCount);
            }

            DbContext.SaveChanges();

            return(oldRating);
        }
Exemplo n.º 2
0
        public void UpdateDisplay()
        {
            SetCurrentRating(RatingStars, Math.Min(Convert.ToInt32(Math.Round(AverageRating, MidpointRounding.AwayFromZero)), MaxRating));

            Count.Text = RatingCount.ToString(CultureInfo.InvariantCulture);

            RatingStars.ReadOnly = !Enabled;
        }
        public void CreateRating(decimal rating, string flowerCode, string userId)
        {
            var item = new RatingFlower()
            {
                FlowerCode = flowerCode,
                UserId     = userId,
                Rating     = rating
            };

            DbContext.RatingFlowers.Add(item);

            var existRatingCount = DbContext.RatingCounts.Find(item.FlowerCode);

            if (existRatingCount != null)
            {
                if (existRatingCount.NumberOfRating == 0)
                {
                    existRatingCount.NumberOfRating = DbContext.RatingFlowers.Where(s => s.FlowerCode == item.FlowerCode).Count();
                }
                else
                {
                    existRatingCount.NumberOfRating++;
                }

                DbContext.RatingCounts.AddOrUpdate(existRatingCount);
            }
            else
            {
                var ratingCount = new RatingCount()
                {
                    Code           = item.FlowerCode,
                    NumberOfRating = DbContext.RatingFlowers.Where(s => s.FlowerCode == item.FlowerCode).Count() + 1
                };

                DbContext.RatingCounts.Add(ratingCount);
            }

            //var ratingCount = new RatingCount()
            //{
            //    Code = flowerCode,
            //    NumberOfRating = 1
            //};

            //DbContext.RatingCounts.Add(ratingCount);
            DbContext.SaveChanges();
        }