Implements adding an item to a hunt
This commit is contained in:
@@ -6,14 +6,22 @@ import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.Hunt
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.HuntItem
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.Item
|
||||
import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntItemRepository
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
||||
import net.halfbinary.scavengerhuntapi.repository.ItemRepository
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Service
|
||||
class HuntService(private val huntRepository: HuntRepository) {
|
||||
class HuntService(
|
||||
private val huntRepository: HuntRepository,
|
||||
private val itemRepository: ItemRepository,
|
||||
private val huntItemRepository: HuntItemRepository
|
||||
) {
|
||||
fun getHunt(huntId: HuntId): Hunt {
|
||||
return huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id $huntId found")
|
||||
}
|
||||
@@ -55,4 +63,11 @@ class HuntService(private val huntRepository: HuntRepository) {
|
||||
fun createHunt(hunt: Hunt): Hunt {
|
||||
return huntRepository.save(hunt.toRecord()).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()
|
||||
huntItemRepository.save(HuntItem(huntId = huntId, itemId = savedItem.id).toRecord())
|
||||
return savedItem
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user