-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : 3차 세미나 실습 과제_post 생성 API 추가 #7
- Loading branch information
1 parent
0978fe5
commit 4e6bf73
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
week02/seminar/demo/src/main/java/com/example/demo/controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.example.demo.controller; | ||
|
||
|
||
import com.example.demo.common.dto.SuccessMessage; | ||
import com.example.demo.common.dto.SuccessStatusResponse; | ||
import com.example.demo.domain.Blog; | ||
import com.example.demo.service.MemberService; | ||
import com.example.demo.service.PostService; | ||
import com.example.demo.service.dto.blog.BlogCreateRequest; | ||
import com.example.demo.service.dto.member.MemberCreateDto; | ||
import com.example.demo.service.dto.post.PostCreateRequest; | ||
import jakarta.validation.Valid; | ||
import java.net.URI; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1") // 버전1의 api를 만든다. | ||
public class PostController { | ||
|
||
private final PostService postService; | ||
|
||
@PostMapping("post/{blogId}") | ||
public ResponseEntity<SuccessStatusResponse> publishPost( | ||
@RequestHeader(name = "memberId") Long memberId, | ||
@PathVariable(name = "blogId") Long blogId, | ||
@Valid @RequestBody PostCreateRequest postCreateRequest | ||
) { | ||
|
||
|
||
// Location은 생성된 리소스의 위치를 나타낸다. 이 코드의 경우 생성된 post의 id | ||
return ResponseEntity.status(HttpStatus.CREATED).header( | ||
"Location", | ||
postService.create(memberId, blogId, postCreateRequest)) | ||
.body(SuccessStatusResponse.of(SuccessMessage.POST_CREATE_SUCCESS)); | ||
} | ||
|
||
} |