Implements Hunt basic create and get, and adds field validation to controllers

This commit is contained in:
2025-12-22 08:44:14 -06:00
parent 04b61485ea
commit 5905882763
16 changed files with 230 additions and 5 deletions

View File

@@ -0,0 +1,36 @@
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.converter.toDomain
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
import net.halfbinary.scavengerhuntapi.model.domain.Hunt
import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
@Service
class HuntService(private val huntRepository: HuntRepository) {
fun getHunt(huntId: HuntId): Hunt {
return huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id ${huntId} found")
}
fun getAllHunts(status: HuntStatus?): List<Hunt> {
return when(status) {
HuntStatus.UNSTARTED -> huntRepository.findAllUnstarted().map { it.toDomain() }
HuntStatus.ONGOING -> huntRepository.findAllOngoing().map { it.toDomain() }
HuntStatus.CLOSED -> huntRepository.findAllClosed().map { it.toDomain() }
else -> huntRepository.findAll().map { it.toDomain() }
}
}
fun getHuntsByHunter(hunterId: HunterId): List<Hunt> {
return huntRepository.findAllOngoingByHunter(hunterId).map { it.toDomain() }
}
fun createHunt(hunt: Hunt): Hunt {
return huntRepository.save(hunt.toRecord()).toDomain()
}
}