Skip to content

Commit

Permalink
Merge pull request #4 from DDD-Community/MARA-17
Browse files Browse the repository at this point in the history
MARA-17 : 냉장고 CRUD 구현
  • Loading branch information
Jiwon-cho authored Jan 24, 2024
2 parents 5d7406f + c4960c4 commit 3fdbea7
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mara.server.domain.refrigerator

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
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)
val refrigeratorId: Long = 0L

fun update(refrigeratorUpdateRequest: RefrigeratorUpdateRequest) {
this.name = refrigeratorUpdateRequest.name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mara.server.domain.refrigerator

import mara.server.common.CommonResponse
import mara.server.common.success
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")
class RefrigeratorController(
private val refrigeratorService: RefrigeratorService
) {
@PostMapping
fun createRefrigerator(@RequestBody refrigeratorRequest: RefrigeratorRequest): CommonResponse<Long> {
return success(refrigeratorService.createRefrigerator(refrigeratorRequest))
}

@GetMapping("/{id}")
fun getRefrigerator(@PathVariable(name = "id") id: Long): CommonResponse<RefrigeratorResponse> {
return success(refrigeratorService.getRefrigerator((id)))
}

@GetMapping("/users/{user-id}")
fun getRefrigeratorList(@PathVariable(name = "user-id") userId: Long): CommonResponse<List<RefrigeratorResponse>> {
return success(refrigeratorService.getRefrigeratorList((userId)))
}

@PutMapping("/{id}")
fun updateRefrigerator(
@PathVariable(name = "id") id: Long,
@RequestBody refrigeratorUpdateRequest: RefrigeratorUpdateRequest
): CommonResponse<RefrigeratorResponse> {
return success(refrigeratorService.updateRefrigerator(id, refrigeratorUpdateRequest))
}

@DeleteMapping("/{id}")
fun updateRefrigerator(@PathVariable(name = "id") id: Long): CommonResponse<String> {
return success(refrigeratorService.deleteRefrigerator(id))
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mara.server.domain.refrigerator

data class RefrigeratorRequest(
var name: String,
val userId: Long
)

data class RefrigeratorUpdateRequest(
var name: String
)

data class RefrigeratorResponse(
var id: Long,
var name: String,
) {
constructor(refrigerator: Refrigerator) : this(
id = refrigerator.refrigeratorId,
name = refrigerator.name
)
}

fun List<Refrigerator>.toRefrigeratorResponseList(): List<RefrigeratorResponse> {
return this.map { RefrigeratorResponse(it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +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<Refrigerator, Long> {
fun findRefrigeratorsByUser(user: User): List<Refrigerator>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 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 getRefrigerator(id: Long): RefrigeratorResponse {
val refrigerator =
refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") }
return RefrigeratorResponse(refrigerator)
}

fun getRefrigeratorList(userId: Long): List<RefrigeratorResponse> {
val user =
userRepository.findById(userId).orElseThrow { NoSuchElementException("해당 유저가 존재하지 않습니다. ID: $userId") }
val refrigeratorList = refrigeratorRepository.findRefrigeratorsByUser(user)
return refrigeratorList.toRefrigeratorResponseList()
}

fun updateRefrigerator(id: Long, refrigeratorUpdateRequest: RefrigeratorUpdateRequest): RefrigeratorResponse {
val refrigerator =
refrigeratorRepository.findById(id).orElseThrow { NoSuchElementException("해당 냉장고가 존재하지 않습니다. ID: $id") }
refrigerator.update(refrigeratorUpdateRequest)
return RefrigeratorResponse(refrigeratorRepository.save(refrigerator))
}

fun deleteRefrigerator(id: Long): String {
refrigeratorRepository.deleteById(id)
return "deleted"
}
}

0 comments on commit 3fdbea7

Please sign in to comment.