Adds/collects Hunter endpoints and cleans up the code a bit

This commit is contained in:
2026-05-12 23:54:27 -05:00
parent 4a1077833e
commit b2ba9ce676
17 changed files with 115 additions and 60 deletions

View File

@@ -5,7 +5,7 @@ import io.jsonwebtoken.Jwts
import jakarta.annotation.PostConstruct import jakarta.annotation.PostConstruct
import org.springframework.beans.factory.annotation.Value import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import java.util.Date import java.util.*
import javax.crypto.SecretKey import javax.crypto.SecretKey
import javax.crypto.spec.SecretKeySpec import javax.crypto.spec.SecretKeySpec

View File

@@ -11,16 +11,19 @@ import net.halfbinary.scavengerhuntapi.model.request.HuntCreateRequest
import net.halfbinary.scavengerhuntapi.model.request.HuntStatus import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
import net.halfbinary.scavengerhuntapi.model.response.HuntResponse import net.halfbinary.scavengerhuntapi.model.response.HuntResponse
import net.halfbinary.scavengerhuntapi.service.HuntService import net.halfbinary.scavengerhuntapi.service.HuntService
import net.halfbinary.scavengerhuntapi.service.HunterService
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.security.core.Authentication import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.PathVariable
import java.time.LocalDateTime import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@RestController @RestController
@RequestMapping("hunt") @RequestMapping("hunt")
class HuntController(private val huntService: HuntService, private val hunterService: HunterService) { class HuntController(private val huntService: HuntService) {
@GetMapping("/{id}") @GetMapping("/{id}")
@Operation(summary = "Gets the specified hunt information") @Operation(summary = "Gets the specified hunt information")
@@ -29,24 +32,12 @@ class HuntController(private val huntService: HuntService, private val hunterSer
} }
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@Tag(name = "Admin") @Tag(name = "Admin")
@GetMapping() @GetMapping
@Operation(summary = "Gets all Hunts") @Operation(summary = "Gets all Hunts")
fun getAllHunts(@RequestParam status: HuntStatus?): ResponseEntity<List<HuntResponse>> { fun getAllHunts(@RequestParam status: HuntStatus?): ResponseEntity<List<HuntResponse>> {
return ResponseEntity.ok(huntService.getAllHunts(status).map { it.toResponse() }) return ResponseEntity.ok(huntService.getAllHunts(status).map { it.toResponse() })
} }
@GetMapping("/ongoing")
@Operation(summary = "Gets list of all currently running Hunts (filtered by the calling hunter)")
fun getOngoingHunts(authentication: Authentication, @RequestParam status: HuntStatus?): ResponseEntity<List<HuntResponse>> {
val email = authentication.name
val isAdmin = hunterService.getHunterByEmail(email).isAdmin
return if(isAdmin) {
ResponseEntity.ok(huntService.getAllHunts(HuntStatus.ONGOING).map { it.toResponse() })
} else {
ResponseEntity.ok(huntService.getHuntsByEmail(email, status).map { it.toResponse() })
}
}
@GetMapping("/unstarted") @GetMapping("/unstarted")
@Operation(summary = "Gets list of all upcoming Hunts") @Operation(summary = "Gets list of all upcoming Hunts")
fun getUnstartedHunts(): ResponseEntity<List<HuntResponse>> { fun getUnstartedHunts(): ResponseEntity<List<HuntResponse>> {
@@ -55,7 +46,7 @@ class HuntController(private val huntService: HuntService, private val hunterSer
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@Tag(name = "Admin") @Tag(name = "Admin")
@PostMapping() @PostMapping
@Operation(summary = "Creates a new Hunt") @Operation(summary = "Creates a new Hunt")
fun createHunt(@Valid @RequestBody huntRequest: HuntCreateRequest): ResponseEntity<HuntResponse> { fun createHunt(@Valid @RequestBody huntRequest: HuntCreateRequest): ResponseEntity<HuntResponse> {
return ResponseEntity.ok(huntService.createHunt(huntRequest.toDomain()).toResponse()) return ResponseEntity.ok(huntService.createHunt(huntRequest.toDomain()).toResponse())

View File

@@ -0,0 +1,51 @@
package net.halfbinary.scavengerhuntapi.controller
import io.swagger.v3.oas.annotations.Operation
import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.TeamId
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
import net.halfbinary.scavengerhuntapi.model.response.HuntResponse
import net.halfbinary.scavengerhuntapi.model.response.TeamResponse
import net.halfbinary.scavengerhuntapi.service.HuntService
import net.halfbinary.scavengerhuntapi.service.HunterService
import net.halfbinary.scavengerhuntapi.service.TeamService
import org.springframework.http.ResponseEntity
import org.springframework.security.core.Authentication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/hunter")
class HunterController(private val hunterService: HunterService,
private val huntService: HuntService,
private val teamService: TeamService) {
@GetMapping("/hunt/ongoing")
@Operation(summary = "Gets list of all currently running Hunts (filtered by the calling hunter)")
fun getOngoingHunts(authentication: Authentication, @RequestParam status: HuntStatus?): ResponseEntity<List<HuntResponse>> {
val email = authentication.name
val isAdmin = hunterService.getHunterByEmail(email).isAdmin
return if(isAdmin) {
ResponseEntity.ok(huntService.getAllHunts(HuntStatus.ONGOING).map { it.toResponse() })
} else {
ResponseEntity.ok(huntService.getHuntsByEmail(email, status).map { it.toResponse() })
}
}
@PostMapping("/hunt/{huntId}/team/{teamId}")
@Operation(summary = "Joins Hunter to specified Team for specified Hunt")
fun joinTeamForHunt(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId, authentication: Authentication) {
teamService.joinTeam(teamId, authentication.name)
}
@GetMapping("/hunt/{huntId}/team")
@Operation(summary = "Gets the Team for the Hunter for the specified Hunt")
fun getHunterHuntTeam(@PathVariable huntId: HuntId, authentication: Authentication): ResponseEntity<TeamResponse> {
TODO("Get the Team information for the Hunt for the Hunter (AKA the user)")
}
}

View File

@@ -3,20 +3,17 @@ package net.halfbinary.scavengerhuntapi.controller
import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.Operation
import jakarta.validation.Valid import jakarta.validation.Valid
import net.halfbinary.scavengerhuntapi.model.HuntId import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.HunterId
import net.halfbinary.scavengerhuntapi.model.ItemId import net.halfbinary.scavengerhuntapi.model.ItemId
import net.halfbinary.scavengerhuntapi.model.TeamHuntId
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.ItemRequest import net.halfbinary.scavengerhuntapi.model.request.ItemRequest
import net.halfbinary.scavengerhuntapi.model.response.HuntResponse
import net.halfbinary.scavengerhuntapi.model.response.ItemResponse import net.halfbinary.scavengerhuntapi.model.response.ItemResponse
import net.halfbinary.scavengerhuntapi.service.HuntService import net.halfbinary.scavengerhuntapi.service.HuntService
import org.springframework.http.ResponseEntity 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.* import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController @RestController
@RequestMapping("hunt/{huntId}/item") @RequestMapping("hunt/{huntId}/item")
@@ -24,18 +21,18 @@ class ItemController(private val huntService: HuntService) {
@GetMapping @GetMapping
fun getItemsForHunt(@PathVariable huntId: HuntId): ResponseEntity<List<ItemResponse>> { fun getItemsForHunt(@PathVariable huntId: HuntId): ResponseEntity<List<ItemResponse>> {
TODO() TODO("List the Items related to the specified Hunt")
} }
@GetMapping("/{itemId}") @GetMapping("/{itemId}")
fun getItem(@PathVariable huntId: HuntId, @PathVariable itemId: ItemId): ResponseEntity<ItemResponse> { fun getItem(@PathVariable huntId: HuntId, @PathVariable itemId: ItemId): ResponseEntity<ItemResponse> {
TODO() TODO("Get detailed information about the specified Item for the specified Hunt")
} }
@PostMapping @PostMapping
@Operation(summary = "Adds new Item to specified Hunt") @Operation(summary = "Adds new Item to specified Hunt")
fun addItemToHunt(@PathVariable huntId: HuntId, @Valid @RequestBody body: ItemRequest) { fun addItemToHunt(@PathVariable huntId: HuntId, @Valid @RequestBody body: ItemRequest) {
TODO() TODO("Add a new item to the specified Hunt")
} }
} }

View File

@@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.Operation
import jakarta.validation.Valid import jakarta.validation.Valid
import net.halfbinary.scavengerhuntapi.model.HuntId import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.ItemId import net.halfbinary.scavengerhuntapi.model.ItemId
import net.halfbinary.scavengerhuntapi.model.PhotoId
import net.halfbinary.scavengerhuntapi.model.TeamId import net.halfbinary.scavengerhuntapi.model.TeamId
import net.halfbinary.scavengerhuntapi.model.converter.toResponse import net.halfbinary.scavengerhuntapi.model.converter.toResponse
import net.halfbinary.scavengerhuntapi.model.request.TeamRequest import net.halfbinary.scavengerhuntapi.model.request.TeamRequest
@@ -11,6 +12,7 @@ import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse
import net.halfbinary.scavengerhuntapi.model.response.TeamItemResponse import net.halfbinary.scavengerhuntapi.model.response.TeamItemResponse
import net.halfbinary.scavengerhuntapi.model.response.TeamResponse import net.halfbinary.scavengerhuntapi.model.response.TeamResponse
import net.halfbinary.scavengerhuntapi.service.TeamService import net.halfbinary.scavengerhuntapi.service.TeamService
import org.springframework.core.io.InputStreamSource
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.security.core.Authentication import org.springframework.security.core.Authentication
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
@@ -18,7 +20,9 @@ import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
@RestController @RestController
@RequestMapping("hunt/{huntId}/team") @RequestMapping("hunt/{huntId}/team")
@@ -41,24 +45,43 @@ class TeamController(private val teamService: TeamService) {
return ResponseEntity.ok(teamService.getTeamFromHunt(huntId, teamId).toResponse()) return ResponseEntity.ok(teamService.getTeamFromHunt(huntId, teamId).toResponse())
} }
@PostMapping("/{teamId}")
fun joinTeamForHunt(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId, authentication: Authentication) {
teamService.joinTeam(teamId, authentication.name)
}
@GetMapping("/{teamId}/item/{itemId}") @GetMapping("/{teamId}/item/{itemId}")
fun getItemsForTeam(@PathVariable huntId: HuntId, fun getItemForTeam(@PathVariable huntId: HuntId,
@PathVariable teamId: TeamId, @PathVariable teamId: TeamId,
@PathVariable itemId: ItemId): ResponseEntity<TeamItemResponse> { @PathVariable itemId: ItemId): ResponseEntity<TeamItemResponse> {
TODO() TODO("Get found/not found status and photo information about the Item for the specified Team, Hunt, and Item")
} }
@GetMapping("/{teamId}/item/{itemId}/photo") @GetMapping("/{teamId}/item/{itemId}/photo")
fun getPhotosForTeam(@PathVariable huntId: HuntId, fun getItemPhotos(@PathVariable huntId: HuntId,
@PathVariable teamId: TeamId, @PathVariable teamId: TeamId,
@PathVariable itemId: ItemId): ResponseEntity<PhotoResponse> { @PathVariable itemId: ItemId): ResponseEntity<List<PhotoResponse>> {
TODO() TODO("Get list of photo information for the specified Team, Hunt, and Item")
} }
@GetMapping("/{teamId}/item/{itemId}/photo/{photoId}")
fun getPhotoInfo(@PathVariable huntId: HuntId,
@PathVariable teamId: TeamId,
@PathVariable itemId: ItemId,
@PathVariable photoId: PhotoId): ResponseEntity<PhotoResponse> {
TODO("Get photo information for the specified Team, Hunt, Item, and Photo")
}
@GetMapping("/{teamId}/item/{itemId}/photo/{photoId}/file")
fun getPhoto(@PathVariable huntId: HuntId,
@PathVariable teamId: TeamId,
@PathVariable itemId: ItemId,
@PathVariable photoId: PhotoId): ResponseEntity<InputStreamSource> {
TODO("Get the binary image information for the specified Team, Hunt, Item, and Photo")
}
@PostMapping("/{teamId}/item/{itemId}/photo")
fun submitPhoto(@PathVariable huntId: HuntId,
@PathVariable teamId: TeamId,
@PathVariable itemId: ItemId,
authentication: Authentication,
@RequestParam file: MultipartFile): ResponseEntity<PhotoResponse> {
TODO("Save photo information in the Photo table so that it relates to the specified Team, Hunt, Hunter, and Item, and store the binary file")
}
} }

View File

@@ -1,3 +1,3 @@
package net.halfbinary.scavengerhuntapi.error.exception package net.halfbinary.scavengerhuntapi.error.exception
class LoginFailedException(): RuntimeException("The email and password combination is not correct.") class LoginFailedException : RuntimeException("The email and password combination is not correct.")

View File

@@ -1,11 +1,7 @@
package net.halfbinary.scavengerhuntapi.model.converter package net.halfbinary.scavengerhuntapi.model.converter
import net.halfbinary.scavengerhuntapi.model.domain.Team
import net.halfbinary.scavengerhuntapi.model.domain.TeamHunt import net.halfbinary.scavengerhuntapi.model.domain.TeamHunt
import net.halfbinary.scavengerhuntapi.model.record.TeamHuntRecord 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
fun TeamHunt.toRecord(): TeamHuntRecord { fun TeamHunt.toRecord(): TeamHuntRecord {
return TeamHuntRecord(id, teamId, huntId) return TeamHuntRecord(id, teamId, huntId)

View File

@@ -1,7 +1,7 @@
package net.halfbinary.scavengerhuntapi.model.domain package net.halfbinary.scavengerhuntapi.model.domain
import net.halfbinary.scavengerhuntapi.model.TeamId import net.halfbinary.scavengerhuntapi.model.TeamId
import java.util.UUID import java.util.*
data class Team( data class Team(
val id: TeamId = UUID.randomUUID(), val id: TeamId = UUID.randomUUID(),

View File

@@ -3,7 +3,6 @@ package net.halfbinary.scavengerhuntapi.model.domain
import net.halfbinary.scavengerhuntapi.model.HuntId import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.TeamHuntId import net.halfbinary.scavengerhuntapi.model.TeamHuntId
import net.halfbinary.scavengerhuntapi.model.TeamId import net.halfbinary.scavengerhuntapi.model.TeamId
import java.util.UUID
data class TeamHunt( data class TeamHunt(
val id: TeamHuntId = TeamHuntId.randomUUID(), val id: TeamHuntId = TeamHuntId.randomUUID(),

View File

@@ -3,7 +3,11 @@ package net.halfbinary.scavengerhuntapi.model.record
import jakarta.persistence.Entity import jakarta.persistence.Entity
import jakarta.persistence.Id import jakarta.persistence.Id
import jakarta.persistence.Table import jakarta.persistence.Table
import net.halfbinary.scavengerhuntapi.model.* import net.halfbinary.scavengerhuntapi.model.FoundId
import net.halfbinary.scavengerhuntapi.model.FoundStatus
import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.HunterId
import net.halfbinary.scavengerhuntapi.model.ItemId
import java.time.LocalDateTime import java.time.LocalDateTime
/** /**

View File

@@ -6,7 +6,6 @@ import jakarta.persistence.Table
import net.halfbinary.scavengerhuntapi.model.HuntId import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.TeamHuntId import net.halfbinary.scavengerhuntapi.model.TeamHuntId
import net.halfbinary.scavengerhuntapi.model.TeamId import net.halfbinary.scavengerhuntapi.model.TeamId
import java.util.*
@Entity @Entity
@Table(name = "team_hunt") @Table(name = "team_hunt")

View File

@@ -2,7 +2,6 @@ package net.halfbinary.scavengerhuntapi.repository
import net.halfbinary.scavengerhuntapi.model.HuntId import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.HunterId import net.halfbinary.scavengerhuntapi.model.HunterId
import net.halfbinary.scavengerhuntapi.model.TeamId
import net.halfbinary.scavengerhuntapi.model.record.HuntRecord import net.halfbinary.scavengerhuntapi.model.record.HuntRecord
import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query import org.springframework.data.jpa.repository.Query

View File

@@ -3,7 +3,7 @@ package net.halfbinary.scavengerhuntapi.repository
import net.halfbinary.scavengerhuntapi.model.record.HunterTeamRecord import net.halfbinary.scavengerhuntapi.model.record.HunterTeamRecord
import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import java.util.UUID import java.util.*
@Repository @Repository
interface HunterTeamRepository : JpaRepository<HunterTeamRecord, UUID> interface HunterTeamRepository : JpaRepository<HunterTeamRecord, UUID>

View File

@@ -1,6 +1,5 @@
package net.halfbinary.scavengerhuntapi.repository package net.halfbinary.scavengerhuntapi.repository
import net.halfbinary.scavengerhuntapi.model.HuntId
import net.halfbinary.scavengerhuntapi.model.TeamId import net.halfbinary.scavengerhuntapi.model.TeamId
import net.halfbinary.scavengerhuntapi.model.record.TeamRecord import net.halfbinary.scavengerhuntapi.model.record.TeamRecord
import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.JpaRepository

View File

@@ -15,7 +15,7 @@ import java.time.LocalDateTime
@Service @Service
class HuntService(private val huntRepository: HuntRepository) { class HuntService(private val huntRepository: HuntRepository) {
fun getHunt(huntId: HuntId): Hunt { fun getHunt(huntId: HuntId): Hunt {
return huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id ${huntId} found") return huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id $huntId found")
} }
fun getAllHunts(status: HuntStatus?): List<Hunt> { fun getAllHunts(status: HuntStatus?): List<Hunt> {

View File

@@ -7,7 +7,7 @@ import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.util.Collections import java.util.*
@Service @Service

View File

@@ -8,16 +8,13 @@ import net.halfbinary.scavengerhuntapi.model.converter.toRecord
import net.halfbinary.scavengerhuntapi.model.domain.Team import net.halfbinary.scavengerhuntapi.model.domain.Team
import net.halfbinary.scavengerhuntapi.model.domain.TeamHunt import net.halfbinary.scavengerhuntapi.model.domain.TeamHunt
import net.halfbinary.scavengerhuntapi.model.record.HunterTeamRecord 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.request.TeamRequest
import net.halfbinary.scavengerhuntapi.model.response.TeamResponse
import net.halfbinary.scavengerhuntapi.repository.HunterRepository import net.halfbinary.scavengerhuntapi.repository.HunterRepository
import net.halfbinary.scavengerhuntapi.repository.HunterTeamRepository import net.halfbinary.scavengerhuntapi.repository.HunterTeamRepository
import net.halfbinary.scavengerhuntapi.repository.TeamHuntRepository import net.halfbinary.scavengerhuntapi.repository.TeamHuntRepository
import net.halfbinary.scavengerhuntapi.repository.TeamRepository import net.halfbinary.scavengerhuntapi.repository.TeamRepository
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.util.UUID import java.util.*
@Service @Service
class TeamService( class TeamService(