-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from DDD-Community/MARA-17
MARA-17 : 냉장고 CRUD 구현
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorDto.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/mara/server/domain/refrigerator/RefrigeratorService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |