Skip to content

Commit

Permalink
Merge pull request #21 from jojaeng2/feature/#13-Feed-CRUD-생성
Browse files Browse the repository at this point in the history
[피드] Post 단순 CRUD 생성
  • Loading branch information
jojaeng2 authored Sep 11, 2023
2 parents 684bfe4 + bab56ec commit 11909e2
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Void> {
val postId = postService.create(req.content)
return ResponseEntity.created(URI.create("/posts/${postId}")).build()
}

@GetMapping("/{id}")
fun findPost(@PathVariable id: Long): ResponseEntity<PostResponse> {
val post = postService.findOneById(id)
return ResponseEntity.ok(PostResponse(post))
}

@PutMapping("/{id}")
fun updatePost(@PathVariable id: Long, @RequestBody req: PostUpdateRequest): ResponseEntity<Void> {
postService.update(id, req.content)
return ResponseEntity.noContent().build()
}

@DeleteMapping("/{id}")
fun deletePost(@PathVariable id: Long): ResponseEntity<Void> {
postService.delete(id)
return ResponseEntity.noContent().build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package joryu.sns_service.post.dto.req

data class PostCreateRequest(
val content: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package joryu.sns_service.post.dto.req

data class PostUpdateRequest(
val content: String
)
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 11909e2

Please sign in to comment.