Implements Hunt basic create and get, and adds field validation to controllers
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user