package net.halfbinary.scavengerhuntapi.controller import io.swagger.v3.oas.annotations.Operation import jakarta.validation.Valid import net.halfbinary.scavengerhuntapi.model.HuntId 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.request.TeamRequest import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse import net.halfbinary.scavengerhuntapi.model.response.TeamItemResponse import net.halfbinary.scavengerhuntapi.model.response.TeamResponse import net.halfbinary.scavengerhuntapi.service.PhotoService 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.PatchMapping 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.RequestParam import org.springframework.web.bind.annotation.RestController import org.springframework.web.multipart.MultipartFile @RestController @RequestMapping("hunt/{huntId}/team") class TeamController(private val teamService: TeamService, private val photoService: PhotoService) { @GetMapping @Operation(summary = "List all teams for the specified hunt") fun listHuntTeams(@PathVariable huntId: HuntId): ResponseEntity> { return ResponseEntity.ok(teamService.getListOfTeamsForHunt(huntId).map { it.toResponse()}) } @PostMapping @Operation(summary = "Create a new team for the specified hunt") fun createHuntTeam(@PathVariable huntId: HuntId, @Valid @RequestBody team: TeamRequest) { val teamResponse = teamService.createTeam(team.name) teamService.addTeamToHunt(huntId, teamResponse.id) } @GetMapping("/{teamId}") @Operation(summary = "Get team info for the specified hunt") fun getTeam(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId): ResponseEntity { return ResponseEntity.ok(teamService.getTeamFromHunt(huntId, teamId).toResponse()) } @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, @PathVariable teamId: TeamId, @PathVariable itemId: ItemId, authentication: Authentication): ResponseEntity { val foundStatus = photoService.getItemFoundStatus(huntId, teamId, itemId, authentication.name) return ResponseEntity.ok(TeamItemResponse(id = itemId, itemFoundStatus = foundStatus)) } @GetMapping("/{teamId}/item/{itemId}/photo") @Operation(summary = "Get list of photo information for the specified Team, Hunt, and Item") fun getItemPhotos(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId, @PathVariable itemId: ItemId, authentication: Authentication): ResponseEntity> { return ResponseEntity.ok(photoService.getItemPhotos(huntId, teamId, itemId, authentication.name)) } @PatchMapping("/{teamId}/item/{itemId}/photo/{photoId}") @Operation(summary = "Mark the specified Photo as removed") fun removePhoto(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId, @PathVariable itemId: ItemId, @PathVariable photoId: PhotoId, authentication: Authentication) { photoService.removePhoto(huntId, teamId, itemId, photoId, authentication.name) } @GetMapping("/{teamId}/item/{itemId}/photo/{photoId}") @Operation(summary = "Get photo information for the specified Team, Hunt, Item, and Photo") fun getPhotoInfo(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId, @PathVariable itemId: ItemId, @PathVariable photoId: PhotoId, authentication: Authentication): ResponseEntity { return ResponseEntity.ok(photoService.getPhotoInfo(huntId, teamId, itemId, photoId, authentication.name)) } @PostMapping("/{teamId}/item/{itemId}/photo") @Operation(summary = "Save photo information and store the binary file") fun submitPhoto(@PathVariable huntId: HuntId, @PathVariable teamId: TeamId, @PathVariable itemId: ItemId, authentication: Authentication, @RequestParam file: MultipartFile) { photoService.submitPhoto(huntId, itemId, authentication.name, file) } }