public static void ReputationUpdate(User postOwner, User actor, ReputationAction action) { switch (action) { case ReputationAction.AcceptedAnswer: actor.Reputation += 15; postOwner.Reputation += 2; break; case ReputationAction.PositiveVote: postOwner.Reputation += 10; break; case ReputationAction.NegativeVote: postOwner.Reputation += (-2); actor.Reputation += (-1); break; default: break; } }
//Après reflexion il serait préferable d'avoir un seul controller et de prendre la valeur du vote via le front end. //Mais afin de conserver la logique applicative hors du front end je laisse comme ça pour l'instant private async Task <PostDTO> DoVote(int userId, int postId, int valueVote) { var user = await _userRepository.GetSingle(userId); if (user == null) { return(null); } var post = await _postRepository.GetSingle(postId, search => search .Include(p => p.Replies) .ThenInclude(p => p.Author) .ThenInclude(p => p.Comments) .ThenInclude(c => c.Author) .Include(p => p.Author) .Include(p => p.Comments) .ThenInclude(c => c.Author) .Include(p => p.PostTags) .ThenInclude(pt => pt.Tag) .Include(p => p.Votes) ); if (post == null) { return(null); } Vote newVote = new Vote(userId, postId, valueVote); Vote oldVote = null; if (post.Votes != null) { oldVote = (from vote in post.Votes where vote.AuthorId == newVote.AuthorId && vote.PostId == newVote.PostId select vote).FirstOrDefault(); } var postOwner = post.Author; var actor = user; ReputationAction action = ReputationAction.NegativeVote; bool userUpd = true; if (newVote.UpDown == 1) { action = ReputationAction.PositiveVote; } if (oldVote != null) { if (oldVote.UpDown == newVote.UpDown) //(1) { await _voteRepository.Delete(oldVote); userUpd = false; } else if (oldVote.UpDown == newVote.UpDown * (-1)) //(2) { await _voteRepository.Delete(oldVote); await _voteRepository.Add(newVote); ReputationManagementExtension.ReputationUpdate(postOwner, actor, action); } } else { await _voteRepository.Add(newVote); //(3) ReputationManagementExtension.ReputationUpdate(postOwner, actor, action); } if (userUpd) { await _userRepository.Edit(postOwner); await _userRepository.Edit(actor); } return(post.ToDTO()); }