-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[피드] Post 단순 CRUD 생성
- Loading branch information
Showing
6 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
sns_service/src/main/kotlin/joryu/sns_service/post/controller/PostController.kt
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 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() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostCreateRequest.kt
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,5 @@ | ||
package joryu.sns_service.post.dto.req | ||
|
||
data class PostCreateRequest( | ||
val content: String | ||
) |
5 changes: 5 additions & 0 deletions
5
sns_service/src/main/kotlin/joryu/sns_service/post/dto/req/PostUpdateRequest.kt
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,5 @@ | ||
package joryu.sns_service.post.dto.req | ||
|
||
data class PostUpdateRequest( | ||
val content: String | ||
) |
15 changes: 15 additions & 0 deletions
15
sns_service/src/main/kotlin/joryu/sns_service/post/dto/resp/PostResponse.kt
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,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 | ||
) | ||
} |
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
32 changes: 32 additions & 0 deletions
32
sns_service/src/main/kotlin/joryu/sns_service/post/service/PostService.kt
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,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) | ||
} | ||
} |