Adds user level hunt endpoints
This commit is contained in:
@@ -10,6 +10,7 @@ import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Service
|
||||
class HuntService(private val huntRepository: HuntRepository) {
|
||||
@@ -30,6 +31,27 @@ class HuntService(private val huntRepository: HuntRepository) {
|
||||
return huntRepository.findAllOngoingByHunter(hunterId).map { it.toDomain() }
|
||||
}
|
||||
|
||||
fun getHuntsByEmail(email: String, status: HuntStatus?): List<Hunt> {
|
||||
val allHunts = huntRepository.findHuntsByEmail(email)
|
||||
val filteredHunts = when (status) {
|
||||
HuntStatus.ONGOING -> {
|
||||
allHunts
|
||||
.filter { !it.isTerminated && it.startDateTime < LocalDateTime.now() && it.endDateTime > LocalDateTime.now() }
|
||||
.toList()
|
||||
}
|
||||
HuntStatus.CLOSED -> {
|
||||
allHunts
|
||||
.filter { it.isTerminated || it.endDateTime < LocalDateTime.now() }
|
||||
}
|
||||
HuntStatus.UNSTARTED -> {
|
||||
allHunts
|
||||
.filter { !it.isTerminated && it.startDateTime > LocalDateTime.now() }
|
||||
}
|
||||
else -> { allHunts }
|
||||
}
|
||||
return filteredHunts.map { it.toDomain() }
|
||||
}
|
||||
|
||||
fun createHunt(hunt: Hunt): Hunt {
|
||||
return huntRepository.save(hunt.toRecord()).toDomain()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package net.halfbinary.scavengerhuntapi.service
|
||||
|
||||
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
|
||||
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class HunterService(private val hunterRepository: HunterRepository) {
|
||||
fun getHunterByEmail(email: String): Hunter {
|
||||
return hunterRepository.findByEmail(email)?.toDomain()
|
||||
?: throw NotFoundException("No hunter with email $email found")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user