Пример #1
0
        public async Task <IActionResult> Create([FromBody] CreatePostRequest postRequest)
        {
            var post = new Post
            {
                Name   = postRequest.Name,
                UserId = HttpContext.GetUserId()
            };

            var created = await _postService.CreatePostAsync(post);

            if (!created)
            {
                return(BadRequest(new ErrorResponse {
                    Errors = new List <ErrorModel> {
                        new ErrorModel {
                            Message = "Unable to create post"
                        }
                    }
                }));
            }

            var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
            var locationUri = baseUrl + "/" + ApiRoutes.Posts.Get.Replace("{postId}", post.Id.ToString()); //user bunu nereden retrieve eder

            var postResponse = new PostResponse {
                Id = post.Id
            };

            return(Created(locationUri, postResponse));
        }
Пример #2
0
        public async Task <IActionResult> Create([FromBody] CreatePostRequest post)
        {
            var newPost = new Post(post.Name);

            var createdPost = await _postServices.CreatePostAsync(newPost);

            var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
            var locationUri = baseUrl + "/" + ApiRoutes.Posts.Get.Replace("{postId}", createdPost.Id.ToString());

            var postResponse = new CreatePostResponse()
            {
                Id = createdPost.Id, Name = post.Name
            };

            return(Created(locationUri, postResponse));
        }