-
Notifications
You must be signed in to change notification settings - Fork 0
CRUD 완성 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: review/kotlin
Are you sure you want to change the base?
CRUD 완성 #5
Changes from 1 commit
0e4a4db
66b2bf3
1d084ca
5b3b4b4
96c155d
16aa5d6
37e1c3d
19d786a
edd5c5e
3466e73
b771d92
fc5c2ed
4a2f321
27c34d5
bccf0de
40ea599
b7af788
464b465
02a3ef6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.example.kotlinweb.board.controller | ||
| import com.example.kotlinweb.board.model.Post | ||
| import com.example.kotlinweb.board.service.BoardService | ||
| import org.springframework.http.HttpStatus | ||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.web.bind.annotation.* | ||
|
|
||
| @RestController | ||
| class BoardController(private val boardService: BoardService) { | ||
|
|
||
| @GetMapping(value = ["/board"]) | ||
| fun findAllPosts(): ResponseEntity<Any> {; | ||
| return ResponseEntity(boardService.findPosts(), HttpStatus.OK); | ||
| } | ||
|
|
||
| @PostMapping(value = ["/board"]) | ||
| fun savePost(@RequestBody post: Post): ResponseEntity<Any> { | ||
| return ResponseEntity(boardService.savePost(post), HttpStatus.CREATED); | ||
| } | ||
|
|
||
| @PutMapping(value = ["/board/{id}"]) | ||
| fun updatePost(@RequestBody post: Post, @PathVariable id: String): ResponseEntity<Any> { | ||
| return ResponseEntity(boardService.updatePost(post), HttpStatus.OK); | ||
| } | ||
|
|
||
| @DeleteMapping(value = ["/board"]) | ||
| fun deletePost(@RequestBody post: Post): ResponseEntity<Any> { | ||
| return ResponseEntity(boardService.deletePost(post), HttpStatus.OK); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.kotlinweb.board.model | ||
|
|
||
| class AbstractDatabase( | ||
|
||
|
|
||
| ) { | ||
| companion object { | ||
| var posts: MutableList<Any> = mutableListOf() | ||
|
||
|
|
||
| fun create(obj: Any?) { | ||
| obj?.let { | ||
| posts.add(obj) | ||
| } | ||
| } | ||
|
|
||
| fun delete(obj: Any?) { | ||
| obj?.let { | ||
| obj as Post | ||
| posts.removeAt(obj.id.toInt() - 1) | ||
| } | ||
| } | ||
|
|
||
| fun read(): MutableList<Any> { | ||
| return posts; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.kotlinweb.board.model | ||
|
|
||
| import java.time.LocalDateTime | ||
|
|
||
| class Post( | ||
| val id: Long, val title: String, val writer: String, val text: String | ||
| ) { | ||
| val createDate: LocalDateTime = LocalDateTime.now() | ||
| var updateDate: LocalDateTime = LocalDateTime.now() | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.example.kotlinweb.board.repository | ||
|
|
||
| import com.example.kotlinweb.board.model.AbstractDatabase | ||
| import com.example.kotlinweb.board.model.Post | ||
| import org.springframework.stereotype.Repository | ||
|
|
||
| @Repository | ||
| class PostCrudRepository : PostRepository<Post> { | ||
|
|
||
| override fun <Post> save(post: Post) { | ||
| AbstractDatabase.create(post); | ||
| } | ||
|
|
||
| override fun find(): MutableList<Any> { | ||
|
||
| return AbstractDatabase.read() | ||
| } | ||
|
|
||
| override fun findById(postId: Long): Post { | ||
| return find().map { any -> any as Post }.first { post -> post.id == postId } | ||
| } | ||
|
|
||
| override fun <Post> delete(post: Post) { | ||
| return AbstractDatabase.delete(post) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.kotlinweb.board.repository | ||
|
|
||
| interface PostRepository<T> { | ||
| fun <T> save(t: T) | ||
| fun find(): Any | ||
| fun findById(t: Long): Any | ||
| fun <T> delete(t: T) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.kotlinweb.board.service | ||
|
|
||
| import com.example.kotlinweb.board.model.Post | ||
|
|
||
| interface BoardService { | ||
| fun savePost(post: Post) | ||
| fun findPosts(): Any | ||
| fun updatePost(post: Post) | ||
| fun deletePost(post: Post) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.example.kotlinweb.board.service | ||
|
|
||
| import com.example.kotlinweb.board.model.Post | ||
| import com.example.kotlinweb.board.repository.PostRepository | ||
| import org.springframework.stereotype.Service | ||
|
|
||
| @Service | ||
| class BoardServiceImpl(private val postRepository: PostRepository<Post>) : BoardService { | ||
|
|
||
| override fun savePost(post: Post) { | ||
| postRepository.save(post) | ||
| } | ||
|
|
||
| override fun findPosts(): Any { | ||
| return postRepository.find() | ||
| } | ||
|
|
||
| override fun updatePost(post: Post) { | ||
| var foundPost: Post = postRepository.findById(post.id) as Post | ||
| foundPost?.let { | ||
| postRepository.delete(post) | ||
|
||
| postRepository.save(post) | ||
| } | ||
| } | ||
|
|
||
| override fun deletePost(post: Post) { | ||
| var foundPost: Post = postRepository.findById(post.id) as Post | ||
| foundPost?.let { | ||
| postRepository.delete(post) | ||
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.example.kotlinweb.board.service | ||
|
|
||
| import com.example.kotlinweb.board.model.AbstractDatabase | ||
| import com.example.kotlinweb.board.model.Post | ||
| import com.example.kotlinweb.board.repository.PostCrudRepository | ||
| import org.junit.jupiter.api.Assertions | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.assertAll | ||
| import org.junit.jupiter.api.extension.ExtendWith | ||
| import org.mockito.Mockito.times | ||
| import org.mockito.Mockito.verify | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.boot.test.context.SpringBootTest | ||
| import org.springframework.test.context.junit.jupiter.SpringExtension | ||
|
|
||
| @ExtendWith(SpringExtension::class) | ||
|
||
| @SpringBootTest | ||
| internal class BoardServiceImplTest( | ||
| @Autowired val repository: PostCrudRepository | ||
| ) { | ||
|
|
||
| @Test | ||
| fun savePostTest() { | ||
| val post: Post = Post(1L, "Test Title", "KDH", "memo") | ||
| val boardService: BoardService = BoardServiceImpl(repository) | ||
| boardService.savePost(post) | ||
| verify(AbstractDatabase, times(1)).create(Any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun readPostsTest() { | ||
| val post1: Post = Post(1L, "Test Title", "KDH", "memo1") | ||
| val post2: Post = Post(2L, "Test Title2", "KDH2", "memo2") | ||
| val boardService: BoardService = BoardServiceImpl(repository) | ||
| boardService.savePost(post1) | ||
| boardService.savePost(post2) | ||
| var posts: MutableList<Any> = AbstractDatabase.read() | ||
| val savedPost1: Post = posts[0] as Post | ||
| val savedPost2: Post = posts[1] as Post | ||
| assertAll( | ||
| "posts", | ||
| { Assertions.assertEquals(post1.title, savedPost1.title) }, | ||
| { Assertions.assertEquals(post2.writer, savedPost2.writer) } | ||
| ) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
근데 delete는 body지원 안하지 않나요? (잘 모름..)