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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user