diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt index 117f08f..431a1a8 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/controller/ItemController.kt @@ -25,7 +25,7 @@ class ItemController(private val huntService: HuntService) { @GetMapping fun getItemsForHunt(@PathVariable huntId: HuntId): ResponseEntity> { - TODO("List the Items related to the specified Hunt") + return ResponseEntity.ok(huntService.getItemsForHunt(huntId).map { it.toResponse() }) } @GetMapping("/{itemId}") diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/ItemRepository.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/ItemRepository.kt index 8acd6e6..59465f7 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/ItemRepository.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/repository/ItemRepository.kt @@ -1,9 +1,19 @@ package net.halfbinary.scavengerhuntapi.repository +import net.halfbinary.scavengerhuntapi.model.HuntId import net.halfbinary.scavengerhuntapi.model.ItemId import net.halfbinary.scavengerhuntapi.model.record.ItemRecord import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query import org.springframework.stereotype.Repository @Repository -interface ItemRepository : JpaRepository \ No newline at end of file +interface ItemRepository : JpaRepository { + @Query(""" + SELECT i.* + FROM item i + INNER JOIN hunt_item hi ON i.id = hi.item_id + WHERE hi.hunt_id = :huntId + """, nativeQuery = true) + fun findAllByHuntId(huntId: HuntId): List +} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt index a32cdac..bbe6352 100644 --- a/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt +++ b/src/main/kotlin/net/halfbinary/scavengerhuntapi/service/HuntService.kt @@ -64,6 +64,11 @@ class HuntService( return huntRepository.save(hunt.toRecord()).toDomain() } + fun getItemsForHunt(huntId: HuntId): List { + huntRepository.findByIdOrNull(huntId) ?: throw NotFoundException("No hunt with id $huntId found") + return itemRepository.findAllByHuntId(huntId).map { it.toDomain() } + } + fun addItemToHunt(huntId: HuntId, item: Item): Item { huntRepository.findByIdOrNull(huntId) ?: throw NotFoundException("No hunt with id $huntId found") val savedItem = itemRepository.save(item.toRecord()).toDomain()