Ejemplo n.º 1
0
        public async Task <OperationDetails> RateComment(long commentId, string username, RateAction rateAction)
        {
            Comment     comment = repo.Comments.Get(commentId);
            UserProfile profile = repo.Profiles.Find(p => p.User.UserName == username).FirstOrDefault();


            if (comment != null && profile != null)
            {
                if (comment.Author.AccountId == profile.AccountId)
                {
                    return(new OperationDetails(false, "Can`t rate own comment"));
                }

                if (rateAction == RateAction.Up)
                {
                    if (comment.DislikedBy.Contains(profile))
                    {
                        comment.DislikedBy.Remove(profile);
                    }
                    else if (!comment.LikedBy.Contains(profile))
                    {
                        comment.LikedBy.Add(profile);
                    }
                    else
                    {
                        return(new OperationDetails(false, "This comment is already rated up"));
                    }
                }
                else
                {
                    if (comment.LikedBy.Contains(profile))
                    {
                        comment.LikedBy.Remove(profile);
                    }
                    else if (!comment.DislikedBy.Contains(profile))
                    {
                        comment.DislikedBy.Add(profile);
                    }
                    else
                    {
                        return(new OperationDetails(false, "This comment is already rated down"));
                    }
                }
                repo.Comments.Update(comment);
                await repo.SaveChangesAsync();

                return(new OperationDetails(true, "Successfull"));
            }

            return(new OperationDetails(false, "Comment not found"));
        }
Ejemplo n.º 2
0
        public async Task <OperationDetails> RatePublication(long publicationId, string username, RateAction rateAction)
        {
            var publication = repo.Publications.Get(publicationId);
            var profile     = repo.Profiles.Find(p => p.User.UserName == username).FirstOrDefault();

            if (publication != null && profile != null)
            {
                if (publication.Author.AccountId == profile.AccountId)
                {
                    return(new OperationDetails(false, "Can`t rate own publication"));
                }

                if (rateAction == RateAction.Up)
                {
                    if (publication.DislikedBy.Contains(profile))
                    {
                        publication.DislikedBy.Remove(profile);
                    }
                    else if (!publication.LikedBy.Contains(profile))
                    {
                        publication.LikedBy.Add(profile);
                    }
                    else
                    {
                        return(new OperationDetails(false, "This publication is already rated up"));
                    }
                }
                else
                {
                    if (publication.LikedBy.Contains(profile))
                    {
                        publication.LikedBy.Remove(profile);
                    }
                    else if (!publication.DislikedBy.Contains(profile))
                    {
                        publication.DislikedBy.Add(profile);
                    }
                    else
                    {
                        return(new OperationDetails(false, "This publication is already rated down"));
                    }
                }
                repo.Publications.Update(publication);
                await repo.SaveChangesAsync();

                return(new OperationDetails(true, "Successfull"));
            }

            return(new OperationDetails(false, "Publication not found"));
        }