Exemplo n.º 1
0
        public VoteResult Downvote(int threadId, int userId)
        {
            var thread = _context.Threads.Include(u => u.User)
                         .Include(v => v.Votes).ThenInclude(u => u.VoteBy)
                         .Include(c => c.Comments)
                         .First(x => x.ThreadId == threadId);

            var user = _context.Users.First(x => x.UserId == userId);

            var vote = thread.Votes.FirstOrDefault(x => x.VoteBy.UserId == user.UserId);

            if (vote == null)
            {
                vote = new ThreadVote()
                {
                    VoteScore = -1,
                    VoteBy    = user
                };

                thread.Votes.Add(vote);
            }
            else
            {
                vote.VoteScore = vote.VoteScore == -1 ? 0 : -1;
            }

            _context.SaveChanges();

            return(new VoteResult()
            {
                Score = thread.Votes.Sum(x => x.VoteScore),
                DidScoreIncrease = vote.VoteScore == 0,
            });
        }
Exemplo n.º 2
0
        public int CreateThread(Thread thread)
        {
            var entityThread = _mapper.Map <DAL.Entities.Thread>(thread);

            entityThread.Created  = DateTime.Now;
            entityThread.Modified = DateTime.Now;

            var threadVote = new ThreadVote()
            {
                VoteBy    = entityThread.User,
                VoteScore = 1
            };

            entityThread.Votes.Add(threadVote);

            _context.Threads.Add(entityThread);
            _context.SaveChanges();

            return(entityThread.ThreadId);
        }