コード例 #1
0
        public IActionResult AddCommentWithRating(CommentWithRatingViewModel model)
        {
            //for now Hard Code the UserId = 0bb8a75f-68ea-4d27-9c5f-81b8cdd9d000
            Guid?userId = Guid.Parse("0bb8a75f-68ea-4d27-9c5f-81b8cdd9d000");

            var post = _context.Posts.FirstOrDefault(p => p.Id == model.PostId);

            if (post != null)
            {
                if (post.RatingsEnabled == true)
                {
                    var userRating = _context.Ratings.FirstOrDefault(r => r.UserId == userId && r.PostId == model.PostId);

                    if (userRating == null)
                    {
                        userRating = new Infrastructure.Domain.Models.Rating()
                        {
                            Score     = model.Score,
                            PostId    = model.PostId,
                            UserId    = userId,
                            IsCounted = true,
                            MaskUser  = false
                        };

                        _context.Ratings.Add(userRating);
                        _context.SaveChanges();
                    }
                }

                if (post.CommentsEnabled == true)
                {
                    var userComment = new Infrastructure.Domain.Models.Comment()
                    {
                        Content      = model.Comment,
                        IsPublished  = true,
                        PostId       = model.PostId,
                        UserId       = userId,
                        MaskUser     = false,
                        LikesEnabled = true,
                        Likes        = 0,
                    };

                    _context.Comments.Add(userComment);
                    _context.SaveChanges();
                }

                return(RedirectPermanent("~/posts/" + model.PostId));
            }

            return(NotFound());
        }
コード例 #2
0
        public IActionResult AddCommentWithRating(CommentWithRatingViewModel model)
        {
            var user = _context.Users.FirstOrDefault(p => p.Id == model.UserId);

            var shop = _context.Shops.FirstOrDefault(p => p.Id == model.ShopId);

            if (shop != null)
            {
                if (shop.RatingsEnabled == true)
                {
                    var userRating = _context.Ratings.FirstOrDefault(s => s.UserId == model.UserId && s.Id == model.ShopId);
                    if (userRating == null)
                    {
                        userRating = new Rating()
                        {
                            Score     = model.Score,
                            ShopId    = model.ShopId,
                            UserId    = model.UserId,
                            IsCounted = true,
                            MaskUser  = false,
                        };

                        _context.Ratings.Add(userRating);
                        _context.SaveChanges();
                    }

                    if (shop.CommentsEnabled == true)
                    {
                        var userComment = new Comment()
                        {
                            Content      = model.Comment,
                            IsPublished  = true,
                            ShopId       = model.ShopId,
                            UserId       = model.UserId,
                            MaskUser     = false,
                            LikesEnabled = false,
                            Likes        = 0
                        };

                        _context.Comments.Add(userComment);
                        _context.SaveChanges();
                    }

                    //return RedirectPermanent("~/shop/shop-items/" + model.ShopId);
                    return(RedirectPermanent("~/shop/" + model.ShopId));
                }
            }
            return(NotFound());
        }
コード例 #3
0
        public IActionResult CommentWithRating(CommentWithRatingViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            var user    = this._context.Users.FirstOrDefault(u => u.Id == model.UserId);
            var service = this._context.Services.FirstOrDefault(s => s.Id == model.ServiceId);

            if (service != null)
            {
                if (service.RatingsEnabled == true)
                {
                    var userRating = _context.Ratings.FirstOrDefault(r => r.UserId == model.UserId && r.ServiceId == model.ServiceId);
                    if (userRating == null)
                    {
                        userRating = new Infrastructures.Domain.Models.Rating()
                        {
                            Score     = model.Score,
                            ServiceId = model.ServiceId,
                            UserId    = model.UserId,
                            IsCounted = true,
                            MaskUser  = false
                        };

                        _context.Ratings.Add(userRating);
                        _context.SaveChanges();
                    }
                }
                if (service.CommentsEnabled == true)
                {
                    var userComment = new Comment()
                    {
                        Content      = model.Comment,
                        IsPublished  = true,
                        ServiceId    = model.ServiceId,
                        UserId       = model.UserId,
                        MaskUser     = false,
                        LikesEnabled = true,
                        Likes        = 0,
                    };
                    _context.Comments.Add(userComment);
                    _context.SaveChanges();
                }

                return(RedirectPermanent("~/booking/book/" + model.ServiceId));
            }
            return(NotFound());
        }