public async Task <ActionResult <CommentDTO> > PostComment(CommentDTO data)
        {
            var currentUser = await _context.Users.Where(u => u.Pseudo == User.Identity.Name).SingleOrDefaultAsync();

            //Création d'un object comment depuis le DTO
            var newComment = new Comment()
            {
                Body   = data.Body,
                UserId = currentUser.Id,
                PostId = data.PostId
            };

            //Ajout du comment dans le context
            _context.Comments.Add(newComment);
            //Sauvegarde de l'ajout
            var res = await _context.SaveChangesAsyncWithValidation();

            //Contrôle de la sauvegarde
            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            //return NoContent();
            return(CreatedAtAction(nameof(Getone), new { userId = newComment.UserId, postId = newComment.PostId }, newComment.ToDTO()));
        }
Example #2
0
        public async Task <ActionResult <UserDTO> > PostTag(TagDTO data)
        {
            var newTag = data.ToOBJ();

            _context.Tags.Add(newTag);

            var res = await _context.SaveChangesAsyncWithValidation();

            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            return(CreatedAtAction(nameof(GetOne), new { name = newTag.Name }, newTag.ToDTO()));
        }
Example #3
0
        public async Task <ActionResult <UserDTO> > PostUser(UserDTO data)
        {
            var newUser        = data.ToOBJ();
            var passwordLength = data.Password.Length;

            if (!(passwordLength >= 3 && passwordLength <= 10))
            {
                var err = new ValidationErrors().Add("Remark must have min length of 3 and max Length of 10", nameof(newUser.Password));
                return(BadRequest(err));
            }

            _context.Users.Add(newUser);
            var res = await _context.SaveChangesAsyncWithValidation();

            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            return(CreatedAtAction(nameof(GetOne), new { pseudo = newUser.Pseudo }, newUser.ToDTO()));
        }
Example #4
0
        public async Task <ActionResult <UserDTO> > PostVote(VoteDTO data)
        {
            var connectedUser = await _context.Users.Where(u => u.Pseudo == User.Identity.Name).SingleOrDefaultAsync();

            var existVote = await _context.Votes.Where(v => v.PostId == data.PostId && v.UserId == connectedUser.Id).SingleOrDefaultAsync();

            if (existVote != null)
            {
                return(BadRequest());
            }
            var vote = new Vote
            {
                UpDown = data.UpDown,
                UserId = connectedUser.Id,
                PostId = data.PostId
            };

            _context.Votes.Add(vote);
            var res = await _context.SaveChangesAsyncWithValidation();

            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            //recuperation du nouveau vote en DB
            var newVote = await _context.Votes.Where(v => v.PostId == data.PostId && v.UserId == connectedUser.Id).SingleOrDefaultAsync();

            //recuperation du l'user du post
            var poster = await _context.Posts.FindAsync(data.PostId);

            var user = poster.User;

            setReputation(newVote, user, connectedUser);
            res = await _context.SaveChangesAsyncWithValidation();

            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            return(NoContent());
        }
Example #5
0
        public async Task <IActionResult> PostPost(PostDTO data)
        {
            var user = await _context.Users.Where(u => u.Pseudo == User.Identity.Name).SingleOrDefaultAsync();

            var newPost = data.ToOBJ();

            newPost.UserId = user.Id;
            _context.Posts.Add(newPost);

            foreach (var t in data.Tags)
            {
                var tag = new PostTag();
                tag.TagId = t.Id;
                newPost.PostTags.Add(tag);
            }
            var res = await _context.SaveChangesAsyncWithValidation();

            if (!res.IsEmpty)
            {
                return(BadRequest(res));
            }
            //return CreatedAtAction(nameof(GetQuestionByBody), new { Body = newPost.Body }, newPost.ToDTO());
            return(NoContent());
        }