-
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.
* 식재료(Ingredient) 껍데기 구현 * 식재료 생성 기능 구현 * 식재료 조회 기능 구현 * 식재료 업데이트 기능 구현 * 식재료 삭제 기능 구현 * 식재료 전체 조회 기능 구현 * ktlint 적용 * Request, Response -> DTO 통합 * ingredientId var -> val 변경
- Loading branch information
1 parent
e1f2c56
commit 8832194
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/mara/server/domain/ingredient/Ingredient.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,25 @@ | ||
package mara.server.domain.ingredient | ||
|
||
import jakarta.persistence.Entity | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
|
||
@Entity | ||
class Ingredient( | ||
var category: String, | ||
var name: String, | ||
var iconImage: String, | ||
var expirationDays: Int = 0 | ||
) { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val ingredientId: Long = 0L | ||
|
||
fun update(ingredientRequest: IngredientRequest) { | ||
this.category = ingredientRequest.category | ||
this.name = ingredientRequest.name | ||
this.iconImage = ingredientRequest.iconImage | ||
this.expirationDays = ingredientRequest.expirationDays | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/mara/server/domain/ingredient/IngredientController.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,47 @@ | ||
package mara.server.domain.ingredient | ||
|
||
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("/ingrs") | ||
class IngredientController( | ||
private val ingredientService: IngredientService | ||
) { | ||
|
||
@PostMapping | ||
fun createIngredient(@RequestBody ingredientRequest: IngredientRequest): CommonResponse<Long> { | ||
return success(ingredientService.createIngredient(ingredientRequest)) | ||
} | ||
|
||
@GetMapping("/{id}") | ||
fun getIngredient(@PathVariable(name = "id") id: Long): CommonResponse<IngredientResponse> { | ||
return success(ingredientService.getIngredient(id)) | ||
} | ||
|
||
@GetMapping | ||
fun getIngredientList(): CommonResponse<List<IngredientResponse>> { | ||
return success(ingredientService.getIngredientList()) | ||
} | ||
|
||
@PutMapping("/{id}") | ||
fun updateIngredient( | ||
@PathVariable(name = "id") id: Long, | ||
@RequestBody ingredientRequest: IngredientRequest | ||
): CommonResponse<IngredientResponse> { | ||
return success(ingredientService.updateIngredient(id, ingredientRequest)) | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
fun deleteIngredient(@PathVariable(name = "id") id: Long): CommonResponse<String> { | ||
return success(ingredientService.deleteIngredient(id)) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/mara/server/domain/ingredient/IngredientDto.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,28 @@ | ||
package mara.server.domain.ingredient | ||
|
||
data class IngredientRequest( | ||
val category: String, | ||
val name: String, | ||
val iconImage: String, | ||
val expirationDays: Int | ||
) | ||
|
||
data class IngredientResponse( | ||
val ingredientId: Long, | ||
val category: String, | ||
val name: String, | ||
val iconImage: String, | ||
val expirationDays: Int | ||
) { | ||
constructor(ingredient: Ingredient) : this( | ||
ingredientId = ingredient.ingredientId, | ||
category = ingredient.category, | ||
name = ingredient.name, | ||
iconImage = ingredient.iconImage, | ||
expirationDays = ingredient.expirationDays | ||
) | ||
} | ||
|
||
fun List<Ingredient>.toIngredientResponseList(): List<IngredientResponse> { | ||
return this.map { IngredientResponse(it) } | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/mara/server/domain/ingredient/IngredientRepository.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,7 @@ | ||
package mara.server.domain.ingredient | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
interface IngredientRepository : JpaRepository<Ingredient, Long> |
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/mara/server/domain/ingredient/IngredientService.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,40 @@ | ||
package mara.server.domain.ingredient | ||
|
||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class IngredientService( | ||
private val ingredientRepository: IngredientRepository | ||
) { | ||
|
||
fun createIngredient(ingredientRequest: IngredientRequest): Long { | ||
val ingredient = Ingredient( | ||
category = ingredientRequest.category, | ||
name = ingredientRequest.name, | ||
iconImage = ingredientRequest.iconImage, | ||
expirationDays = ingredientRequest.expirationDays | ||
) | ||
return ingredientRepository.save(ingredient).ingredientId | ||
} | ||
|
||
fun getIngredient(id: Long): IngredientResponse { | ||
val ingredient = ingredientRepository.findById(id).orElseThrow { NoSuchElementException("해당 식재료가 존재하지 않습니다. ID: $id") } | ||
return IngredientResponse(ingredient) | ||
} | ||
|
||
fun getIngredientList(): List<IngredientResponse> { | ||
val ingredientList = ingredientRepository.findAll() | ||
return ingredientList.toIngredientResponseList() | ||
} | ||
|
||
fun updateIngredient(id: Long, ingredientRequest: IngredientRequest): IngredientResponse { | ||
val ingredient = ingredientRepository.findById(id).orElseThrow { NoSuchElementException("해당 식재료가 존재하지 않습니다. ID: $id") } | ||
ingredient.update(ingredientRequest) | ||
return IngredientResponse(ingredientRepository.save(ingredient)) | ||
} | ||
|
||
fun deleteIngredient(id: Long): String { | ||
ingredientRepository.deleteById(id) | ||
return "deleted" | ||
} | ||
} |