Beispiel #1
0
        private async Task <IActionResult> DeleteUser(User user)
        {
            _context.Users.Remove(user);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Beispiel #2
0
        public async Task <IActionResult> Delete(int id)
        {
            var comment = await _context.Comments.FindAsync(id);

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

            _context.Comments.Remove(comment);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Beispiel #3
0
        public async Task <IActionResult> Delete(int id)
        {
            var tag = await _context.Tags.FindAsync(id);

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

            _context.Tags.Remove(tag);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
        public async Task <IActionResult> PutPost(int id, PostDTO dto)
        {
            if (id != dto.Id)
            {
                return(BadRequest());
            }

            var post = await _context.Posts.FindAsync(id);

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

            post.Title            = dto.Title;
            post.Body             = dto.Body;
            post.AcceptedAnswerId = dto.AcceptedAnswerId;
            // post.Timestamp = DateTime.Now;
            //post.Tags = dto.Tags;

            if (post.Tags != dto.Tags)
            {
                await this.DeletePostsPostTags(post);
            }
            if (dto.Tags != null)
            {
                foreach (var t in dto.Tags)
                {
                    var tag = await _context.Tags.SingleOrDefaultAsync(tg => tg.Name == t.Name);

                    var pst = await _context.Posts.SingleOrDefaultAsync(p => p.Id == post.Id);

                    var newPostTag = new PostTag()
                    {
                        Post   = pst,
                        Tag    = tag,
                        PostId = pst.Id,
                        TagId  = tag.Id
                    };
                    _context.PostTags.Add(newPostTag);
                }
            }
            if (dto.Votes != null)
            {
                foreach (var v in dto.Votes)
                {
                    if (VoteAlreadyExists(v))
                    {
                        Console.WriteLine("CONSOLE: VoteAlreadyExists " + VoteAlreadyExists(v));
                        await this.DeleteVote(this.GetVoteFromVoteDTO(v));
                    }
                    var newVote = new Vote();
                    newVote.PostId   = post.Id;
                    newVote.AuthorId = v.AuthorId;
                    newVote.UpDown   = v.UpDown;
                    post.Votes.Add(newVote);
                    await _context.SaveChangesAsync();
                }
            }
            var res = await _context.SaveChangesAsyncWithValidation();

            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            return(NoContent());
        }