Skip to content
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

(#2) GABI #9

Open
wants to merge 1 commit into
base: review/kotlin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.kotlinweb.controller

import com.example.kotlinweb.db.Content
import com.example.kotlinweb.service.Service
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/v1")
class PostingController(
val service: Service
) {
@PostMapping("/post")
fun addPostToBlog(
@RequestBody content: Content
) {
service.addContent(content)
}

@GetMapping("/get")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제로랑 같은 내용입니다만 참고로 말씀드리면 서버개발할때 주로 RESTful(REST API)를 사용하는데 URI에 동사를 넣지 않아요..
https://gmlwjd9405.github.io/2018/09/21/rest-and-restful.html 이것 참고해보세용..ㅎㅎ

한번 RESTful에 대해서 누군가 설명을 해봐야할 것 같아보이네요!

fun getPostedBlogList(): List<Content> {
return service.blogDB.blogList
}

@PutMapping("/update")
fun updateBlogList(
@RequestBody content: Content
) {
service.updateContent(content)
}

@DeleteMapping("/delete")
fun deletePostedBlogList(
@RequestBody id: Int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DELETE 메소드는 Body를 사용하는 것이 좋지 않은 방법으로 알고 있습니다.(사실 안되는걸로 알고있음) 대안으로 @PathVariable를 사용하는 것이 좋을 것 같습니다!

) {
service.deleteContent(id)
}

}
9 changes: 9 additions & 0 deletions src/main/kotlin/com/example/kotlinweb/db/BlogDB.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.kotlinweb.db

import org.springframework.stereotype.Component

@Component
class BlogDB {
val blogList : MutableList<Content> = mutableListOf()

}
10 changes: 10 additions & 0 deletions src/main/kotlin/com/example/kotlinweb/db/Content.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.kotlinweb.db

class Content (
val id :Int,
val title : String,
val content : String,
val date : String
){

}
23 changes: 23 additions & 0 deletions src/main/kotlin/com/example/kotlinweb/service/Service.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.kotlinweb.service

import com.example.kotlinweb.db.BlogDB
import com.example.kotlinweb.db.Content
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class Service @Autowired constructor(
val blogDB: BlogDB
) {
fun addContent(content: Content) {
blogDB.blogList.add(content)
}
fun updateContent(content: Content){
val updateItem = blogDB.blogList.filter { it.id == content.id }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아직 덜 짠거겠죠..??

// updateItem = content
}
fun deleteContent(id : Int){
val deleteItem = blogDB.blogList.filter { it.id == id }
Copy link
Member

@minkukjo minkukjo Aug 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 Service에서 blodgDB에 대한 로직처리를 하고 있는데, 이는 추후에 blogDB에 대한 로직에 변경이 발생했을 때 이를 사용하는 다른 Service 객체에 모든 변경사항을 반영해주어야해서 유지보수가 어렵습니다.

객체지향 관점에서 봤을 때 유지보수하기 쉬운 코드를 짜려면 데이터에 대한 로직은 blogDB가 갖게 하는게 어떨까 싶습니다.

예시 코드를 아래에 남겨둘게요.

@Service
class Service @Autowired constructor(
        val blogDB: BlogDB
) {
    fun deleteContent(id : Int){
        blogDB.remove(id)
    }
}
@Component
class BlogDB {
    val blogList : MutableList<Content> = mutableListOf()

    fun remove(id: Int) {
        this.blogList.removeIf{ it.id == id}
    }

}

blogDB.blogList.remove(deleteItem)
}
}