From a6d0ac53557d0c46bde72068c9972b18e24fb53e Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 12:36:56 +0900 Subject: [PATCH 01/14] =?UTF-8?q?=EB=83=89=EC=9E=A5=EA=B3=A0=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8(refrigerator)=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20=EC=B5=9C=EC=B4=88=20=EC=97=94=ED=8B=B0=ED=8B=B0=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/refrigerator/Refrigerator.kt | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt new file mode 100644 index 0000000..e0c54f9 --- /dev/null +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -0,0 +1,17 @@ +package mara.server.domain.refrigerator + +import jakarta.persistence.* +import mara.server.domain.user.User + +@Entity +class Refrigerator( + var name: String = "기본 냉장고", + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "userId") + val user: User, +) { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "refrigerator_id", nullable = false) + var refrigeratorId: Long = 0L +} \ No newline at end of file From 18c3ae34857209b3c26bddf7e7fb2ce4abd84724 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 12:40:32 +0900 Subject: [PATCH 02/14] =?UTF-8?q?refrigerator=20controller=20=EA=BB=8D?= =?UTF-8?q?=EB=8D=B0=EA=B8=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../refrigerator/RefrigeratorController.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt new file mode 100644 index 0000000..2714d29 --- /dev/null +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -0,0 +1,16 @@ +package mara.server.domain.refrigerator + +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/refrigs") +class RefrigeratorController() { + + @GetMapping + fun getRefrigerator(): String { + return "ok" + } +} \ No newline at end of file From e195ec8d9cd5b10da18433197f9d02c7b8a7cb08 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 12:43:37 +0900 Subject: [PATCH 03/14] =?UTF-8?q?refrigerator=20service=20=EA=BB=8D?= =?UTF-8?q?=EB=8D=B0=EA=B8=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/refrigerator/RefrigeratorController.kt | 7 ++++--- .../server/domain/refrigerator/RefrigeratorService.kt | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index 2714d29..7086751 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -1,16 +1,17 @@ package mara.server.domain.refrigerator import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/refrigs") -class RefrigeratorController() { +class RefrigeratorController( + private val refrigeratorService: RefrigeratorService +) { @GetMapping fun getRefrigerator(): String { - return "ok" + return refrigeratorService.getBoard(1) } } \ No newline at end of file diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt new file mode 100644 index 0000000..b5ae515 --- /dev/null +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -0,0 +1,11 @@ +package mara.server.domain.refrigerator + +import org.springframework.stereotype.Service + +@Service +class RefrigeratorService() { + + fun getBoard(id: Long): String { + return "service ok" + } +} \ No newline at end of file From 281b8f9245d65e71a54dde796b5d6628bded7614 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 12:50:38 +0900 Subject: [PATCH 04/14] =?UTF-8?q?refrigerator=20repository=20=EA=BB=8D?= =?UTF-8?q?=EB=8D=B0=EA=B8=B0=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EB=B0=9C=EC=83=9D=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/refrigerator/RefrigeratorRepository.kt | 8 ++++++++ .../server/domain/refrigerator/RefrigeratorService.kt | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt new file mode 100644 index 0000000..18162a3 --- /dev/null +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt @@ -0,0 +1,8 @@ +package mara.server.domain.refrigerator + +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +@Repository +interface RefrigeratorRepository : JpaRepository { +} \ No newline at end of file diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index b5ae515..a553865 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -3,9 +3,13 @@ package mara.server.domain.refrigerator import org.springframework.stereotype.Service @Service -class RefrigeratorService() { +class RefrigeratorService( + private val refrigeratorRepository: RefrigeratorRepository +) { fun getBoard(id: Long): String { + val refrigerator = + refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("냉장고가 존재하지 않습니다. ID: $id") } return "service ok" } } \ No newline at end of file From 1adce8cb42c862fbdc0af458a94be5884241793e Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 13:31:56 +0900 Subject: [PATCH 05/14] =?UTF-8?q?refrigerator=20Request=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B0=8F=20=EB=83=89=EC=9E=A5=EA=B3=A0=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/refrigerator/Refrigerator.kt | 5 +++-- .../refrigerator/RefrigeratorController.kt | 10 ++++++---- .../domain/refrigerator/RefrigeratorRequest.kt | 6 ++++++ .../domain/refrigerator/RefrigeratorService.kt | 16 ++++++++++++++-- 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt index e0c54f9..6fbe5bd 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -5,11 +5,12 @@ import mara.server.domain.user.User @Entity class Refrigerator( - var name: String = "기본 냉장고", + var name: String? = null, @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "userId") val user: User, -) { + + ) { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "refrigerator_id", nullable = false) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index 7086751..13c0754 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -1,17 +1,19 @@ package mara.server.domain.refrigerator -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RestController +import org.springframework.web.bind.annotation.* @RestController @RequestMapping("/refrigs") class RefrigeratorController( private val refrigeratorService: RefrigeratorService ) { + @PostMapping + fun createRefrigerator(@RequestBody refrigeratorRequest: RefrigeratorRequest): Long { + return refrigeratorService.createRefrigerator(refrigeratorRequest) + } @GetMapping fun getRefrigerator(): String { - return refrigeratorService.getBoard(1) + return refrigeratorService.getRefrigerator(1) } } \ No newline at end of file diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt new file mode 100644 index 0000000..88ecd16 --- /dev/null +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt @@ -0,0 +1,6 @@ +package mara.server.domain.refrigerator + +data class RefrigeratorRequest( + var name: String, + val userId: Long +) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index a553865..3b2e954 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -1,13 +1,25 @@ package mara.server.domain.refrigerator +import mara.server.domain.user.UserRepository import org.springframework.stereotype.Service @Service class RefrigeratorService( - private val refrigeratorRepository: RefrigeratorRepository + private val refrigeratorRepository: RefrigeratorRepository, + private val userRepository: UserRepository ) { + fun createRefrigerator(refrigeratorRequest: RefrigeratorRequest): Long { + val userId = refrigeratorRequest.userId + val user = + userRepository.findById(userId).orElseThrow { NoSuchElementException("해당 유저가 존재하지 않습니다. ID: $userId") } + val refrigerator = Refrigerator( + name = refrigeratorRequest.name, + user = user + ) + return refrigeratorRepository.save(refrigerator).refrigeratorId + } - fun getBoard(id: Long): String { + fun getRefrigerator(id: Long): String { val refrigerator = refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("냉장고가 존재하지 않습니다. ID: $id") } return "service ok" From 1e6015dfb51ab2feb0ac5e46d8a57678eff0725d Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 16:21:10 +0900 Subject: [PATCH 06/14] =?UTF-8?q?=EB=83=89=EC=9E=A5=EA=B3=A0=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/refrigerator/RefrigeratorController.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index 13c0754..3c4ff26 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -12,8 +12,8 @@ class RefrigeratorController( return refrigeratorService.createRefrigerator(refrigeratorRequest) } - @GetMapping - fun getRefrigerator(): String { - return refrigeratorService.getRefrigerator(1) + @GetMapping("/{id}") + fun getRefrigerator(@PathVariable(name = "id") id: Long): String { + return refrigeratorService.getRefrigerator((id)) } } \ No newline at end of file From 7b0cd75286a354736be40c424957766b2589f515 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 16:55:19 +0900 Subject: [PATCH 07/14] =?UTF-8?q?=EB=83=89=EC=9E=A5=EA=B3=A0=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mara/server/domain/refrigerator/Refrigerator.kt | 7 ++++++- .../domain/refrigerator/RefrigeratorController.kt | 7 +++++++ .../domain/refrigerator/RefrigeratorResponse.kt | 11 +++++++++++ .../server/domain/refrigerator/RefrigeratorService.kt | 7 +++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt index 6fbe5bd..ec7f607 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -1,11 +1,12 @@ package mara.server.domain.refrigerator +import com.fasterxml.jackson.annotation.JsonIgnore import jakarta.persistence.* import mara.server.domain.user.User @Entity class Refrigerator( - var name: String? = null, + var name: String, @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "userId") val user: User, @@ -15,4 +16,8 @@ class Refrigerator( @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "refrigerator_id", nullable = false) var refrigeratorId: Long = 0L + + fun update(refrigeratorRequest: RefrigeratorRequest) { + this.name = refrigeratorRequest.name + } } \ No newline at end of file diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index 3c4ff26..2297a1e 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -1,5 +1,7 @@ package mara.server.domain.refrigerator +import mara.server.common.CommonResponse +import mara.server.common.success import org.springframework.web.bind.annotation.* @RestController @@ -16,4 +18,9 @@ class RefrigeratorController( fun getRefrigerator(@PathVariable(name = "id") id: Long): String { return refrigeratorService.getRefrigerator((id)) } + + @PutMapping("/{id}") + fun updateRefrigerator(@PathVariable(name = "id") id: Long, @RequestBody refrigeratorRequest: RefrigeratorRequest): CommonResponse { + return success(refrigeratorService.updateRefrigerator(id, refrigeratorRequest)) + } } \ No newline at end of file diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt new file mode 100644 index 0000000..8ca0b67 --- /dev/null +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt @@ -0,0 +1,11 @@ +package mara.server.domain.refrigerator + +data class RefrigeratorResponse( + var id: Long, + var name: String, +) { + constructor(refrigerator: Refrigerator) : this( + id = refrigerator.refrigeratorId, + name = refrigerator.name + ) +} \ No newline at end of file diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index 3b2e954..fe27653 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -24,4 +24,11 @@ class RefrigeratorService( refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("냉장고가 존재하지 않습니다. ID: $id") } return "service ok" } + + fun updateRefrigerator(id: Long, refrigeratorRequest: RefrigeratorRequest): RefrigeratorResponse { + val refrigerator = + refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("냉장고가 존재하지 않습니다. ID: $id") } + refrigerator.update(refrigeratorRequest) + return RefrigeratorResponse(refrigeratorRepository.save(refrigerator)) + } } \ No newline at end of file From ec3802c82b769772842b68bc73ebad76dedd4c4f Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 16:57:51 +0900 Subject: [PATCH 08/14] =?UTF-8?q?=EB=83=89=EC=9E=A5=EA=B3=A0=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/refrigerator/RefrigeratorController.kt | 5 +++++ .../mara/server/domain/refrigerator/RefrigeratorService.kt | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index 2297a1e..aae13bb 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -23,4 +23,9 @@ class RefrigeratorController( fun updateRefrigerator(@PathVariable(name = "id") id: Long, @RequestBody refrigeratorRequest: RefrigeratorRequest): CommonResponse { return success(refrigeratorService.updateRefrigerator(id, refrigeratorRequest)) } + + @DeleteMapping("/{id}") + fun updateRefrigerator(@PathVariable(name = "id") id: Long): CommonResponse { + return success(refrigeratorService.deleteRefrigerator(id)) + } } \ No newline at end of file diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index fe27653..a03030e 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -31,4 +31,9 @@ class RefrigeratorService( refrigerator.update(refrigeratorRequest) return RefrigeratorResponse(refrigeratorRepository.save(refrigerator)) } + + fun deleteRefrigerator(id: Long): String { + refrigeratorRepository.deleteById(id) + return "deleted" + } } \ No newline at end of file From 24ff519d033bc1e509d5f93588b84eeb6c960e1b Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 17:05:29 +0900 Subject: [PATCH 09/14] =?UTF-8?q?CommonResponse=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mara/server/domain/refrigerator/Refrigerator.kt | 1 - .../domain/refrigerator/RefrigeratorController.kt | 13 ++++++++----- .../domain/refrigerator/RefrigeratorService.kt | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt index ec7f607..1a3be89 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -1,6 +1,5 @@ package mara.server.domain.refrigerator -import com.fasterxml.jackson.annotation.JsonIgnore import jakarta.persistence.* import mara.server.domain.user.User diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index aae13bb..ccb5f81 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -10,17 +10,20 @@ class RefrigeratorController( private val refrigeratorService: RefrigeratorService ) { @PostMapping - fun createRefrigerator(@RequestBody refrigeratorRequest: RefrigeratorRequest): Long { - return refrigeratorService.createRefrigerator(refrigeratorRequest) + fun createRefrigerator(@RequestBody refrigeratorRequest: RefrigeratorRequest): CommonResponse { + return success(refrigeratorService.createRefrigerator(refrigeratorRequest)) } @GetMapping("/{id}") - fun getRefrigerator(@PathVariable(name = "id") id: Long): String { - return refrigeratorService.getRefrigerator((id)) + fun getRefrigerator(@PathVariable(name = "id") id: Long): CommonResponse { + return success(refrigeratorService.getRefrigerator((id))) } @PutMapping("/{id}") - fun updateRefrigerator(@PathVariable(name = "id") id: Long, @RequestBody refrigeratorRequest: RefrigeratorRequest): CommonResponse { + fun updateRefrigerator( + @PathVariable(name = "id") id: Long, + @RequestBody refrigeratorRequest: RefrigeratorRequest + ): CommonResponse { return success(refrigeratorService.updateRefrigerator(id, refrigeratorRequest)) } diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index a03030e..b01d635 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -19,15 +19,15 @@ class RefrigeratorService( return refrigeratorRepository.save(refrigerator).refrigeratorId } - fun getRefrigerator(id: Long): String { + fun getRefrigerator(id: Long): RefrigeratorResponse { val refrigerator = - refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("냉장고가 존재하지 않습니다. ID: $id") } - return "service ok" + refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") } + return RefrigeratorResponse(refrigerator) } fun updateRefrigerator(id: Long, refrigeratorRequest: RefrigeratorRequest): RefrigeratorResponse { val refrigerator = - refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("냉장고가 존재하지 않습니다. ID: $id") } + refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") } refrigerator.update(refrigeratorRequest) return RefrigeratorResponse(refrigeratorRepository.save(refrigerator)) } From d709ce3ea58eac79e60c9afa46e1069da997f491 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 17:07:13 +0900 Subject: [PATCH 10/14] =?UTF-8?q?ktlint=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mara/server/domain/refrigerator/Refrigerator.kt | 13 ++++++++++--- .../domain/refrigerator/RefrigeratorController.kt | 11 +++++++++-- .../domain/refrigerator/RefrigeratorRepository.kt | 3 +-- .../domain/refrigerator/RefrigeratorResponse.kt | 2 +- .../domain/refrigerator/RefrigeratorService.kt | 2 +- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt index 1a3be89..dbd0b7e 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -1,6 +1,13 @@ package mara.server.domain.refrigerator -import jakarta.persistence.* +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.FetchType +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne import mara.server.domain.user.User @Entity @@ -10,7 +17,7 @@ class Refrigerator( @JoinColumn(name = "userId") val user: User, - ) { +) { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "refrigerator_id", nullable = false) @@ -19,4 +26,4 @@ class Refrigerator( fun update(refrigeratorRequest: RefrigeratorRequest) { this.name = refrigeratorRequest.name } -} \ No newline at end of file +} diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index ccb5f81..303c8af 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -2,7 +2,14 @@ package mara.server.domain.refrigerator import mara.server.common.CommonResponse import mara.server.common.success -import org.springframework.web.bind.annotation.* +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 @RestController @RequestMapping("/refrigs") @@ -31,4 +38,4 @@ class RefrigeratorController( fun updateRefrigerator(@PathVariable(name = "id") id: Long): CommonResponse { return success(refrigeratorService.deleteRefrigerator(id)) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt index 18162a3..560cd79 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt @@ -4,5 +4,4 @@ import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository @Repository -interface RefrigeratorRepository : JpaRepository { -} \ No newline at end of file +interface RefrigeratorRepository : JpaRepository diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt index 8ca0b67..39660d0 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt @@ -8,4 +8,4 @@ data class RefrigeratorResponse( id = refrigerator.refrigeratorId, name = refrigerator.name ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index b01d635..14a25d5 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -36,4 +36,4 @@ class RefrigeratorService( refrigeratorRepository.deleteById(id) return "deleted" } -} \ No newline at end of file +} From a803702e2ee19f4f018fcea3174525617b0ba321 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Sun, 21 Jan 2024 18:24:15 +0900 Subject: [PATCH 11/14] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=EB=83=89?= =?UTF-8?q?=EC=9E=A5=EA=B3=A0=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EB=B6=88?= =?UTF-8?q?=EB=9F=AC=EC=98=A4=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/refrigerator/RefrigeratorController.kt | 5 +++++ .../server/domain/refrigerator/RefrigeratorRepository.kt | 5 ++++- .../server/domain/refrigerator/RefrigeratorResponse.kt | 4 ++++ .../mara/server/domain/refrigerator/RefrigeratorService.kt | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index 303c8af..d813a17 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -26,6 +26,11 @@ class RefrigeratorController( return success(refrigeratorService.getRefrigerator((id))) } + @GetMapping("/users/{user-id}") + fun getRefrigeratorList(@PathVariable(name = "user-id") userId: Long): CommonResponse> { + return success(refrigeratorService.getRefrigeratorList((userId))) + } + @PutMapping("/{id}") fun updateRefrigerator( @PathVariable(name = "id") id: Long, diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt index 560cd79..bd3aeed 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt @@ -1,7 +1,10 @@ package mara.server.domain.refrigerator +import mara.server.domain.user.User import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository @Repository -interface RefrigeratorRepository : JpaRepository +interface RefrigeratorRepository : JpaRepository { + fun findRefrigeratorsByUser(user: User): List +} diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt index 39660d0..a29e809 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt @@ -9,3 +9,7 @@ data class RefrigeratorResponse( name = refrigerator.name ) } + +fun List.toRefrigeratorResponseList(): List { + return this.map { RefrigeratorResponse(it) } +} diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index 14a25d5..41637b3 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -25,6 +25,13 @@ class RefrigeratorService( return RefrigeratorResponse(refrigerator) } + fun getRefrigeratorList(userId: Long): List { + val user = + userRepository.findById(userId).orElseThrow { NoSuchElementException("해당 유저가 존재하지 않습니다. ID: $userId") } + val refrigeratorList = refrigeratorRepository.findRefrigeratorsByUser(user) + return refrigeratorList.toRefrigeratorResponseList() + } + fun updateRefrigerator(id: Long, refrigeratorRequest: RefrigeratorRequest): RefrigeratorResponse { val refrigerator = refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") } From 1707a9b26a1449787129edbf4eb49b3e01e7e556 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Wed, 24 Jan 2024 21:43:50 +0900 Subject: [PATCH 12/14] =?UTF-8?q?var=20->=20val=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt index dbd0b7e..de0f3b1 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -21,7 +21,7 @@ class Refrigerator( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "refrigerator_id", nullable = false) - var refrigeratorId: Long = 0L + val refrigeratorId: Long = 0L fun update(refrigeratorRequest: RefrigeratorRequest) { this.name = refrigeratorRequest.name From 74a46cc916b43432d5a02736b94f44616bc5776e Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Wed, 24 Jan 2024 21:48:34 +0900 Subject: [PATCH 13/14] =?UTF-8?q?Request,=20Response=20->=20Dto=20?= =?UTF-8?q?=EB=B3=91=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{RefrigeratorResponse.kt => RefrigeratorDto.kt} | 5 +++++ .../mara/server/domain/refrigerator/RefrigeratorRequest.kt | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-) rename src/main/kotlin/mara/server/domain/refrigerator/{RefrigeratorResponse.kt => RefrigeratorDto.kt} (83%) delete mode 100644 src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt similarity index 83% rename from src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt rename to src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt index a29e809..73f5b80 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorResponse.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt @@ -1,5 +1,10 @@ package mara.server.domain.refrigerator +data class RefrigeratorRequest( + var name: String, + val userId: Long +) + data class RefrigeratorResponse( var id: Long, var name: String, diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt deleted file mode 100644 index 88ecd16..0000000 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRequest.kt +++ /dev/null @@ -1,6 +0,0 @@ -package mara.server.domain.refrigerator - -data class RefrigeratorRequest( - var name: String, - val userId: Long -) From c4960c42726cb84b888f1bfc259d3134a7e0def6 Mon Sep 17 00:00:00 2001 From: jhkang1517 Date: Wed, 24 Jan 2024 21:52:19 +0900 Subject: [PATCH 14/14] =?UTF-8?q?update=20=EC=A0=84=EC=9A=A9=20updateReque?= =?UTF-8?q?st=20DTO=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/mara/server/domain/refrigerator/Refrigerator.kt | 4 ++-- .../mara/server/domain/refrigerator/RefrigeratorController.kt | 4 ++-- .../kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt | 4 ++++ .../mara/server/domain/refrigerator/RefrigeratorService.kt | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt index de0f3b1..4b7de4d 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -23,7 +23,7 @@ class Refrigerator( @Column(name = "refrigerator_id", nullable = false) val refrigeratorId: Long = 0L - fun update(refrigeratorRequest: RefrigeratorRequest) { - this.name = refrigeratorRequest.name + fun update(refrigeratorUpdateRequest: RefrigeratorUpdateRequest) { + this.name = refrigeratorUpdateRequest.name } } diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt index d813a17..9600834 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt @@ -34,9 +34,9 @@ class RefrigeratorController( @PutMapping("/{id}") fun updateRefrigerator( @PathVariable(name = "id") id: Long, - @RequestBody refrigeratorRequest: RefrigeratorRequest + @RequestBody refrigeratorUpdateRequest: RefrigeratorUpdateRequest ): CommonResponse { - return success(refrigeratorService.updateRefrigerator(id, refrigeratorRequest)) + return success(refrigeratorService.updateRefrigerator(id, refrigeratorUpdateRequest)) } @DeleteMapping("/{id}") diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt index 73f5b80..8093775 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt @@ -5,6 +5,10 @@ data class RefrigeratorRequest( val userId: Long ) +data class RefrigeratorUpdateRequest( + var name: String +) + data class RefrigeratorResponse( var id: Long, var name: String, diff --git a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt index 41637b3..79e4d42 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt @@ -32,10 +32,10 @@ class RefrigeratorService( return refrigeratorList.toRefrigeratorResponseList() } - fun updateRefrigerator(id: Long, refrigeratorRequest: RefrigeratorRequest): RefrigeratorResponse { + fun updateRefrigerator(id: Long, refrigeratorUpdateRequest: RefrigeratorUpdateRequest): RefrigeratorResponse { val refrigerator = refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") } - refrigerator.update(refrigeratorRequest) + refrigerator.update(refrigeratorUpdateRequest) return RefrigeratorResponse(refrigeratorRepository.save(refrigerator)) }