コード例 #1
0
        public JsonResult CreateNewCommentRating(long commentId, bool newRating)
        {
            CommentRating cr = new CommentRating
            {
                LikeOwner = UsersHelper.LoggedInUserUsername(Session),
                CommentId = commentId,
                LikeDate  = DateTime.Now,
                IsLike    = newRating
            };

            _commentRatingsRepository.CreateCommentRating(cr);
            Comment comment = _commentsRepository.GetCommentById((long)cr.CommentId);

            if (newRating)
            {
                comment.LikesCount += 1;
            }
            else
            {
                comment.DislikesCount += 1;
            }
            _commentsRepository.UpdateComment(comment);

            string returnMessage = (newRating == true) ? "like" : "dislike";

            return(Json(new { returnMessage, comment.LikesCount, comment.DislikesCount }, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
 public void UpdateCommentRating(CommentRating cr)
 {
     if (cr != null)
     {
         db.Entry(cr).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
コード例 #3
0
 public void CreateCommentRating(CommentRating cr)
 {
     if (cr != null)
     {
         db.CommentRatings.Add(cr);
         db.SaveChanges();
     }
 }
コード例 #4
0
        public void Add(CommentRating commentRating)
        {
            if (commentRating == null)
            {
                return;
            }

            context.CommentRating.Add(commentRating);
            context.SaveChanges();
        }
コード例 #5
0
        public async Task <ActionResult <TComment> > PostTComment(CommentRating commentRating)
        {
            //validate comment and rating.
            var errStr = new StringBuilder();

            if (string.IsNullOrWhiteSpace(commentRating.Comment))
            {
                errStr.Append(Resources.BlankComment);
            }
            else if (!string.IsNullOrWhiteSpace(commentRating.Comment) && commentRating.Comment.Length > 200)
            {
                errStr.Append(Resources.CommentTooLong);
            }
            else if (commentRating.Rating < 1 || commentRating.Rating > 5)
            {
                errStr.Append(Resources.RatingOutOfRange);
            }


            try
            {
                using (var conn = _context)
                {
                    //check coffee exists
                    var coffee = await conn.TCoffees.FirstAsync(x => x.CoffeeId == commentRating.CoffeeID);

                    if (coffee == null)
                    {
                        errStr.Append(Resources.NoCoffeeFound);
                    }

                    //send error back
                    if (!string.IsNullOrEmpty(errStr.ToString()))
                    {
                        ValidationProblemDetails vpd = new ValidationProblemDetails();
                        vpd.Detail = errStr.ToString();
                        return(ValidationProblem(vpd));
                    }
                    var newRating = new TRating(commentRating.Rating, commentRating.CoffeeID);
                    conn.TRatings.Add(newRating);
                    await conn.SaveChangesAsync();

                    var newComment = new TComment(commentRating.Comment, commentRating.CoffeeID);
                    newComment.RatingId = newRating.RatingId;
                    conn.TComments.Add(newComment);
                    await conn.SaveChangesAsync();

                    return(CreatedAtAction("GetTComment", new { id = newComment.CommentId }, newComment));
                }
            }
            catch (Exception e)
            {
                return(Problem(Resources.ErrorSavingComment, $"Comment: {commentRating.Comment}. Rating: {commentRating.Rating}. CoffeeID: {commentRating.CoffeeID}. {e.Message}", null, Resources.ErrorSavingCommentTitle, "Comment"));
            }
        }
コード例 #6
0

        
コード例 #7
0
        public void AddRatingToComment(int commentId, int userId, bool positive)
        {
            var commentRating = new CommentRating
            {
                CommentId = commentId,
                UserId    = userId,
                Positive  = positive,
            };

            _repository.InsertCommentRating(commentRating);
        }
コード例 #8
0
        public void AddCommentRating(CommentRating commentRating)
        {
            var comment = context.UserComment.Where(x => x.Id == commentRating.CommentId).FirstOrDefault();

            if (comment == null)
            {
                return;
            }
            comment.CommentRatings.Add(commentRating);
            context.SaveChanges();
        }
コード例 #9
0
        public static CommentRatingsDTO ConvertCommentRToDTO(CommentRating cr)
        {
            CommentRatingsDTO newCRDTO = new CommentRatingsDTO
            {
                LikeID    = cr.LikeID,
                LikeOwner = cr.LikeOwner,
                CommentId = (long)cr.CommentId,
                IsLike    = cr.IsLike
            };

            return(newCRDTO);
        }
コード例 #10
0
        public JsonResult AlterExistingCommentRating(CommentRating cr, bool newRating)
        {
            string  returnMessage = "";
            Comment comment       = _commentsRepository.GetCommentById((long)cr.CommentId);

            //true = like, false = dislike
            if (cr.IsLike)
            {
                if (newRating)
                {
                    comment.LikesCount -= 1;
                    returnMessage       = "neutral";
                }
                else
                {
                    comment.LikesCount    -= 1;
                    comment.DislikesCount += 1;
                    returnMessage          = "dislike";
                }
            }
            else
            {
                if (newRating)
                {
                    comment.LikesCount    += 1;
                    comment.DislikesCount -= 1;
                    returnMessage          = "like";
                }
                else
                {
                    comment.DislikesCount -= 1;
                    returnMessage          = "neutral";
                }
            }

            _commentsRepository.UpdateComment(comment);

            cr.IsLike = newRating;
            if (returnMessage == "neutral")
            {
                _commentRatingsRepository.DeleteCommentRating(cr.LikeID);
            }
            else
            {
                _commentRatingsRepository.UpdateCommentRating(cr);
            }

            return(Json(new { returnMessage, comment.LikesCount, comment.DislikesCount }, JsonRequestBehavior.AllowGet));
        }
コード例 #11
0
        public void Update(CommentRating commentRating)
        {
            var result = context.CommentRating.Where(x => x.Id == commentRating.Id).FirstOrDefault();

            if (result == null)
            {
                return;
            }
            result.Rating   = commentRating.Rating;
            result.DownVote = commentRating.DownVote;
            result.UpVote   = commentRating.UpVote;

            result.Reported   = commentRating.Reported;
            result.ReportText = commentRating.ReportText;

            context.SaveChanges();
        }
コード例 #12
0
        public JsonResult CreateCommentRating(long commentId, bool newRating)
        {
            if (UsersHelper.LoggedInUserUsername(Session) == null)
            {
                return(null);
            }
            string        username = UsersHelper.LoggedInUserUsername(Session);
            CommentRating cr       = _commentRatingsRepository.GetCommentRating(commentId, username);

            if (cr != null)
            {
                return(AlterExistingCommentRating(cr, newRating));
            }
            else
            {
                return(CreateNewCommentRating(commentId, newRating));
            }
        }
コード例 #13
0
ファイル: HomeController.cs プロジェクト: thanhtrannn/ParkNGo
        /// <summary>
        /// Function to save review to db
        /// </summary>
        /// <param name="comment">Comment input left by user</param>
        /// <param name="id">Username</param>
        /// <param name="rating">Rating input by user</param>
        /// <returns>HTTP status code with message</returns>
        public async Task <IActionResult> SaveReview(string comment, int id, string rating)
        {
            try
            {
                Log.Information("Saving comment to DB...");
                // build comment object and save to db
                var Comment = new CommentRating()
                {
                    Username = ViewData["Username"].ToString(), Id = id, Comment = comment, Rating = Int32.Parse(rating), Date = DateTime.Now
                };
                _context.Add(Comment);
                await _context.SaveChangesAsync();

                // update property rating
                var propertyComment = _context.CommentRating.Where(x => x.Id == id).ToList();
                // check rating for all comment left for property
                int totalRating  = 0;
                var ratingAmount = 0;
                foreach (var prop in propertyComment)
                {
                    totalRating += (int)prop.Rating;
                    ratingAmount++;
                }

                // average
                float averageRating = totalRating / ratingAmount;
                Log.Information("Average rating is " + averageRating);
                var property = _context.UserProperty.FirstOrDefault(x => x.PropertyId == id);
                property.Rating = averageRating;
                _context.Update(property);
                await _context.SaveChangesAsync();

                return(Ok("Review successfully saved!"));
            }
            catch (Exception ex)
            {
                Log.ForContext <HomeController>().Error(ex, "Problem with saving user comment to db");
                return(NotFound("Error with saviing"));
            }
        }
コード例 #14
0
        public static CommentRating ConvertToCommentRating(CommentRatingModel model)
        {
            if (model == null)
            {
                return(null);
            }

            CommentRating commentRating = new CommentRating()
            {
                Id          = model.Id,
                CommentId   = model.CommentId,
                OwnerId     = model.OwnerId,
                storeItemId = model.StoreItemId,
                DataCreated = model.DataCreated,
                Rating      = model.Rating,
                DownVote    = model.DownVote,
                UpVote      = model.UpVote,
                Reported    = model.Report,
                ReportText  = model.OpinionText,
            };

            return(commentRating);
        }
コード例 #15
0
        public static CommentRatingModel ConvertToCommentRatingModel(CommentRating commentRating)
        {
            if (commentRating == null)
            {
                return(null);
            }

            CommentRatingModel model = new CommentRatingModel()
            {
                Id          = commentRating.Id,
                CommentId   = commentRating.CommentId,
                OwnerId     = commentRating.OwnerId,
                StoreItemId = commentRating.storeItemId,
                DataCreated = commentRating.DataCreated,
                Rating      = commentRating.Rating,
                DownVote    = commentRating.DownVote,
                UpVote      = commentRating.UpVote,
                Report      = commentRating.Reported,
                OpinionText = commentRating.ReportText,
            };

            return(model);
        }
コード例 #16
0
        public void SyncService_Map_ReplaceQuot()
        {
            Type type = typeof(SyncService);
            //var hello = Activator.CreateInstance(type, firstName, lastName);
            var        syncService = new SyncService(null, null, null, null);
            MethodInfo mapMethod   = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
                                     .Where(x => x.Name == "Map" && x.IsPrivate)
                                     .First();

            string      initialText = "&quot;это&quot;";
            CommentInfo from        = new()
            {
                Rating = new CommentRating(),
                Text   = initialText
            };
            NewsArticleComment to = new();

            mapMethod.Invoke(syncService, new object[] { from, to });
            Assert.NotEqual(from.Text, to.Text);
            string mappedText = "\"это\"";

            Assert.Equal(mappedText, to.Text);
        }
    }
コード例 #17
0
        public async Task <CommentRatingDTO> Update(CommentRatingDTO item)
        {
            CommentRating commentRating = await _database.CommentRatingRepository.Update(_mapper.Map <CommentRatingDTO, CommentRating>(item));

            return(_mapper.Map <CommentRating, CommentRatingDTO>(commentRating));
        }
コード例 #18
0
        public async Task <CommentRatingDTO> Delete(string id)
        {
            CommentRating commentRating = await _database.CommentRatingRepository.Delete(id);

            return(_mapper.Map <CommentRating, CommentRatingDTO>(commentRating));
        }
コード例 #19
0
 public void InsertCommentRating(CommentRating commentRating)
 {
     _context.CommentsRatings.Add(commentRating);
     _context.SaveChanges();
 }