Implements team item status endpoint
This commit is contained in:
@@ -47,12 +47,13 @@ class TeamController(private val teamService: TeamService, private val photoServ
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{teamId}/item/{itemId}")
|
@GetMapping("/{teamId}/item/{itemId}")
|
||||||
@Operation(summary = "Get found/not found status and photo information about the Item for the specified Team, Hunt, and Item")
|
@Operation(summary = "Get found/not found status about the Item for the specified Team, Hunt, and Item")
|
||||||
fun getItemForTeam(@PathVariable huntId: HuntId,
|
fun getItemForTeam(@PathVariable huntId: HuntId,
|
||||||
@PathVariable teamId: TeamId,
|
@PathVariable teamId: TeamId,
|
||||||
@PathVariable itemId: ItemId): ResponseEntity<TeamItemResponse> {
|
@PathVariable itemId: ItemId,
|
||||||
TODO("Get found/not found status about the Item for the specified Team, Hunt, and Item")
|
authentication: Authentication): ResponseEntity<TeamItemResponse> {
|
||||||
|
val foundStatus = photoService.getItemFoundStatus(huntId, teamId, itemId, authentication.name)
|
||||||
|
return ResponseEntity.ok(TeamItemResponse(id = itemId, itemFoundStatus = foundStatus))
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{teamId}/item/{itemId}/photo")
|
@GetMapping("/{teamId}/item/{itemId}/photo")
|
||||||
|
|||||||
@@ -4,6 +4,5 @@ enum class FoundStatus {
|
|||||||
NOT_FOUND,
|
NOT_FOUND,
|
||||||
SUBMITTED,
|
SUBMITTED,
|
||||||
APPROVED,
|
APPROVED,
|
||||||
REJECTED,
|
REJECTED
|
||||||
REMOVED
|
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,7 @@ import net.halfbinary.scavengerhuntapi.model.ItemId
|
|||||||
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.util.UUID
|
import java.util.*
|
||||||
|
|
||||||
data class Photo(
|
data class Photo(
|
||||||
val id: PhotoId = UUID.randomUUID(),
|
val id: PhotoId = UUID.randomUUID(),
|
||||||
|
|||||||
@@ -2,11 +2,8 @@ package net.halfbinary.scavengerhuntapi.model.response
|
|||||||
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.FoundStatus
|
import net.halfbinary.scavengerhuntapi.model.FoundStatus
|
||||||
import net.halfbinary.scavengerhuntapi.model.ItemId
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
import java.time.LocalDateTime
|
|
||||||
|
|
||||||
data class TeamItemResponse(
|
data class TeamItemResponse(
|
||||||
val id: ItemId,
|
val id: ItemId,
|
||||||
val hunterName: String?,
|
val itemFoundStatus: FoundStatus
|
||||||
val itemFoundStatus: FoundStatus,
|
|
||||||
val itemStatusChangeDateTime: LocalDateTime,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.repository
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.HunterId
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
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
|
||||||
@@ -9,4 +10,5 @@ import java.util.*
|
|||||||
@Repository
|
@Repository
|
||||||
interface HunterTeamRepository : JpaRepository<HunterTeamRecord, UUID> {
|
interface HunterTeamRepository : JpaRepository<HunterTeamRecord, UUID> {
|
||||||
fun findByHunterId(hunterId: HunterId): List<HunterTeamRecord>
|
fun findByHunterId(hunterId: HunterId): List<HunterTeamRecord>
|
||||||
|
fun findByTeamId(teamId: TeamId): List<HunterTeamRecord>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,6 @@ import org.springframework.stereotype.Repository
|
|||||||
@Repository
|
@Repository
|
||||||
interface PhotoRepository : JpaRepository<PhotoRecord, PhotoId> {
|
interface PhotoRepository : JpaRepository<PhotoRecord, PhotoId> {
|
||||||
fun findByItemId(itemId: ItemId): List<PhotoRecord>
|
fun findByItemId(itemId: ItemId): List<PhotoRecord>
|
||||||
|
fun findByHuntIdAndItemId(huntId: HuntId, itemId: ItemId): List<PhotoRecord>
|
||||||
fun findByIdAndItemIdAndHuntId(id: PhotoId, itemId: ItemId, huntId: HuntId): PhotoRecord?
|
fun findByIdAndItemIdAndHuntId(id: PhotoId, itemId: ItemId, huntId: HuntId): PhotoRecord?
|
||||||
}
|
}
|
||||||
@@ -5,17 +5,18 @@ import net.coobird.thumbnailator.tasks.UnsupportedFormatException
|
|||||||
import net.halfbinary.scavengerhuntapi.error.exception.BadFileException
|
import net.halfbinary.scavengerhuntapi.error.exception.BadFileException
|
||||||
import net.halfbinary.scavengerhuntapi.error.exception.ForbiddenException
|
import net.halfbinary.scavengerhuntapi.error.exception.ForbiddenException
|
||||||
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.FoundStatus
|
||||||
import net.halfbinary.scavengerhuntapi.model.HuntId
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
import net.halfbinary.scavengerhuntapi.model.ItemId
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.ImageVersion
|
import net.halfbinary.scavengerhuntapi.model.ImageVersion
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
||||||
import net.halfbinary.scavengerhuntapi.model.TeamId
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
import net.halfbinary.scavengerhuntapi.model.domain.PhotoFile
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
|
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
|
||||||
import net.halfbinary.scavengerhuntapi.model.domain.Photo
|
import net.halfbinary.scavengerhuntapi.model.domain.Photo
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.PhotoFile
|
||||||
import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse
|
import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse
|
||||||
import net.halfbinary.scavengerhuntapi.repository.PhotoRepository
|
import net.halfbinary.scavengerhuntapi.repository.PhotoRepository
|
||||||
import org.springframework.core.io.InputStreamResource
|
import org.springframework.core.io.InputStreamResource
|
||||||
@@ -115,6 +116,30 @@ class PhotoService(
|
|||||||
return PhotoFile(InputStreamResource(stream), MediaType.parseMediaType(contentType))
|
return PhotoFile(InputStreamResource(stream), MediaType.parseMediaType(contentType))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getItemFoundStatus(huntId: HuntId, teamId: TeamId, itemId: ItemId, email: String): FoundStatus {
|
||||||
|
val requestingHunter = hunterService.getHunterByEmail(email)
|
||||||
|
|
||||||
|
if (!requestingHunter.isAdmin) {
|
||||||
|
val team = try {
|
||||||
|
teamService.getTeamForHunterInHunt(huntId, email)
|
||||||
|
} catch (_: NotFoundException) {
|
||||||
|
throw ForbiddenException()
|
||||||
|
}
|
||||||
|
if (team.id != teamId) throw ForbiddenException()
|
||||||
|
}
|
||||||
|
|
||||||
|
val teamHunterIds = teamService.getHunterIdsForTeam(teamId)
|
||||||
|
val photos = photoRepository.findByHuntIdAndItemId(huntId, itemId)
|
||||||
|
.filter { it.hunterId in teamHunterIds && it.status != PhotoStatus.REMOVED }
|
||||||
|
|
||||||
|
return when {
|
||||||
|
photos.any { it.status == PhotoStatus.APPROVED } -> FoundStatus.APPROVED
|
||||||
|
photos.any { it.status == PhotoStatus.REJECTED } -> FoundStatus.REJECTED
|
||||||
|
photos.any { it.status == PhotoStatus.SUBMITTED } -> FoundStatus.SUBMITTED
|
||||||
|
else -> FoundStatus.NOT_FOUND
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun updatePhotoStatus(photoId: PhotoId, status: PhotoStatus) {
|
fun updatePhotoStatus(photoId: PhotoId, status: PhotoStatus) {
|
||||||
val record = photoRepository.findByIdOrNull(photoId)
|
val record = photoRepository.findByIdOrNull(photoId)
|
||||||
?: throw NotFoundException(PHOTO_NOT_FOUND)
|
?: throw NotFoundException(PHOTO_NOT_FOUND)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.halfbinary.scavengerhuntapi.service
|
|||||||
|
|
||||||
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
import net.halfbinary.scavengerhuntapi.model.HuntId
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
import net.halfbinary.scavengerhuntapi.model.TeamId
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
||||||
@@ -49,6 +50,10 @@ class TeamService(
|
|||||||
?: throw NotFoundException("No team found for hunter $email in hunt $huntId")
|
?: throw NotFoundException("No team found for hunter $email in hunt $huntId")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getHunterIdsForTeam(teamId: TeamId): Set<HunterId> {
|
||||||
|
return hunterTeamRepository.findByTeamId(teamId).map { it.hunterId }.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
fun joinTeam(teamId: TeamId, email: String) {
|
fun joinTeam(teamId: TeamId, email: String) {
|
||||||
val hunter = hunterRepository.findByEmail(email) ?: throw NotFoundException("No hunter with email $email found")
|
val hunter = hunterRepository.findByEmail(email) ?: throw NotFoundException("No hunter with email $email found")
|
||||||
hunterTeamRepository.save(HunterTeamRecord(UUID.randomUUID(), hunter.id, teamId))
|
hunterTeamRepository.save(HunterTeamRecord(UUID.randomUUID(), hunter.id, teamId))
|
||||||
|
|||||||
Reference in New Issue
Block a user