コード例 #1
0
        public async Task CreatePost([FromBody] PostForCreateDto postForCreateDto)
        {
            var post = new Post();

            post.CreatedOn = postForCreateDto.CreatedOn;
            post.UpdatedOn = postForCreateDto.CreatedOn;
            post.Title     = postForCreateDto.Title;
            post.Summary   = postForCreateDto.Summary;
            post.Likes     = 0;
            post.Comments  = new List <Comment>();
            post.Author    = postForCreateDto.Author;

            if (!string.IsNullOrWhiteSpace(postForCreateDto.Pic))
            {
                try
                {
                    var path = await _fileStorageService.SaveFile(Convert.FromBase64String(postForCreateDto.Pic), "png", "posts");

                    post.Pic = path;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message, ex);
                }
            }

            await _dbContext.AddAsync <Post>(post);

            await _dbContext.SaveChangesAsync();
        }
コード例 #2
0
        public async Task <IActionResult> AddPost([FromBody] PostForCreateDto postForCreateDto)
        {
            var content = postForCreateDto.Content;

            var post = _mapper.Map <Post>(postForCreateDto);

            _repository.Add(post);

            if (await _repository.SaveAll())
            {
                var postForList = _mapper.Map <PostForListDto>(post);
                return(CreatedAtRoute(nameof(GetPost), new { id = post.Id }, postForList));
            }
            return(BadRequest());
        }
コード例 #3
0
        public async Task <IActionResult> CreatePost(PostForCreateDto postForCreateDto)
        {
            var post = _mapper.Map <Post>(postForCreateDto);

            post.Created     = DateTime.Now;
            post.LastUpdated = post.Created;

            _repo.Add(post);
            var result = await _repo.SaveAll();

            if (result)
            {
                var postToReturn = _mapper.Map <PostForReturnDto>(post);
                return(Ok(postToReturn));
            }

            return(BadRequest("Failed to create post!"));
        }