-
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
Conversation
| val id: Long, val title: String, val writer: String, val text: String | ||
| ) { | ||
| val createDate: LocalDateTime = LocalDateTime.now() | ||
| var updateDate: LocalDateTime = LocalDateTime.now() |
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.
createDate, updateDate도 생성자에 넣는거 가능합니다!
코틀린에서는 default값 주입 가능해요
class Post(
val id: Long,
val title: String,
val writer: String,
val text: String,
val createDate: LocalDateTime = LocalDateTime.now()
var updateDate: LocalDateTime = LocalDateTime.now()
)
|
|
||
| ) { | ||
| companion object { | ||
| var posts: MutableList<Any> = mutableListOf() |
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.
posts를 바꿀 경우가 있나요? var로 선언이 되어있길래..
그리고 Any를 쓴 이유가 있을까요?
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.
val로 바꾸는게 맞는것 같네요.
모든 타입 다 가능하게끔 하려고 Any선언 했는데. 나름의 추상화...
해당 클래스 파일도 제네릭으로 하는게 맞았던거 같네요
| AbstractDatabase.create(post); | ||
| } | ||
|
|
||
| override fun find(): MutableList<Any> { |
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.
여기도 Any가 있네요..
| override fun updatePost(post: Post) { | ||
| var foundPost: Post = postRepository.findById(post.id) as Post | ||
| foundPost?.let { | ||
| postRepository.delete(post) |
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(it)이 더 올바른 표현처럼 보여요
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.
급하게 하고, 올리다보니 실수..
| } | ||
|
|
||
| @DeleteMapping(value = ["/board"]) | ||
| fun deletePost(@RequestBody post: Post): ResponseEntity<Any> { |
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지원 안하지 않나요? (잘 모름..)
| override fun deletePost(post: Post) { | ||
| var foundPost: Post = postRepository.findById(post.id) as Post | ||
| foundPost?.let { | ||
| postRepository.delete(post) |
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.
let을 null check만 하는 것은 좋지 않은걸로 알고있는데 제가 잘 알고있는지는... (https://tourspace.tistory.com/208)
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.
foundPost가 nullable 타입이 아니라서 safety call을 할 필요는 없을 것 같습니다.
mark가 의도하신 바가 JPA query method를 사용해서 못찾은 경우에 null check를 하려는 의도셨다면 Post -> Post? 타입으로 바꾸는게 좋을 것 같아요.
다니엘이 올려주신 예제가 아주 좋네요. 저도 널체크는 명시적으로 if( A == null )로 하는게 좀 더 직관적인 것 같습니다. 이건 개인차 인것 같아요~
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.
if로 하는게 맞을 것 같아요! 저도 급하게 하다보니 ?를.. 생략해버렸네요
| @@ -0,0 +1,26 @@ | |||
| package com.example.kotlinweb.board.model | |||
|
|
|||
| class AbstractDatabase( | |||
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.
JPA를 안쓰기 위해 별도의 싱글톤을 사용하신거군요 멋진 아이디어네요
| import org.springframework.boot.test.context.SpringBootTest | ||
| import org.springframework.test.context.junit.jupiter.SpringExtension | ||
|
|
||
| @ExtendWith(SpringExtension::class) |
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.
SpringBootTest에 포함되어있어서 JUnit 5에서는 생략해도됩니다~
|
Depoy가 몹니까 지금.. 커밋이 장난입니까? 엌 |
|
@mingimin |

코틀린 잘 못해여.. 리팩토링 가이드 부탁드려요 :)