Adds update and delete item endpoints
This commit is contained in:
@@ -3,12 +3,14 @@ package net.halfbinary.scavengerhuntapi.service
|
||||
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||
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.model.request.ItemUpdateRequest
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntItemRepository
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
||||
import net.halfbinary.scavengerhuntapi.repository.ItemRepository
|
||||
@@ -75,4 +77,23 @@ class HuntService(
|
||||
huntItemRepository.save(HuntItem(huntId = huntId, itemId = savedItem.id).toRecord())
|
||||
return savedItem
|
||||
}
|
||||
|
||||
fun updateItem(huntId: HuntId, itemId: ItemId, request: ItemUpdateRequest): Item {
|
||||
huntItemRepository.findByHuntIdAndItemId(huntId, itemId)
|
||||
?: throw NotFoundException("No item with id $itemId found in hunt $huntId")
|
||||
val existing = itemRepository.findByIdOrNull(itemId)
|
||||
?: throw NotFoundException("No item with id $itemId found")
|
||||
val updated = existing.copy(
|
||||
name = request.name ?: existing.name,
|
||||
points = request.points ?: existing.points
|
||||
)
|
||||
return itemRepository.save(updated).toDomain()
|
||||
}
|
||||
|
||||
fun deleteItem(huntId: HuntId, itemId: ItemId) {
|
||||
val huntItem = huntItemRepository.findByHuntIdAndItemId(huntId, itemId)
|
||||
?: throw NotFoundException("No item with id $itemId found in hunt $huntId")
|
||||
huntItemRepository.delete(huntItem)
|
||||
itemRepository.deleteById(itemId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user