55 lines
2.3 KiB
Kotlin
55 lines
2.3 KiB
Kotlin
package net.halfbinary.scavengerhuntapi.service
|
|
|
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
|
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
|
import net.halfbinary.scavengerhuntapi.model.domain.Team
|
|
import net.halfbinary.scavengerhuntapi.model.domain.TeamHunt
|
|
import net.halfbinary.scavengerhuntapi.model.record.HunterTeamRecord
|
|
import net.halfbinary.scavengerhuntapi.model.record.TeamHuntRecord
|
|
import net.halfbinary.scavengerhuntapi.model.record.TeamRecord
|
|
import net.halfbinary.scavengerhuntapi.model.request.TeamRequest
|
|
import net.halfbinary.scavengerhuntapi.model.response.TeamResponse
|
|
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
|
import net.halfbinary.scavengerhuntapi.repository.HunterTeamRepository
|
|
import net.halfbinary.scavengerhuntapi.repository.TeamHuntRepository
|
|
import net.halfbinary.scavengerhuntapi.repository.TeamRepository
|
|
import org.springframework.stereotype.Service
|
|
import java.util.UUID
|
|
|
|
@Service
|
|
class TeamService(
|
|
private val teamRepository: TeamRepository,
|
|
private val teamHuntRepository: TeamHuntRepository,
|
|
private val hunterRepository: HunterRepository,
|
|
private val hunterTeamRepository: HunterTeamRepository,
|
|
) {
|
|
fun getListOfTeamsForHunt(huntId: HuntId): List<Team> {
|
|
return getTeamsForHunt(huntId)
|
|
}
|
|
|
|
fun createTeam(name: String): Team {
|
|
return teamRepository.save(TeamRequest(name).toDomain().toRecord()).toDomain()
|
|
}
|
|
|
|
fun addTeamToHunt(huntId: HuntId, teamId: TeamId) {
|
|
teamHuntRepository.save(TeamHunt(teamId = teamId, huntId = huntId).toRecord()).toDomain()
|
|
}
|
|
|
|
fun getTeamFromHunt(huntId: HuntId, teamId: TeamId): Team {
|
|
return getTeamsForHunt(huntId)
|
|
.filter { it.id == teamId }
|
|
.elementAt(0)
|
|
}
|
|
|
|
fun joinTeam(teamId: TeamId, email: String) {
|
|
val hunter = hunterRepository.findByEmail(email) ?: throw NotFoundException("No hunter with email $email found")
|
|
hunterTeamRepository.save(HunterTeamRecord(UUID.randomUUID(), hunter.id, teamId))
|
|
}
|
|
|
|
private fun getTeamsForHunt(huntId: HuntId): List<Team> {
|
|
return teamHuntRepository.findTeamsByHuntId(huntId).map { it.toDomain() }
|
|
}
|
|
} |