Compare commits
2 Commits
48b2ffd7b2
...
8ff73cda2b
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ff73cda2b | |||
| 08d0b1730a |
@@ -8,11 +8,15 @@ import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
|
||||
import net.halfbinary.scavengerhuntapi.model.request.ItemRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.request.ItemUpdateRequest
|
||||
import net.halfbinary.scavengerhuntapi.model.response.ItemResponse
|
||||
import net.halfbinary.scavengerhuntapi.service.HuntService
|
||||
import org.springframework.http.ResponseEntity
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.security.core.Authentication
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
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
|
||||
@@ -24,8 +28,8 @@ import org.springframework.web.bind.annotation.RestController
|
||||
class ItemController(private val huntService: HuntService) {
|
||||
|
||||
@GetMapping
|
||||
fun getItemsForHunt(@PathVariable huntId: HuntId): ResponseEntity<List<ItemResponse>> {
|
||||
return ResponseEntity.ok(huntService.getItemsForHunt(huntId).map { it.toResponse() })
|
||||
fun getItemsForHunt(@PathVariable huntId: HuntId, authentication: Authentication): ResponseEntity<List<ItemResponse>> {
|
||||
return ResponseEntity.ok(huntService.getItemsForHunt(huntId, authentication.name).map { it.toResponse() })
|
||||
}
|
||||
|
||||
@GetMapping("/{itemId}")
|
||||
@@ -41,4 +45,21 @@ class ItemController(private val huntService: HuntService) {
|
||||
return ResponseEntity.ok(huntService.addItemToHunt(huntId, body.toDomain()).toResponse())
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@Tag(name = "Admin")
|
||||
@PatchMapping("/{itemId}")
|
||||
@Operation(summary = "Updates name and/or points for the specified Item in the specified Hunt")
|
||||
fun updateItem(@PathVariable huntId: HuntId, @PathVariable itemId: ItemId, @RequestBody body: ItemUpdateRequest): ResponseEntity<ItemResponse> {
|
||||
return ResponseEntity.ok(huntService.updateItem(huntId, itemId, body).toResponse())
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@Tag(name = "Admin")
|
||||
@DeleteMapping("/{itemId}")
|
||||
@Operation(summary = "Deletes the specified Item from the specified Hunt")
|
||||
fun deleteItem(@PathVariable huntId: HuntId, @PathVariable itemId: ItemId): ResponseEntity<Unit> {
|
||||
huntService.deleteItem(huntId, itemId)
|
||||
return ResponseEntity.noContent().build()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,4 +10,7 @@ data class Hunt(
|
||||
val startDateTime: LocalDateTime,
|
||||
val endDateTime: LocalDateTime,
|
||||
val isTerminated: Boolean
|
||||
)
|
||||
) {
|
||||
val isOngoing: Boolean
|
||||
get() = !isTerminated && startDateTime < LocalDateTime.now() && endDateTime > LocalDateTime.now()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package net.halfbinary.scavengerhuntapi.model.request
|
||||
|
||||
data class ItemUpdateRequest(
|
||||
val name: String?,
|
||||
val points: Int?
|
||||
)
|
||||
@@ -1,9 +1,13 @@
|
||||
package net.halfbinary.scavengerhuntapi.repository
|
||||
|
||||
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||
import net.halfbinary.scavengerhuntapi.model.record.HuntItemRecord
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.util.*
|
||||
|
||||
@Repository
|
||||
interface HuntItemRepository : JpaRepository<HuntItemRecord, UUID>
|
||||
interface HuntItemRepository : JpaRepository<HuntItemRecord, UUID> {
|
||||
fun findByHuntIdAndItemId(huntId: HuntId, itemId: ItemId): HuntItemRecord?
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package net.halfbinary.scavengerhuntapi.service
|
||||
|
||||
import net.halfbinary.scavengerhuntapi.error.exception.ForbiddenException
|
||||
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
||||
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.ItemUpdateRequest
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntItemRepository
|
||||
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
||||
import net.halfbinary.scavengerhuntapi.repository.ItemRepository
|
||||
@@ -20,7 +23,8 @@ import java.time.LocalDateTime
|
||||
class HuntService(
|
||||
private val huntRepository: HuntRepository,
|
||||
private val itemRepository: ItemRepository,
|
||||
private val huntItemRepository: HuntItemRepository
|
||||
private val huntItemRepository: HuntItemRepository,
|
||||
private val hunterService: HunterService
|
||||
) {
|
||||
fun getHunt(huntId: HuntId): Hunt {
|
||||
return huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id $huntId found")
|
||||
@@ -64,8 +68,10 @@ class HuntService(
|
||||
return huntRepository.save(hunt.toRecord()).toDomain()
|
||||
}
|
||||
|
||||
fun getItemsForHunt(huntId: HuntId): List<Item> {
|
||||
huntRepository.findByIdOrNull(huntId) ?: throw NotFoundException("No hunt with id $huntId found")
|
||||
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)
|
||||
if (!hunter.isAdmin && !hunt.isOngoing) throw ForbiddenException()
|
||||
return itemRepository.findAllByHuntId(huntId).map { it.toDomain() }
|
||||
}
|
||||
|
||||
@@ -75,4 +81,23 @@ class HuntService(
|
||||
huntItemRepository.save(HuntItem(huntId = huntId, itemId = savedItem.id).toRecord())
|
||||
return savedItem
|
||||
}
|
||||
|
||||
fun updateItem(huntId: HuntId, itemId: ItemId, request: ItemUpdateRequest): Item {
|
||||
huntItemRepository.findByHuntIdAndItemId(huntId, itemId)
|
||||
?: throw NotFoundException("No item with id $itemId found in hunt $huntId")
|
||||
val existing = itemRepository.findByIdOrNull(itemId)
|
||||
?: throw NotFoundException("No item with id $itemId found")
|
||||
val updated = existing.copy(
|
||||
name = request.name ?: existing.name,
|
||||
points = request.points ?: existing.points
|
||||
)
|
||||
return itemRepository.save(updated).toDomain()
|
||||
}
|
||||
|
||||
fun deleteItem(huntId: HuntId, itemId: ItemId) {
|
||||
val huntItem = huntItemRepository.findByHuntIdAndItemId(huntId, itemId)
|
||||
?: throw NotFoundException("No item with id $itemId found in hunt $huntId")
|
||||
huntItemRepository.delete(huntItem)
|
||||
itemRepository.deleteById(itemId)
|
||||
}
|
||||
}
|
||||
@@ -36,10 +36,15 @@ class PhotoService(
|
||||
private val photoRepository: PhotoRepository,
|
||||
private val hunterService: HunterService,
|
||||
private val teamService: TeamService,
|
||||
private val huntService: HuntService,
|
||||
private val s3StorageService: S3StorageService,
|
||||
private val fileProbeService: FileProbeService
|
||||
) {
|
||||
fun submitPhoto(huntId: HuntId, itemId: ItemId, email: String, file: MultipartFile) {
|
||||
val hunter = hunterService.getHunterByEmail(email)
|
||||
val hunt = huntService.getHunt(huntId)
|
||||
if (!hunter.isAdmin && !hunt.isOngoing) throw ForbiddenException()
|
||||
|
||||
val originalBytes = file.bytes
|
||||
val fileType = fileProbeService.getFileType(originalBytes)
|
||||
|
||||
@@ -51,7 +56,6 @@ class PhotoService(
|
||||
throw BadFileException("Image type is not supported")
|
||||
}
|
||||
|
||||
val hunter = hunterService.getHunterByEmail(email)
|
||||
val now = LocalDateTime.now()
|
||||
val photo = Photo(
|
||||
itemId = itemId,
|
||||
@@ -76,6 +80,8 @@ class PhotoService(
|
||||
?: throw NotFoundException(PHOTO_NOT_FOUND)
|
||||
|
||||
if (!requestingHunter.isAdmin) {
|
||||
val hunt = huntService.getHunt(huntId)
|
||||
if (!hunt.isOngoing) throw ForbiddenException()
|
||||
val team = try {
|
||||
teamService.getTeamForHunterInHunt(huntId, email)
|
||||
} catch (_: NotFoundException) {
|
||||
@@ -121,6 +127,8 @@ class PhotoService(
|
||||
val requestingHunter = hunterService.getHunterByEmail(email)
|
||||
|
||||
if (!requestingHunter.isAdmin) {
|
||||
val hunt = huntService.getHunt(huntId)
|
||||
if (!hunt.isOngoing) throw ForbiddenException()
|
||||
val team = try {
|
||||
teamService.getTeamForHunterInHunt(huntId, email)
|
||||
} catch (_: NotFoundException) {
|
||||
@@ -142,15 +150,24 @@ class PhotoService(
|
||||
}
|
||||
|
||||
fun removePhoto(huntId: HuntId, teamId: TeamId, itemId: ItemId, photoId: PhotoId, email: String) {
|
||||
val requestingHunter = hunterService.getHunterByEmail(email)
|
||||
|
||||
if (!requestingHunter.isAdmin) {
|
||||
val hunt = huntService.getHunt(huntId)
|
||||
if (!hunt.isOngoing) throw ForbiddenException()
|
||||
}
|
||||
|
||||
val photoRecord = photoRepository.findByIdAndItemIdAndHuntId(photoId, itemId, huntId)
|
||||
?: throw NotFoundException(PHOTO_NOT_FOUND)
|
||||
|
||||
val team = try {
|
||||
teamService.getTeamForHunterInHunt(huntId, email)
|
||||
} catch (_: NotFoundException) {
|
||||
throw ForbiddenException()
|
||||
if (!requestingHunter.isAdmin) {
|
||||
val team = try {
|
||||
teamService.getTeamForHunterInHunt(huntId, email)
|
||||
} catch (_: NotFoundException) {
|
||||
throw ForbiddenException()
|
||||
}
|
||||
if (team.id != teamId) throw ForbiddenException()
|
||||
}
|
||||
if (team.id != teamId) throw ForbiddenException()
|
||||
|
||||
if (photoRecord.status == PhotoStatus.APPROVED) throw ConflictException("Cannot remove an approved photo")
|
||||
|
||||
@@ -161,6 +178,8 @@ class PhotoService(
|
||||
val requestingHunter = hunterService.getHunterByEmail(email)
|
||||
|
||||
if (!requestingHunter.isAdmin) {
|
||||
val hunt = huntService.getHunt(huntId)
|
||||
if (!hunt.isOngoing) throw ForbiddenException()
|
||||
val team = try {
|
||||
teamService.getTeamForHunterInHunt(huntId, email)
|
||||
} catch (_: NotFoundException) {
|
||||
|
||||
Reference in New Issue
Block a user