public async Task <IActionResult> Post(PostApiModel apiModel) { var email = User.FindFirst(ct => ct.Type.Contains("nameidentifier")).Value; var user = await _userRepo.GetUserByEmailAsync(email); if (email.ToLower() == user.Email.ToLower()) { try { apiModel.User = ApiModelConverter.ToUserApiModel(user); var post = ApiModelConverter.ToPost(_userRepo, _commentRepo, apiModel); int result = await _postRepo.CreatePostAsync(post); if (result != -1) { // return Created() return(Ok()); } else { return(BadRequest()); } } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } } else { return(BadRequest()); } }
public async Task <ActionResult <PostReadDto> > CreatePostAsync(PostCreateDto post) { var postModel = _mapper.Map <Post>(post); await _repository.CreatePostAsync(postModel); await _repository.SaveChangesAsync(); var postReadDto = _mapper.Map <PostReadDto>(postModel); return(CreatedAtRoute(nameof(GetPostByIdAsync), new { Id = postReadDto.Id }, postReadDto)); // return Ok(postReadDto); }
public async Task <bool> CreatePostAsync(Post post) { return(await _repo.CreatePostAsync(post)); }