public async Task <IActionResult> DeleteUser(string pseudo) { var user = await _context.Users.Where(u => u.Pseudo == pseudo).SingleOrDefaultAsync(); if (user == null) { return(NotFound()); } //delete de tous les commentaires de l'user _context.Comments.RemoveRange(from c in _context.Comments where c.UserId == user.Id select c); //delete de tous les votes de l'user _context.Votes.RemoveRange(from v in _context.Votes where v.UserId == user.Id select v); //delete de tous les posts de l'user à supprimer //recursion: suppression de toutes les réponses si le post est une question var posts = (from p in _context.Posts where p.UserId == user.Id select p); foreach (var p in posts) { _context.Comments.RemoveRange(from c in _context.Comments where c.PostId == p.Id select c); _context.Votes.RemoveRange(from v in _context.Votes where v.PostId == p.Id select v); _context.PostTags.RemoveRange(from post in _context.PostTags where post.PostId == p.Id select post); var responses = (from r in _context.Posts where r.ParentId == p.Id select r); foreach (var r in responses) { _context.Comments.RemoveRange(from c in _context.Comments where c.PostId == r.Id select c); _context.Votes.RemoveRange(from v in _context.Votes where v.PostId == r.Id select v); } _context.Posts.RemoveRange(responses); await _context.SaveChangesAsync(); _context.Posts.Remove(p); } _context.Users.Remove(user); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task SaveRefreshTokenAsync(string pseudo, string token) { var user = await _context.Users.Where(u => u.Pseudo == pseudo).SingleOrDefaultAsync(); if (user != null) { user.RefreshToken = token; await _context.SaveChangesAsync(); } }
public async Task <IActionResult> DeletePost(int id) { var post = await _context.Posts.FindAsync(id); if (post == null) { return(NotFound()); } var connectedUser = await _context.Users.Where(u => u.Pseudo == User.Identity.Name).SingleOrDefaultAsync(); if (!(post.User.Id == connectedUser.Id || connectedUser.Role == Role.Admin)) { return(BadRequest()); } if (connectedUser.Role != Role.Admin && post.User.Id == connectedUser.Id && !(post.Comments.Count() == 0 && post.Responses.Count == 0)) { return(BadRequest()); } _context.Comments.RemoveRange(from c in _context.Comments where c.PostId == id select c); _context.Votes.RemoveRange(from v in _context.Votes where v.PostId == id select v); _context.PostTags.RemoveRange(from p in _context.PostTags where p.PostId == id select p); var responses = (from r in _context.Posts where r.ParentId == id select r); foreach (var r in responses) { _context.Comments.RemoveRange(from c in _context.Comments where c.PostId == r.Id select c); _context.Votes.RemoveRange(from v in _context.Votes where v.PostId == r.Id select v); } _context.Posts.RemoveRange(responses); await _context.SaveChangesAsync(); _context.Posts.Remove(post); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> DeleteTag(int id) { var tag = await _context.Tags.FindAsync(id); if (tag == null) { return(NotFound()); } _context.PostTags.RemoveRange(from t in _context.PostTags where t.TagId == id select t); _context.Tags.Remove(tag); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> DeleteVote(int id) { var connectedUser = await _context.Users.Where(u => u.Pseudo == User.Identity.Name).SingleOrDefaultAsync(); var existVote = await _context.Votes.Where(v => v.PostId == id && v.User.Pseudo == User.Identity.Name).SingleOrDefaultAsync(); if (existVote == null) { return(NotFound()); } if (existVote.UserId != connectedUser.Id) { return(BadRequest()); } _context.Votes.Remove(existVote); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> DeleteComment(int id) { var currentUser = await _context.Users.Where(u => u.Pseudo == User.Identity.Name).SingleOrDefaultAsync(); //récuperation du commentaire dans le context var comment = await _context.Comments.FindAsync(id); //Contrôle résultat du context if (comment == null) { return(NotFound()); } //Contrôle comment appartient à l'user if (!(comment.User.Id == currentUser.Id || currentUser.Role == Role.Admin)) { return(BadRequest()); } //Suppression du comment dans le context _context.Comments.Remove(comment); await _context.SaveChangesAsync(); //end return(NoContent()); }