Example #1
0
        public async Task SaveRefreshTokenAsync(string pseudo, string token)
        {
            var member = await _context.Users.SingleOrDefaultAsync(u => u.Pseudo == pseudo);

            if (member != null)
            {
                member.RefreshToken = token;
                await _context.SaveChangesAsync();
            }
        }
Example #2
0
        public async Task <IActionResult> DeleteUser(int id)
        {
            var user = await _context.Users.FindAsync(id);

            if (user == null)
            {
                return(NotFound());
            }

            // Suppression en cascade des relations avec ce membre
            var comments = (from c in _context.Comments where c.User.Id == user.Id select c);
            var votes    = (from v in _context.Votes where v.User.Id == user.Id select v);
            var posts    = (from p in _context.Posts where p.Id == user.Id select p);

            if (comments != null)
            {
                foreach (var c in comments)
                {
                    _context.Remove(c);
                }
            }
            if (votes != null)
            {
                foreach (var v in votes)
                {
                    _context.Votes.Remove(v);
                }
            }
            if (posts != null)
            {
                foreach (var p in posts)
                {
                    _context.Posts.Remove(p);
                }
            }
            _context.Users.Remove(user);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Example #3
0
        public async Task <IActionResult> DeletePost(int id)
        {
            var post = await model.Posts.FindAsync(id);

            if (post == null)
            {
                return(NotFound());
            }

            var user = await model.Users.FindAsync(post.AuthorId);

            if (user == null)
            {
                return(NotFound());
            }

            if ((User.Identity.Name == user.Pseudo && post.NumResponse == 0 && post.NumComment == 0) || User.IsInRole(Role.Admin.ToString()))
            {
                var comments  = (from c in model.Comments where c.PostId == post.Id select c);
                var votes     = (from v in model.Votes where v.PostId == post.Id select v);
                var responses = (from r in model.Posts where r.ParentId == post.Id select r);
                var postTags  = (from pt in model.PostTags where pt.PostId == post.Id select pt);

                // foreach(var c in comments)
                if (comments != null)
                {
                    model.Comments.RemoveRange(comments);
                }

                foreach (var v in votes)
                {
                    if (v != null)
                    {
                        model.Votes.Remove(v);
                    }
                }

                foreach (var r in responses)
                {
                    if (r != null)
                    {
                        model.Posts.Remove(r);
                    }
                }
                foreach (var pt in postTags)
                {
                    if (pt != null)
                    {
                        model.PostTags.Remove(pt);
                    }
                }

                model.Posts.Remove(post);

                await model.SaveChangesAsync();
            }
            else
            {
                return(NotFound());
            }

            return(NoContent());
        }