Пример #1
0
        public async Task <ActionResult <UserDTO> > PostUser(UserDTO data)
        {
            var user = await _context.Users.FindAsync(data.Id);

            if (user != null)
            {
                var err = new ValidationErrors().Add("Pseudo already in use", nameof(user.Pseudo));
                return(BadRequest(err));
            }

            var newUser = new User()
            {
                Id         = GetNewId(),
                Pseudo     = data.Pseudo,
                Email      = data.Email,
                Password   = TokenHelper.GetPasswordHash(data.Password),
                FirstName  = data.FirstName,
                LastName   = data.LastName,
                BirthDate  = data.BirthDate,
                Reputation = 0,
                Role       = data.Role
            };

            _context.Users.Add(newUser);

            var res = await _context.SaveChangesAsyncWithValidation();

            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            return(CreatedAtAction(nameof(GetUserById), new { Id = newUser.Id }, newUser.ToDTO()));
        }
Пример #2
0
        public async Task <ActionResult <CommentDTO> > PostComment(CommentDTO data)
        {
            var commentDto = await _context.Comments.FindAsync(data.Id);

            if (commentDto != null)
            {
                var err = new ValidationErrors().Add("Comment already in use", nameof(commentDto.Body));
                return(BadRequest(err));
            }

            var newComment = new Comment()
            {
                Id        = GetNewId(),
                Body      = data.Body,
                Timestamp = data.Timestamp,
                AuthorId  = data.AuthorId,
                PostId    = data.PostId,
            };

            Console.WriteLine("AuthorID: " + newComment.AuthorId);

            _context.Comments.Add(newComment);
            await _context.SaveChangesAsyncWithValidation();

            var res = await _context.SaveChangesAsyncWithValidation();

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

            return(CreatedAtAction(nameof(GetCmtById), new { Id = newComment.Id }, newComment.ToDTO()));
        }
Пример #3
0
        public async Task <ActionResult <PostDTO> > PostPost(PostDTO data)
        {
            var postDto = await _context.Posts.FindAsync(data.Id);

            if (postDto != null)
            {
                var err = new ValidationErrors().Add("Post already in use", nameof(postDto.Title));
                return(BadRequest(err));
            }

            var newPost = new Post()
            {
                Title            = data.Title,
                Body             = data.Body,
                Timestamp        = data.Timestamp,
                ParentId         = data.ParentId,
                AuthorId         = data.AuthorId,
                AcceptedAnswerId = data.AcceptedAnswerId,
            };

            _context.Posts.Add(newPost);
            await _context.SaveChangesAsyncWithValidation();

            if (data.Tags != null)
            {
                foreach (var t in data.Tags)
                {
                    var tag = await _context.Tags.SingleOrDefaultAsync(tg => tg.Name == t.Name);

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

                    var newPostTag = new PostTag()
                    {
                        Post   = post,
                        Tag    = tag,
                        PostId = post.Id,
                        TagId  = tag.Id
                    };

                    _context.PostTags.Add(newPostTag);
                }
            }

            var res = await _context.SaveChangesAsyncWithValidation();

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

            return(CreatedAtAction(nameof(GetPostById), new { Id = newPost.Id }, newPost.ToDTO()));
        }
Пример #4
0
        public async Task <ActionResult <TagDTO> > Post(TagDTO data)
        {
            var newTag = new Tag()
            {
                Id   = GetNewId(),
                Name = data.Name
            };

            _context.Tags.Add(newTag);
            var res = await _context.SaveChangesAsyncWithValidation();

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

            return(CreatedAtAction(nameof(GetTagById), new { Id = newTag.Id }, newTag.ToDTO()));
        }