Пример #1
0
        public async Task <ActionResult <PostDto> > AddPost(string userId, [FromForm] PostToAddDto postToAdd)
        {
            if (Guid.TryParse(userId, out Guid gUserId))
            {
                try
                {
                    if (await _userService.CheckIfUserExists(gUserId))
                    {
                        PostDto addedPost = await _postService.AddPostAsync(gUserId, postToAdd);

                        return(CreatedAtRoute("GetPost",
                                              new { userId, postId = addedPost.Id },
                                              addedPost));
                    }
                    else
                    {
                        return(NotFound($"User: {userId} not found."));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error occured during adding the user post. User id: {user}", userId);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            else
            {
                return(BadRequest($"{userId} is not valid guid."));
            }
        }
Пример #2
0
        public async Task <PostDto> AddPostAsync(Guid userId, PostToAddDto post)
        {
            _logger.LogDebug("Trying to add post {post}.", post);
            if (post == null)
            {
                throw new ArgumentNullException(nameof(post));
            }
            if (userId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            try
            {
                Post postToAdd = _mapper.Map <Post>(post);
                postToAdd.UserId = userId;

                if (post.Picture != null)
                {
                    (postToAdd.ImagePath, postToAdd.ImageFullPath) = await _imageManager.SaveImage(post.Picture);
                }

                Post addedPost = await _postRepository.AddAsync(postToAdd);

                await _postRepository.SaveAsync();

                return(_mapper.Map <PostDto>(addedPost));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error occured during adding the post.");
                throw;
            }
        }
Пример #3
0
        public async Task <ActionResult> addStrict(int userId, PostToAddDto postToAddDto)
        {
            if (postToAddDto.content == null)
            {
                return(BadRequest("there is no content"));
            }
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized("You are not the Author"));
            }

            Categorie categorie = await _postContext.getCatergorieById(postToAddDto.categorieId);

            Author author = new Author();

            author = await _postContext.GetAuthorByUserId(userId);

            if (author == null)
            {
                return(Unauthorized("You are not an author"));
            }
            if (categorie == null)
            {
                return(BadRequest("categorie doesn't exist"));
            }

            // await _postContext.add(postToAddDto.title,postToAddDto.content,author.id,categorie.id,postToAddDto.postTypeId,postToAddDto.imageUrl);
            if (!await _postContext.saveAll())
            {
                return(BadRequest("Couldn't add post"));
            }

            return(Ok("post added"));
        }
Пример #4
0
        public async Task <IActionResult> AddPost(int userid, PostToAddDto postToAddDto)
        {
            /*if(userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
             *  return Unauthorized();
             */
            postToAddDto.Video_link = postToAddDto.Video_link.Replace("watch?v=", "embed/");
            postToAddDto.AccountId  = userid;
            var post = _mapper.Map <Post>(postToAddDto);

            _repo.Add(post);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Adding music preference failed on save");
        }
Пример #5
0
        public async Task <ActionResult> add(int userId, [FromForm] PostToAddDto postToAddDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized("You are not the Author"));
            }
            int       postId    = 0;
            Categorie categorie = await _postContext.getCatergorieById(postToAddDto.categorieId);

            Author author = new Author();

            author = await _postContext.GetAuthorByUserId(userId);

            if (author == null)
            {
                return(Unauthorized("You are not an author"));
            }
            if (categorie == null)
            {
                return(BadRequest("categorie doesn't exist"));
            }
            PhotoRepo photoRepo = new PhotoRepo();

            if (postToAddDto.postTypeId == 1)
            {
                postId = await _postContext.add(postToAddDto.title, postToAddDto.content, author.id, categorie.id, postToAddDto.postTypeId, " ");
            }
            if (postToAddDto.postTypeId == 2)
            {
                Console.WriteLine(postToAddDto.image + "  f");

                string imageUrl = photoRepo.addPhoto(postToAddDto.image);

                postId = await _postContext.add(postToAddDto.title, " ", author.id, categorie.id, postToAddDto.postTypeId, imageUrl);
            }

            if (postId == 0)
            {
                return(BadRequest("didn't post"));
            }
            Console.WriteLine("postNumber : " + postId);
            return(Ok(new{ num = postId }));
        }
 public PostsControllerAddPostTests() : base()
 {
     _postToAdd = _fixture.Create <PostToAddDto>();
 }