From bab56ec0d917ae8a3516740f7359bdd83c81dc4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=ED=95=B8=EB=AA=A8?= Date: Fri, 1 Sep 2023 23:40:22 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Post=20=EB=8B=A8=EC=88=9C=20CRUD=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/controller/PostController.kt | 46 +++++++++++++++++++ .../post/dto/req/PostCreateRequest.kt | 5 ++ .../post/dto/req/PostUpdateRequest.kt | 5 ++ .../sns_service/post/dto/resp/PostResponse.kt | 15 ++++++ .../joryu/sns_service/post/entity/Post.kt | 4 ++ .../sns_service/post/service/PostService.kt | 32 +++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/post/controller/PostController.kt create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostCreateRequest.kt create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostUpdateRequest.kt create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/post/dto/resp/PostResponse.kt create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/post/service/PostService.kt diff --git a/sns_service/src/main/kotlin/joryu/sns_service/post/controller/PostController.kt b/sns_service/src/main/kotlin/joryu/sns_service/post/controller/PostController.kt new file mode 100644 index 0000000..eaef1a2 --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/post/controller/PostController.kt @@ -0,0 +1,46 @@ +package joryu.sns_service.post.controller + +import joryu.sns_service.post.dto.req.PostCreateRequest +import joryu.sns_service.post.dto.req.PostUpdateRequest +import joryu.sns_service.post.dto.resp.PostResponse +import joryu.sns_service.post.service.PostService +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.PutMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController +import java.net.URI + +@RestController +@RequestMapping("/posts") +class PostController( + private val postService: PostService +) { + @PostMapping + fun createPost(@RequestBody req: PostCreateRequest): ResponseEntity { + val postId = postService.create(req.content) + return ResponseEntity.created(URI.create("/posts/${postId}")).build() + } + + @GetMapping("/{id}") + fun findPost(@PathVariable id: Long): ResponseEntity { + val post = postService.findOneById(id) + return ResponseEntity.ok(PostResponse(post)) + } + + @PutMapping("/{id}") + fun updatePost(@PathVariable id: Long, @RequestBody req: PostUpdateRequest): ResponseEntity { + postService.update(id, req.content) + return ResponseEntity.noContent().build() + } + + @DeleteMapping("/{id}") + fun deletePost(@PathVariable id: Long): ResponseEntity { + postService.delete(id) + return ResponseEntity.noContent().build() + } +} diff --git a/sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostCreateRequest.kt b/sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostCreateRequest.kt new file mode 100644 index 0000000..555a9e8 --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostCreateRequest.kt @@ -0,0 +1,5 @@ +package joryu.sns_service.post.dto.req + +data class PostCreateRequest( + val content: String +) diff --git a/sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostUpdateRequest.kt b/sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostUpdateRequest.kt new file mode 100644 index 0000000..3b6cf5a --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostUpdateRequest.kt @@ -0,0 +1,5 @@ +package joryu.sns_service.post.dto.req + +data class PostUpdateRequest( + val content: String +) diff --git a/sns_service/src/main/kotlin/joryu/sns_service/post/dto/resp/PostResponse.kt b/sns_service/src/main/kotlin/joryu/sns_service/post/dto/resp/PostResponse.kt new file mode 100644 index 0000000..0dcb064 --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/post/dto/resp/PostResponse.kt @@ -0,0 +1,15 @@ +package joryu.sns_service.post.dto.resp + +import joryu.sns_service.post.entity.Post + +data class PostResponse( + val id: Long, + val content: String, + val viewCount: Long, +) { + constructor(post: Post) : this( + id = post.id, + content = post.content, + viewCount = post.viewCount + ) +} diff --git a/sns_service/src/main/kotlin/joryu/sns_service/post/entity/Post.kt b/sns_service/src/main/kotlin/joryu/sns_service/post/entity/Post.kt index e57acab..81e1139 100644 --- a/sns_service/src/main/kotlin/joryu/sns_service/post/entity/Post.kt +++ b/sns_service/src/main/kotlin/joryu/sns_service/post/entity/Post.kt @@ -26,4 +26,8 @@ class Post( @Column(name = "content", nullable = false, length = 1000) var content: String = content private set + + fun changeContent(content: String) { + this.content = content + } } diff --git a/sns_service/src/main/kotlin/joryu/sns_service/post/service/PostService.kt b/sns_service/src/main/kotlin/joryu/sns_service/post/service/PostService.kt new file mode 100644 index 0000000..84088c5 --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/post/service/PostService.kt @@ -0,0 +1,32 @@ +package joryu.sns_service.post.service + +import joryu.sns_service.post.entity.Post +import joryu.sns_service.post.repository.PostRepository +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +@Transactional(readOnly = true) +class PostService( + val postRepository: PostRepository +) { + @Transactional + fun create(content: String): Long { + return postRepository.save(Post(content)).id + } + + fun findOneById(id: Long): Post { + return postRepository.findById(id).orElseThrow() + } + + @Transactional + fun update(id: Long, newContent: String) { + val postForUpdate = postRepository.findById(id).orElseThrow() + postForUpdate.changeContent(newContent) + } + + @Transactional + fun delete(id: Long) { + postRepository.deleteById(id) + } +}