Adds Team members list endpoint and Hunt details update endpoint
This commit is contained in:
@@ -9,11 +9,13 @@ import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.request.HuntCreateRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
|
||||
import net.halfbinary.scavengerhuntapi.model.request.HuntUpdateRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.response.HuntResponse
|
||||
import net.halfbinary.scavengerhuntapi.service.HuntService
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PatchMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
@@ -52,6 +54,14 @@ class HuntController(private val huntService: HuntService) {
|
||||
return ResponseEntity.ok(huntService.createHunt(huntRequest.toDomain()).toResponse())
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@Tag(name = "Admin")
|
||||
@PatchMapping("/{id}")
|
||||
@Operation(summary = "Updates details of the specified Hunt")
|
||||
fun updateHunt(@PathVariable("id") huntId: HuntId, @RequestBody body: HuntUpdateRequest): ResponseEntity<HuntResponse> {
|
||||
return ResponseEntity.ok(huntService.updateHunt(huntId, body).toResponse())
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@Tag(name = "Admin")
|
||||
@GetMapping("/hunter/{hunterId}")
|
||||
|
||||
@@ -7,7 +7,9 @@ import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toSummaryResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.request.TeamRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.response.HunterSummaryResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.response.TeamItemResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.response.TeamResponse
|
||||
@@ -47,6 +49,12 @@ class TeamController(private val teamService: TeamService, private val photoServ
|
||||
return ResponseEntity.ok(teamService.getTeamFromHunt(huntId, teamId).toResponse())
|
||||
}
|
||||
|
||||
@GetMapping("/{teamId}/hunter")
|
||||
@Operation(summary = "Get all Hunters for the specified Team in the specified Hunt")
|
||||
fun getHuntersForTeam(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId): ResponseEntity<List<HunterSummaryResponse>> {
|
||||
return ResponseEntity.ok(teamService.getHuntersForTeam(huntId, teamId).map { it.toSummaryResponse() })
|
||||
}
|
||||
|
||||
@GetMapping("/{teamId}/item/{itemId}")
|
||||
@Operation(summary = "Get found/not found status about the Item for the specified Team, Hunt, and Item")
|
||||
fun getItemForTeam(@PathVariable huntId: HuntId,
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.halfbinary.scavengerhuntapi.model.converter
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
|
||||
import net.halfbinary.scavengerhuntapi.model.record.HunterRecord
|
||||
import net.halfbinary.scavengerhuntapi.model.request.HunterSignupRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.response.HunterSummaryResponse
|
||||
|
||||
fun HunterSignupRequest.toDomain(): Hunter {
|
||||
return Hunter(
|
||||
@@ -19,4 +20,8 @@ fun Hunter.toRecord(): HunterRecord {
|
||||
|
||||
fun HunterRecord.toDomain(): Hunter {
|
||||
return Hunter(id, email, name, password, isAdmin)
|
||||
}
|
||||
|
||||
fun Hunter.toSummaryResponse(): HunterSummaryResponse {
|
||||
return HunterSummaryResponse(id, name)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package net.halfbinary.scavengerhuntapi.model.request
|
||||
|
||||
import java.time.LocalDateTime
|
||||
|
||||
data class HuntUpdateRequest(
|
||||
val title: String?,
|
||||
val startDateTime: LocalDateTime?,
|
||||
val endDateTime: LocalDateTime?,
|
||||
val isTerminated: Boolean?
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
package net.halfbinary.scavengerhuntapi.model.response
|
||||
|
||||
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||
|
||||
data class HunterSummaryResponse(
|
||||
val id: HunterId,
|
||||
val name: String
|
||||
)
|
||||
@@ -11,6 +11,7 @@ import net.halfbinary.scavengerhuntapi.model.domain.Hunt
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.HuntItem
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.Item
|
||||
import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
|
||||
import net.halfbinary.scavengerhuntapi.model.request.HuntUpdateRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.request.ItemUpdateRequest
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntItemRepository
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
||||
@@ -68,6 +69,18 @@ class HuntService(
|
||||
return huntRepository.save(hunt.toRecord()).toDomain()
|
||||
}
|
||||
|
||||
fun updateHunt(huntId: HuntId, request: HuntUpdateRequest): Hunt {
|
||||
val existing = huntRepository.findByIdOrNull(huntId)
|
||||
?: throw NotFoundException("No hunt with id $huntId found")
|
||||
val updated = existing.copy(
|
||||
title = request.title ?: existing.title,
|
||||
startDateTime = request.startDateTime ?: existing.startDateTime,
|
||||
endDateTime = request.endDateTime ?: existing.endDateTime,
|
||||
isTerminated = request.isTerminated ?: existing.isTerminated
|
||||
)
|
||||
return huntRepository.save(updated).toDomain()
|
||||
}
|
||||
|
||||
fun getItemsForHunt(huntId: HuntId, email: String): List<Item> {
|
||||
val hunt = huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id $huntId found")
|
||||
val hunter = hunterService.getHunterByEmail(email)
|
||||
|
||||
@@ -6,6 +6,7 @@ import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||
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.Hunter
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.Team
|
||||
import net.halfbinary.scavengerhuntapi.model.domain.TeamHunt
|
||||
import net.halfbinary.scavengerhuntapi.model.record.HunterTeamRecord
|
||||
@@ -54,6 +55,12 @@ class TeamService(
|
||||
return hunterTeamRepository.findByTeamId(teamId).map { it.hunterId }.toSet()
|
||||
}
|
||||
|
||||
fun getHuntersForTeam(huntId: HuntId, teamId: TeamId): List<Hunter> {
|
||||
getTeamFromHunt(huntId, teamId)
|
||||
val hunterIds = getHunterIdsForTeam(teamId)
|
||||
return hunterRepository.findAllById(hunterIds).map { it.toDomain() }
|
||||
}
|
||||
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user