예제 #1
0
 public void UpVote(Usuario usuario)
 {
     if (UsuarioJaInteragiuComResposta(usuario))
     {
         throw new UsuarioJaDeuUpVoteException();
     }
     UpVotes.Add(new UpVoteResposta(this, usuario));
 }
예제 #2
0
        public IHttpActionResult UpVote(int id)
        {
            var user      = _context.Users.Find(User.Identity.GetUserId());
            var post      = _context.Posts.Find(id);
            var upVotes   = post.UpVotes.ToList();
            var downVotes = post.DownVotes.ToList();

            if (upVotes.FindAll(u => u.UserId == user.Id).Count == 0)
            {
                if (downVotes.Select(u => u.UserId).Contains(user.Id))
                {
                    var downVote = _context.DownVotes.Where(u => u.UserId == user.Id).FirstOrDefault();
                    _context.DownVotes.Remove(downVote);
                    _context.SaveChanges();
                }
                UpVotes upVote = new UpVotes
                {
                    VotedDateTime = DateTime.Now,
                    UserId        = user.Id,
                    User          = user,
                    Post          = post,
                    PostId        = id,
                };
                post.UpVotes.Add(upVote);
                var repCount = post.UpVotes.Count - post.DownVotes.Count;
                if (repCount <= 2)
                {
                    post.CreatedBy.Reputation = "beginer";
                }
                else if (repCount > 2)
                {
                    post.CreatedBy.Reputation = "Intermediat";
                }
                else if (repCount > 4)
                {
                    post.CreatedBy.Reputation = "pro";
                }
                _context.SaveChanges();
                return(Ok());
            }
            return(BadRequest());
        }
예제 #3
0
 public bool UsuarioJaInteragiuComResposta(Usuario usuario)
 {
     return(UpVotes.Any(u => u.Usuario.Id == usuario.Id) ||
            DownVotes.Any(d => d.Usuario.Id == usuario.Id));
 }
예제 #4
0
 public bool DeveSerExcluida()
 {
     return((UpVotes.Count() - DownVotes.Count()) <= 4);
 }