public async Task <ActionResult <PostDTO> > UpdatePost(int id, PostDTO postDTO) { try { if (id != postDTO.Id) { return(BadRequest()); } Post post = await GetPost(id); PostDTO tmpPostDTO = post.ToDTO(); if (post == null) { return(NotFound()); } post.Title = postDTO.Title; post.Body = postDTO.Body; post.EditTimeStamp = DateTime.Now; bool isNewAcceptedAnswer = post.AcceptedAnswerId != postDTO.AcceptedAnswerId; if (isNewAcceptedAnswer && postDTO.AcceptedAnswerId.HasValue && postDTO.AcceptedAnswerId != 0) { var postOwner = post.Author; var actor = (from r in post.Replies where r.Id == postDTO.AcceptedAnswerId select r.Author).FirstOrDefault(); var action = ReputationAction.AcceptedAnswer; if (actor != null) { ReputationManagementExtension.ReputationUpdate(postOwner, actor, action); } } post.AcceptedAnswerId = postDTO.AcceptedAnswerId; Console.WriteLine("\n " + tmpPostDTO.Tags + " \n - " + postDTO.Tags); if ((post.ParentId == 0 || post.ParentId == null) && CollectionMethodExtension.IsDifferentCollection <PostTagDTO>(tmpPostDTO.Tags, postDTO.Tags)) { //On pourrait utiliser une method generic - To Do si on a le temps - Bad Practice d'avoir une method aussi longue foreach (PostTagDTO tag in postDTO.Tags) { if (!tmpPostDTO.Tags.Contains(tag)) { PostTag pt = new PostTag(); pt.PostId = post.Id; pt.TagId = tag.Id; await _postTagRepository.Add(pt).ConfigureAwait(true); } } foreach (PostTagDTO tag in tmpPostDTO.Tags) { if (!postDTO.Tags.Contains(tag)) { PostTag pt = new PostTag(); pt.PostId = post.Id; pt.TagId = tag.Id; await _postTagRepository.Delete(pt).ConfigureAwait(true); } } } await _postRepository.Edit(post); } catch (Exception ex) { return(StatusCode(500, ex)); throw; } return(Ok((await GetPost(id)).ToDTO())); }
//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()); }