Ejemplo n.º 1
0
        public async Task <GetPostDto> CreateOrUpdate(CreateOrUpdatePostDto input)
        {
            Post post = new Post()
            {
                Id      = input.Id,
                Caption = input.Caption,
                HostId  = input.HostId,
                Status  = PostStatus.Pending
            };

            if (input.Id == null || input.Id == "")
            {
                post.CreatedTime = DateTime.UtcNow;
                await _posts.InsertOneAsync(post);
            }
            else
            {
                var filter = Builders <Post> .Filter.Eq("Id", post.Id);

                post = await _posts.FindOneAndReplaceAsync(filter, post);

                if (post == null)
                {
                    throw new KeyNotFoundException("Không tồn tại bản ghi với Id được cung cấp");
                }
            }
            return(_mapper.Map <GetPostDto>(post));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateOrUpdate(CreateOrUpdatePostDto input)
        {
            try
            {
                var result = await _postService.CreateOrUpdate(input);

                return(Ok(result));
            }
            catch (KeyNotFoundException e)
            {
                return(StatusCode(400, e.Message));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }