Implements getting items for a hunt
This commit is contained in:
@@ -25,7 +25,7 @@ class ItemController(private val huntService: HuntService) {
|
||||
|
||||
@GetMapping
|
||||
fun getItemsForHunt(@PathVariable huntId: HuntId): ResponseEntity<List<ItemResponse>> {
|
||||
TODO("List the Items related to the specified Hunt")
|
||||
return ResponseEntity.ok(huntService.getItemsForHunt(huntId).map { it.toResponse() })
|
||||
}
|
||||
|
||||
@GetMapping("/{itemId}")
|
||||
|
||||
@@ -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<ItemRecord, ItemId>
|
||||
interface ItemRepository : JpaRepository<ItemRecord, ItemId> {
|
||||
@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<ItemRecord>
|
||||
}
|
||||
@@ -64,6 +64,11 @@ class HuntService(
|
||||
return huntRepository.save(hunt.toRecord()).toDomain()
|
||||
}
|
||||
|
||||
fun getItemsForHunt(huntId: HuntId): List<Item> {
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user