Compare commits
18 Commits
9633d95e75
...
feature/ge
| Author | SHA1 | Date | |
|---|---|---|---|
| b349380c93 | |||
| 1585b6eb7d | |||
| 1dd904055c | |||
| 5ca7a685dd | |||
| 863c824421 | |||
| 9324cf2eb0 | |||
| 30c66527b9 | |||
| 46132bb4fd | |||
| b2ba9ce676 | |||
| 4a1077833e | |||
| 1ed64cadd9 | |||
| aff7cd1e28 | |||
| fd754a7ee7 | |||
| 46a78bfc08 | |||
| ab34f16a45 | |||
| 2e0244e1ee | |||
| 0c01c5dbcc | |||
| 69e874c9f2 |
17
README.md
17
README.md
@@ -10,16 +10,9 @@ REST API to support a community scavenger hunt app.
|
|||||||
|
|
||||||
## TODO:
|
## TODO:
|
||||||
### User Endpoints
|
### User Endpoints
|
||||||
* list teams for hunt GET /hunt/{id}/team
|
* upload photo for hunt item POST `/hunt/{huntId}/team/{teamId}/item/{itemId}/photo` - body: image binary
|
||||||
* create new hunt team POST /hunt/{id}/team
|
* delete photo for hunt item DELETE `/hunt/{huntId}/team/{teamId}/item/{itemId}/photo/{photoId}`
|
||||||
* join hunt team POST /hunt/{id}/team/{id}
|
* list hunt teams with scores for hunt `GET /lead/hunt/{huntId}/team`
|
||||||
* list items for hunt GET /hunt/{id}/item
|
* list hunters with scores for hunt GET `/lead/hunt/{huntId}/hunter`
|
||||||
* get hunt item info GET /hunt/{id}/item/{id}
|
|
||||||
* get hunt team item info GET /hunt/{id}/team/{id}/item/{id}
|
|
||||||
* get photos for hunt item GET /hunt/{id}/team/{id}/item/{id}/photo
|
|
||||||
* upload photo for hunt item POST /hunt/{id}/team/{id}/item/{id}/photo
|
|
||||||
* delete photo for hunt item DELETE /hunt/{id}/team/{id}/item/{id}/photo
|
|
||||||
* list hunt teams with scores for hunt GET /lead/hunt/{id}/team
|
|
||||||
* list hunters with scores for hunt GET /lead/hunt/{id}/hunter
|
|
||||||
### Admin Endpoints
|
### Admin Endpoints
|
||||||
* approve photo for hunt item POST /admin/hunt/{id}/team/{id}
|
* approve photo for hunt item POST `/admin/hunt/{huntId}/team/{teamId}/item/{itemId}/photo/{photoId}` - body: approval status
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "2.2.21"
|
kotlin("jvm") version "2.3.21"
|
||||||
kotlin("plugin.spring") version "2.2.21"
|
kotlin("plugin.spring") version "2.3.21"
|
||||||
id("org.springframework.boot") version "4.0.0"
|
id("org.springframework.boot") version "4.0.6"
|
||||||
id("io.spring.dependency-management") version "1.1.7"
|
id("io.spring.dependency-management") version "1.1.7"
|
||||||
kotlin("plugin.jpa") version "2.2.21"
|
kotlin("plugin.jpa") version "2.3.21"
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "net.halfbinary"
|
group = "net.halfbinary"
|
||||||
@@ -27,23 +27,32 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
val mysqlConnectorJ = "9.5.0"
|
val mariaDriver = "3.5.8"
|
||||||
val commonsValidator = "1.10.1"
|
val commonsValidator = "1.10.1"
|
||||||
val jakartaValidation = "3.1.1"
|
val jakartaValidation = "3.1.1"
|
||||||
val jsonWebToken = "0.13.0"
|
val jsonWebToken = "0.13.0"
|
||||||
|
val springdocUi = "3.0.3"
|
||||||
|
val awsSdk = "2.26.0"
|
||||||
|
val thumbnailator = "0.4.20"
|
||||||
|
val tika = "3.3.0"
|
||||||
implementation("org.springframework.boot:spring-boot-starter-actuator")
|
implementation("org.springframework.boot:spring-boot-starter-actuator")
|
||||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||||
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
|
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
|
||||||
implementation("org.springframework.boot:spring-boot-starter-validation")
|
implementation("org.springframework.boot:spring-boot-starter-validation")
|
||||||
implementation("org.springframework.boot:spring-boot-starter-security")
|
implementation("org.springframework.boot:spring-boot-starter-security")
|
||||||
implementation("jakarta.validation:jakarta.validation-api:${jakartaValidation}")
|
implementation("jakarta.validation:jakarta.validation-api:$jakartaValidation")
|
||||||
implementation("com.mysql:mysql-connector-j:${mysqlConnectorJ}")
|
implementation("org.mariadb.jdbc:mariadb-java-client:${mariaDriver}")
|
||||||
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
|
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
|
||||||
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
||||||
implementation("commons-validator:commons-validator:${commonsValidator}")
|
implementation("commons-validator:commons-validator:$commonsValidator")
|
||||||
implementation("io.jsonwebtoken:jjwt-api:${jsonWebToken}")
|
implementation("io.jsonwebtoken:jjwt-api:$jsonWebToken")
|
||||||
implementation("io.jsonwebtoken:jjwt-impl:${jsonWebToken}")
|
implementation("io.jsonwebtoken:jjwt-impl:$jsonWebToken")
|
||||||
implementation("io.jsonwebtoken:jjwt-jackson:${jsonWebToken}")
|
implementation("io.jsonwebtoken:jjwt-jackson:$jsonWebToken")
|
||||||
|
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:$springdocUi")
|
||||||
|
implementation(platform("software.amazon.awssdk:bom:$awsSdk"))
|
||||||
|
implementation("software.amazon.awssdk:s3")
|
||||||
|
implementation("net.coobird:thumbnailator:$thumbnailator")
|
||||||
|
implementation("org.apache.tika:tika-core:$tika")
|
||||||
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
||||||
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
|
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
|
||||||
testImplementation("org.springframework.boot:spring-boot-starter-actuator-test")
|
testImplementation("org.springframework.boot:spring-boot-starter-actuator-test")
|
||||||
|
|||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.0-bin.zip
|
||||||
networkTimeout=10000
|
networkTimeout=10000
|
||||||
validateDistributionUrl=true
|
validateDistributionUrl=true
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
|||||||
2
gradlew
vendored
2
gradlew
vendored
@@ -57,7 +57,7 @@
|
|||||||
# Darwin, MinGW, and NonStop.
|
# Darwin, MinGW, and NonStop.
|
||||||
#
|
#
|
||||||
# (3) This script is generated from the Groovy template
|
# (3) This script is generated from the Groovy template
|
||||||
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
# https://github.com/gradle/gradle/blob/b631911858264c0b6e4d6603d677ff5218766cee/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
# within the Gradle project.
|
# within the Gradle project.
|
||||||
#
|
#
|
||||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
|
|||||||
@@ -31,7 +31,9 @@ class AuthTokenFilter(private val jwtUtils: JwtUtil, private val hunterDetailsSe
|
|||||||
userDetails.authorities
|
userDetails.authorities
|
||||||
)
|
)
|
||||||
authentication.details = WebAuthenticationDetailsSource().buildDetails(request)
|
authentication.details = WebAuthenticationDetailsSource().buildDetails(request)
|
||||||
SecurityContextHolder.getContext().authentication = authentication
|
val context = SecurityContextHolder.createEmptyContext()
|
||||||
|
context.authentication = authentication
|
||||||
|
SecurityContextHolder.setContext(context)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
println("Cannot set user authentication: $e")
|
println("Cannot set user authentication: $e")
|
||||||
|
|||||||
@@ -5,13 +5,14 @@ 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
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
class JwtUtil {
|
class JwtUtil {
|
||||||
@Value($$"${jwt.secret}")
|
@Value($$"${jwt.secret}")
|
||||||
private val jwtSecret: String? = null
|
private val jwtSecret: String = ""
|
||||||
|
|
||||||
@Value($$"${jwt.expiration}")
|
@Value($$"${jwt.expiration}")
|
||||||
private val jwtExpirationMs = 0
|
private val jwtExpirationMs = 0
|
||||||
@@ -22,7 +23,7 @@ class JwtUtil {
|
|||||||
// preventing the repeated creation of the key and enhancing performance
|
// preventing the repeated creation of the key and enhancing performance
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
fun init() {
|
fun init() {
|
||||||
this.key = Jwts.SIG.HS256.key().build()
|
this.key = SecretKeySpec(jwtSecret.toByteArray(Charsets.UTF_8), "HmacSHA256")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate JWT token
|
// Generate JWT token
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.config
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
|
||||||
|
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
|
||||||
|
import software.amazon.awssdk.regions.Region
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client
|
||||||
|
import software.amazon.awssdk.services.s3.S3Configuration
|
||||||
|
import java.net.URI
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
class S3Config(
|
||||||
|
@Value("\${minio.endpoint}") private val endpoint: String,
|
||||||
|
@Value("\${minio.access-key}") private val accessKey: String,
|
||||||
|
@Value("\${minio.secret-key}") private val secretKey: String
|
||||||
|
) {
|
||||||
|
@Bean
|
||||||
|
fun s3Client(): S3Client = S3Client.builder()
|
||||||
|
.endpointOverride(URI.create(endpoint))
|
||||||
|
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)))
|
||||||
|
.region(Region.US_EAST_1)
|
||||||
|
.serviceConfiguration(S3Configuration.builder().pathStyleAccessEnabled(true).build())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.config
|
package net.halfbinary.scavengerhuntapi.config
|
||||||
|
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
import org.springframework.security.authentication.AuthenticationManager
|
import org.springframework.security.authentication.AuthenticationManager
|
||||||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
|
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
|
||||||
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
||||||
import org.springframework.security.config.annotation.web.configurers.CorsConfigurer
|
import org.springframework.security.config.annotation.web.configurers.CorsConfigurer
|
||||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer
|
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer
|
||||||
@@ -17,7 +19,7 @@ import org.springframework.security.web.authentication.UsernamePasswordAuthentic
|
|||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
//@EnableWebSecurity
|
@EnableMethodSecurity
|
||||||
class SecurityConfig(private val authEntrypointJwt: AuthEntrypointJwt,
|
class SecurityConfig(private val authEntrypointJwt: AuthEntrypointJwt,
|
||||||
private val authTokenFilter: AuthTokenFilter) {
|
private val authTokenFilter: AuthTokenFilter) {
|
||||||
|
|
||||||
@@ -26,6 +28,13 @@ class SecurityConfig(private val authEntrypointJwt: AuthEntrypointJwt,
|
|||||||
return authTokenFilter
|
return authTokenFilter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun authTokenFilterRegistration(): FilterRegistrationBean<AuthTokenFilter> {
|
||||||
|
val registration = FilterRegistrationBean(authTokenFilter)
|
||||||
|
registration.isEnabled = false
|
||||||
|
return registration
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun authenticationManager(
|
fun authenticationManager(
|
||||||
@@ -58,7 +67,7 @@ class SecurityConfig(private val authEntrypointJwt: AuthEntrypointJwt,
|
|||||||
}
|
}
|
||||||
.authorizeHttpRequests { authorizeRequests ->
|
.authorizeHttpRequests { authorizeRequests ->
|
||||||
authorizeRequests
|
authorizeRequests
|
||||||
.requestMatchers("/auth/**", "/signup")
|
.requestMatchers("/auth/**", "/signup", "/docs/**")
|
||||||
.permitAll()
|
.permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,39 +1,29 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.controller
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletResponse
|
|
||||||
import jakarta.validation.Valid
|
import jakarta.validation.Valid
|
||||||
import net.halfbinary.scavengerhuntapi.config.JwtUtil
|
import net.halfbinary.scavengerhuntapi.config.JwtUtil
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.converter.toRefreshResponse
|
||||||
import net.halfbinary.scavengerhuntapi.model.request.LoginRequest
|
import net.halfbinary.scavengerhuntapi.model.request.LoginRequest
|
||||||
import net.halfbinary.scavengerhuntapi.model.request.LogoutRequest
|
import net.halfbinary.scavengerhuntapi.model.request.LogoutRequest
|
||||||
import net.halfbinary.scavengerhuntapi.model.request.RefreshRequest
|
import net.halfbinary.scavengerhuntapi.model.request.RefreshRequest
|
||||||
import net.halfbinary.scavengerhuntapi.model.response.LoginResponse
|
import net.halfbinary.scavengerhuntapi.model.response.LoginResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.RefreshResponse
|
||||||
import net.halfbinary.scavengerhuntapi.service.LoginService
|
import net.halfbinary.scavengerhuntapi.service.LoginService
|
||||||
import net.halfbinary.scavengerhuntapi.service.RefreshTokenService
|
import net.halfbinary.scavengerhuntapi.service.RefreshTokenService
|
||||||
import org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
|
||||||
import org.springframework.security.core.userdetails.User
|
|
||||||
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.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import java.util.Collections
|
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/auth")
|
@RequestMapping("/auth")
|
||||||
class AuthController(private val loginService: LoginService, private val jwtUtils: JwtUtil, private val refreshTokenService: RefreshTokenService) {
|
class AuthController(private val loginService: LoginService, private val jwtUtils: JwtUtil, private val refreshTokenService: RefreshTokenService) {
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
fun login(@Valid @RequestBody body: LoginRequest, response: HttpServletResponse): ResponseEntity<LoginResponse> {
|
fun login(@Valid @RequestBody body: LoginRequest): ResponseEntity<LoginResponse> {
|
||||||
val result = loginService.login(body.toDomain())
|
val result = loginService.login(body.toDomain())
|
||||||
// TODO: Figure out how to use the authorities
|
|
||||||
val hunterAuthorities =
|
|
||||||
if (result.isAdmin) {
|
|
||||||
SimpleGrantedAuthority("ROLE_ADMIN")
|
|
||||||
} else {
|
|
||||||
SimpleGrantedAuthority("ROLE_USER")
|
|
||||||
}
|
|
||||||
val user = User(result.email, result.password, Collections.singleton(hunterAuthorities))
|
|
||||||
val accessToken = jwtUtils.generateToken(result.email)
|
val accessToken = jwtUtils.generateToken(result.email)
|
||||||
val refreshToken = refreshTokenService.generateRefreshToken(result.email)
|
val refreshToken = refreshTokenService.generateRefreshToken(result.email)
|
||||||
val loginResponse = LoginResponse(accessToken, refreshToken)
|
val loginResponse = LoginResponse(accessToken, refreshToken)
|
||||||
@@ -41,12 +31,12 @@ class AuthController(private val loginService: LoginService, private val jwtUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/refresh")
|
@PostMapping("/refresh")
|
||||||
fun refresh(@RequestBody body: RefreshRequest): String {
|
fun refresh(@RequestBody body: RefreshRequest): ResponseEntity<RefreshResponse> {
|
||||||
return refreshTokenService.getAccessToken(body.refreshToken)
|
return ResponseEntity.ok(refreshTokenService.getAccessToken(body.refreshToken).toRefreshResponse())
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
fun logout(@RequestBody body: LogoutRequest, response: HttpServletResponse): ResponseEntity<String> {
|
fun logout(@RequestBody body: LogoutRequest): ResponseEntity<String> {
|
||||||
refreshTokenService.removeToken(body.refreshToken)
|
refreshTokenService.removeToken(body.refreshToken)
|
||||||
return ResponseEntity.ok().build()
|
return ResponseEntity.ok().build()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.controller
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag
|
||||||
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.HunterId
|
||||||
@@ -10,29 +12,51 @@ 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 org.springframework.http.ResponseEntity
|
import org.springframework.http.ResponseEntity
|
||||||
import org.springframework.web.bind.annotation.*
|
import org.springframework.security.access.prepost.PreAuthorize
|
||||||
|
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.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) {
|
class HuntController(private val huntService: HuntService) {
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
|
@Operation(summary = "Gets the specified hunt information")
|
||||||
fun getHunt(@PathVariable("id") huntId: HuntId): ResponseEntity<HuntResponse> {
|
fun getHunt(@PathVariable("id") huntId: HuntId): ResponseEntity<HuntResponse> {
|
||||||
return ResponseEntity.ok(huntService.getHunt(huntId).toResponse())
|
return ResponseEntity.ok(huntService.getHunt(huntId).toResponse())
|
||||||
}
|
}
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
@GetMapping()
|
@Tag(name = "Admin")
|
||||||
|
@GetMapping
|
||||||
|
@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() })
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping()
|
@GetMapping("/unstarted")
|
||||||
|
@Operation(summary = "Gets list of all upcoming Hunts")
|
||||||
|
fun getUnstartedHunts(): ResponseEntity<List<HuntResponse>> {
|
||||||
|
return ResponseEntity.ok(huntService.getAllHunts(HuntStatus.UNSTARTED).map { it.toResponse() })
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@Tag(name = "Admin")
|
||||||
|
@PostMapping
|
||||||
|
@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())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@Tag(name = "Admin")
|
||||||
@GetMapping("/hunter/{hunterId}")
|
@GetMapping("/hunter/{hunterId}")
|
||||||
fun getHuntsByHunter(@PathVariable("hunterId") hunterId: HunterId): ResponseEntity<List<HuntResponse>> {
|
@Operation(summary = "Lists all Hunts for specified Hunter")
|
||||||
|
fun getHuntsByHunter(@PathVariable hunterId: HunterId): ResponseEntity<List<HuntResponse>> {
|
||||||
return ResponseEntity.ok(huntService.getHuntsByHunter(hunterId).map { it.toResponse() })
|
return ResponseEntity.ok(huntService.getHuntsByHunter(hunterId).map { it.toResponse() })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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> {
|
||||||
|
return ResponseEntity.ok(teamService.getTeamForHunterInHunt(huntId, authentication.name).toResponse())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag
|
||||||
|
import jakarta.validation.Valid
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
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.response.ItemResponse
|
||||||
|
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.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
|
||||||
|
@RequestMapping("hunt/{huntId}/item")
|
||||||
|
class ItemController(private val huntService: HuntService) {
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
fun getItemsForHunt(@PathVariable huntId: HuntId): ResponseEntity<List<ItemResponse>> {
|
||||||
|
return ResponseEntity.ok(huntService.getItemsForHunt(huntId).map { it.toResponse() })
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{itemId}")
|
||||||
|
fun getItem(@PathVariable huntId: HuntId, @PathVariable itemId: ItemId): ResponseEntity<ItemResponse> {
|
||||||
|
TODO("Get detailed information about the specified Item for the specified Hunt")
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@Tag(name = "Admin")
|
||||||
|
@PostMapping
|
||||||
|
@Operation(summary = "Adds new Item to specified Hunt")
|
||||||
|
fun addItemToHunt(@PathVariable huntId: HuntId, @Valid @RequestBody body: ItemRequest): ResponseEntity<ItemResponse> {
|
||||||
|
return ResponseEntity.ok(huntService.addItemToHunt(huntId, body.toDomain()).toResponse())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,27 +1,96 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.controller
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
|
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.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.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.model.response.TeamResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.service.PhotoService
|
||||||
|
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.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
import org.springframework.web.bind.annotation.PathVariable
|
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/{id}/team")
|
@RequestMapping("hunt/{huntId}/team")
|
||||||
class TeamController {
|
class TeamController(private val teamService: TeamService, private val photoService: PhotoService) {
|
||||||
@GetMapping
|
@GetMapping
|
||||||
fun listHuntTeams(@PathVariable id: HuntId): ResponseEntity<List<TeamResponse>> {
|
@Operation(summary = "List all teams for the specified hunt")
|
||||||
TODO()
|
fun listHuntTeams(@PathVariable huntId: HuntId): ResponseEntity<List<TeamResponse>> {
|
||||||
|
return ResponseEntity.ok(teamService.getListOfTeamsForHunt(huntId).map { it.toResponse()})
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
fun createHuntTeam(@PathVariable id: HuntId, @Valid @RequestBody team: TeamRequest) {
|
@Operation(summary = "Create a new team for the specified hunt")
|
||||||
TODO()
|
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<TeamResponse> {
|
||||||
|
return ResponseEntity.ok(teamService.getTeamFromHunt(huntId, teamId).toResponse())
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{teamId}/item/{itemId}")
|
||||||
|
@Operation(summary = "Get found/not found status and photo information about the Item for the specified Team, Hunt, and Item")
|
||||||
|
fun getItemForTeam(@PathVariable huntId: HuntId,
|
||||||
|
@PathVariable teamId: TeamId,
|
||||||
|
@PathVariable itemId: ItemId): ResponseEntity<TeamItemResponse> {
|
||||||
|
TODO("Get found/not found status about the Item for the specified Team, Hunt, and Item")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@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): ResponseEntity<List<PhotoResponse>> {
|
||||||
|
TODO("Get list of photo information for the specified Team, Hunt, and Item")
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<PhotoResponse> {
|
||||||
|
return ResponseEntity.ok(photoService.getPhotoInfo(huntId, teamId, itemId, photoId, authentication.name))
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{teamId}/item/{itemId}/photo/{photoId}/file")
|
||||||
|
@Operation(summary = "Get the binary image information for the specified Team, Hunt, Item, and Photo")
|
||||||
|
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")
|
||||||
|
@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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.error
|
package net.halfbinary.scavengerhuntapi.error
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.BadFileException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.ForbiddenException
|
||||||
import net.halfbinary.scavengerhuntapi.error.exception.InvalidEmailException
|
import net.halfbinary.scavengerhuntapi.error.exception.InvalidEmailException
|
||||||
import net.halfbinary.scavengerhuntapi.error.exception.LoginFailedException
|
import net.halfbinary.scavengerhuntapi.error.exception.LoginFailedException
|
||||||
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
@@ -12,6 +14,8 @@ import org.springframework.web.bind.MethodArgumentNotValidException
|
|||||||
import org.springframework.web.bind.annotation.ExceptionHandler
|
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus
|
import org.springframework.web.bind.annotation.ResponseStatus
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice
|
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||||
|
import org.springframework.web.multipart.MaxUploadSizeExceededException
|
||||||
|
import java.net.SocketTimeoutException
|
||||||
|
|
||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
@@ -43,6 +47,12 @@ class ExceptionHandler {
|
|||||||
return e.message
|
return e.message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(ForbiddenException::class)
|
||||||
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||||
|
fun forbiddenException(e: ForbiddenException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
@ExceptionHandler(HttpMessageNotReadableException::class)
|
@ExceptionHandler(HttpMessageNotReadableException::class)
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
fun httpMessageNotReadableException(e: HttpMessageNotReadableException): Map<String, String?> {
|
fun httpMessageNotReadableException(e: HttpMessageNotReadableException): Map<String, String?> {
|
||||||
@@ -68,6 +78,24 @@ class ExceptionHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(BadFileException::class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
fun badFileException(e: BadFileException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MaxUploadSizeExceededException::class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
fun maxUploadSizeExceededException(e: MaxUploadSizeExceededException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(SocketTimeoutException::class)
|
||||||
|
@ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
|
||||||
|
fun socketTimeoutException(): String {
|
||||||
|
return "Unable to connect. Try again later."
|
||||||
|
}
|
||||||
|
|
||||||
private fun simpleMap(key: String, value: String?): Map<String, String?> {
|
private fun simpleMap(key: String, value: String?): Map<String, String?> {
|
||||||
return mapOf(Pair(key, value))
|
return mapOf(Pair(key, value))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
class BadFileException(override val message: String): RuntimeException(message)
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
class ForbiddenException(override val message: String): RuntimeException(message)
|
||||||
@@ -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.")
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.model
|
package net.halfbinary.scavengerhuntapi.model
|
||||||
|
|
||||||
enum class FoundStatus {
|
enum class FoundStatus {
|
||||||
|
NOT_FOUND,
|
||||||
SUBMITTED,
|
SUBMITTED,
|
||||||
APPROVED,
|
APPROVED,
|
||||||
REJECTED,
|
REJECTED,
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model
|
||||||
|
|
||||||
|
enum class PhotoStatus {
|
||||||
|
SUBMITTED,
|
||||||
|
APPROVED,
|
||||||
|
REJECTED,
|
||||||
|
REMOVED
|
||||||
|
}
|
||||||
@@ -2,9 +2,10 @@ package net.halfbinary.scavengerhuntapi.model
|
|||||||
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
typealias FoundId = UUID
|
|
||||||
typealias HuntId = UUID
|
typealias HuntId = UUID
|
||||||
typealias HunterId = UUID
|
typealias HunterId = UUID
|
||||||
typealias ItemId = UUID
|
typealias ItemId = UUID
|
||||||
typealias TeamId = UUID
|
typealias TeamId = UUID
|
||||||
typealias RefreshId = UUID
|
typealias RefreshId = UUID
|
||||||
|
typealias TeamHuntId = UUID
|
||||||
|
typealias PhotoId = UUID
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.HuntItem
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.HuntItemRecord
|
||||||
|
|
||||||
|
fun HuntItem.toRecord() = HuntItemRecord(id = id, huntId = huntId, itemId = itemId)
|
||||||
|
|
||||||
|
fun HuntItemRecord.toDomain() = HuntItem(id = id, huntId = huntId, itemId = itemId)
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Item
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.ItemRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.ItemRequest
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.ItemResponse
|
||||||
|
|
||||||
|
fun ItemRequest.toDomain() = Item(name = name, points = points)
|
||||||
|
|
||||||
|
fun Item.toRecord() = ItemRecord(id = id, name = name, points = points)
|
||||||
|
|
||||||
|
fun ItemRecord.toDomain() = Item(id = id, name = name, points = points)
|
||||||
|
|
||||||
|
fun Item.toResponse() = ItemResponse(id = id, name = name, points = points)
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Photo
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.PhotoRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse
|
||||||
|
|
||||||
|
fun Photo.toRecord() = PhotoRecord(
|
||||||
|
id = id,
|
||||||
|
itemId = itemId,
|
||||||
|
huntId = huntId,
|
||||||
|
hunterId = hunterId,
|
||||||
|
foundDateTime = foundDateTime,
|
||||||
|
status = status,
|
||||||
|
statusChangeDateTime = statusChangeDateTime
|
||||||
|
)
|
||||||
|
|
||||||
|
fun PhotoRecord.toDomain() = Photo(
|
||||||
|
id = id,
|
||||||
|
itemId = itemId,
|
||||||
|
huntId = huntId,
|
||||||
|
hunterId = hunterId,
|
||||||
|
foundDateTime = foundDateTime,
|
||||||
|
status = status,
|
||||||
|
statusChangeDateTime = statusChangeDateTime
|
||||||
|
)
|
||||||
|
|
||||||
|
fun Photo.toResponse(hunter: Hunter) = PhotoResponse(
|
||||||
|
id = id,
|
||||||
|
hunterName = hunter.name,
|
||||||
|
photoUploadDateTime = foundDateTime,
|
||||||
|
photoStatus = status,
|
||||||
|
photoStatusChangeDateTime = statusChangeDateTime
|
||||||
|
)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.RefreshResponse
|
||||||
|
|
||||||
|
fun String.toRefreshResponse(): RefreshResponse {
|
||||||
|
return RefreshResponse(this)
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.TeamHunt
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.TeamHuntRecord
|
||||||
|
|
||||||
|
fun TeamHunt.toRecord(): TeamHuntRecord {
|
||||||
|
return TeamHuntRecord(id, teamId, huntId)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun TeamHuntRecord.toDomain(): TeamHunt {
|
||||||
|
return TeamHunt(id, teamId, huntId)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
data class HuntItem(
|
||||||
|
val id: UUID = UUID.randomUUID(),
|
||||||
|
val huntId: HuntId,
|
||||||
|
val itemId: ItemId
|
||||||
|
)
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
data class Item(
|
||||||
|
val id: ItemId = UUID.randomUUID(),
|
||||||
|
val name: String,
|
||||||
|
val points: Int
|
||||||
|
)
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.UUID
|
||||||
|
|
||||||
|
data class Photo(
|
||||||
|
val id: PhotoId = UUID.randomUUID(),
|
||||||
|
val itemId: ItemId,
|
||||||
|
val huntId: HuntId,
|
||||||
|
val hunterId: HunterId,
|
||||||
|
val foundDateTime: LocalDateTime,
|
||||||
|
val status: PhotoStatus,
|
||||||
|
val statusChangeDateTime: LocalDateTime
|
||||||
|
)
|
||||||
@@ -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(),
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamHuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
|
|
||||||
|
data class TeamHunt(
|
||||||
|
val id: TeamHuntId = TeamHuntId.randomUUID(),
|
||||||
|
val teamId: TeamId,
|
||||||
|
val huntId: HuntId
|
||||||
|
)
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.model.record
|
|
||||||
|
|
||||||
import jakarta.persistence.Entity
|
|
||||||
import jakarta.persistence.Id
|
|
||||||
import jakarta.persistence.Table
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.*
|
|
||||||
import java.time.LocalDateTime
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a found Item for a Hunt by a Hunter
|
|
||||||
*/
|
|
||||||
@Entity
|
|
||||||
@Table(name = "found")
|
|
||||||
data class FoundRecord(
|
|
||||||
@Id
|
|
||||||
val id: FoundId,
|
|
||||||
val itemId: ItemId,
|
|
||||||
val huntId: HuntId,
|
|
||||||
val hunterId: HunterId,
|
|
||||||
val foundDateTime: LocalDateTime,
|
|
||||||
val imageName: String,
|
|
||||||
val status: FoundStatus
|
|
||||||
)
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.record
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity
|
||||||
|
import jakarta.persistence.Id
|
||||||
|
import jakarta.persistence.Table
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a found Item for a Hunt by a Hunter
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
@Table(name = "photo")
|
||||||
|
data class PhotoRecord(
|
||||||
|
@Id
|
||||||
|
val id: PhotoId,
|
||||||
|
val itemId: ItemId,
|
||||||
|
val huntId: HuntId,
|
||||||
|
val hunterId: HunterId,
|
||||||
|
val foundDateTime: LocalDateTime,
|
||||||
|
val status: PhotoStatus,
|
||||||
|
val statusChangeDateTime: LocalDateTime,
|
||||||
|
)
|
||||||
@@ -4,14 +4,14 @@ 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.HuntId
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
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")
|
||||||
data class TeamHuntRecord(
|
data class TeamHuntRecord(
|
||||||
@Id
|
@Id
|
||||||
val id: UUID,
|
val id: TeamHuntId,
|
||||||
val teamId: TeamId,
|
val teamId: TeamId,
|
||||||
val huntId: HuntId
|
val huntId: HuntId
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
data class ItemRequest(
|
||||||
|
val name: String,
|
||||||
|
val points: Int
|
||||||
|
)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
|
|
||||||
|
data class JoinTeamRequest(
|
||||||
|
@field:NotBlank
|
||||||
|
val teamId: TeamId
|
||||||
|
)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
|
||||||
|
data class ItemResponse(
|
||||||
|
val id: ItemId,
|
||||||
|
val name: String,
|
||||||
|
val points: Int
|
||||||
|
)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
data class PhotoResponse(
|
||||||
|
val id: PhotoId,
|
||||||
|
val hunterName: String,
|
||||||
|
val photoUploadDateTime: LocalDateTime,
|
||||||
|
val photoStatus: PhotoStatus,
|
||||||
|
val photoStatusChangeDateTime: LocalDateTime,
|
||||||
|
)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
data class RefreshResponse(
|
||||||
|
val accessToken: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.FoundStatus
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
data class TeamItemResponse(
|
||||||
|
val id: ItemId,
|
||||||
|
val hunterName: String?,
|
||||||
|
val itemFoundStatus: FoundStatus,
|
||||||
|
val itemStatusChangeDateTime: LocalDateTime,
|
||||||
|
)
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.repository
|
|
||||||
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.FoundId
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.record.FoundRecord
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository
|
|
||||||
import org.springframework.stereotype.Repository
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
interface FoundRepository : JpaRepository<FoundRecord, FoundId>
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
|
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>
|
||||||
@@ -23,6 +23,17 @@ interface HuntRepository : JpaRepository<HuntRecord, HuntId> {
|
|||||||
""", nativeQuery = true)
|
""", nativeQuery = true)
|
||||||
fun findAllOngoingByHunter(hunterId: HunterId): List<HuntRecord>
|
fun findAllOngoingByHunter(hunterId: HunterId): List<HuntRecord>
|
||||||
|
|
||||||
|
@Query("""
|
||||||
|
SELECT h.*
|
||||||
|
FROM hunter u
|
||||||
|
INNER JOIN hunter_team ht ON u.id = ht.hunter_id
|
||||||
|
INNER JOIN team t ON ht.team_id = t.id
|
||||||
|
INNER JOIN team_hunt th ON t.id = th.team_id
|
||||||
|
INNER JOIN hunt h ON th.hunt_id = h.id
|
||||||
|
WHERE u.email = :email
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun findHuntsByEmail(email: String): List<HuntRecord>
|
||||||
|
|
||||||
@Query("""
|
@Query("""
|
||||||
SELECT h.*
|
SELECT h.*
|
||||||
FROM hunt h
|
FROM hunt h
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.HunterTeamRecord
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.stereotype.Repository
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
interface HunterTeamRepository : JpaRepository<HunterTeamRecord, UUID> {
|
||||||
|
fun findByHunterId(hunterId: HunterId): List<HunterTeamRecord>
|
||||||
|
}
|
||||||
@@ -1,9 +1,19 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.repository
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
import net.halfbinary.scavengerhuntapi.model.ItemId
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
import net.halfbinary.scavengerhuntapi.model.record.ItemRecord
|
import net.halfbinary.scavengerhuntapi.model.record.ItemRecord
|
||||||
import org.springframework.data.jpa.repository.JpaRepository
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.data.jpa.repository.Query
|
||||||
import org.springframework.stereotype.Repository
|
import org.springframework.stereotype.Repository
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
interface ItemRepository : JpaRepository<ItemRecord, ItemId>
|
interface ItemRepository : JpaRepository<ItemRecord, ItemId> {
|
||||||
|
@Query("""
|
||||||
|
SELECT i.*
|
||||||
|
FROM item i
|
||||||
|
INNER JOIN hunt_item hi ON i.id = hi.item_id
|
||||||
|
WHERE hi.hunt_id = :huntId
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun findAllByHuntId(huntId: HuntId): List<ItemRecord>
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.PhotoRecord
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.stereotype.Repository
|
||||||
|
@Repository
|
||||||
|
interface PhotoRepository : JpaRepository<PhotoRecord, PhotoId> {
|
||||||
|
fun findByItemId(itemId: ItemId): List<PhotoRecord>
|
||||||
|
fun findByIdAndItemIdAndHuntId(id: PhotoId, itemId: ItemId, huntId: HuntId): PhotoRecord?
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.HuntRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.TeamHuntRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.TeamRecord
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.data.jpa.repository.Query
|
||||||
|
import org.springframework.stereotype.Repository
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
interface TeamHuntRepository : JpaRepository<TeamHuntRecord, UUID> {
|
||||||
|
@Query("""
|
||||||
|
SELECT h.* FROM hunt h
|
||||||
|
INNER JOIN team_hunt th ON h.id = th.hunt_id
|
||||||
|
WHERE th.team_id = :teamId
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun findHuntsByTeamId(teamId: TeamId): List<HuntRecord>
|
||||||
|
|
||||||
|
@Query("""
|
||||||
|
SELECT t.* FROM team t
|
||||||
|
INNER JOIN team_hunt th ON t.id = th.team_id
|
||||||
|
WHERE th.hunt_id = :huntId
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun findTeamsByHuntId(huntId: HuntId): List<TeamRecord>
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import org.apache.tika.Tika
|
||||||
|
import org.apache.tika.config.TikaConfig
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class FileProbeService {
|
||||||
|
private val tika = Tika()
|
||||||
|
|
||||||
|
fun getFileType(fileBytes: ByteArray): String {
|
||||||
|
return tika.detect(fileBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getFileExtension(fileType: String): String {
|
||||||
|
return TikaConfig.getDefaultConfig().mimeRepository.forName(fileType).extension
|
||||||
|
}
|
||||||
|
fun isImageType(fileType: String): Boolean {
|
||||||
|
return fileType.startsWith("image")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,15 +6,24 @@ import net.halfbinary.scavengerhuntapi.model.HunterId
|
|||||||
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.domain.Hunt
|
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.HuntStatus
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.HuntItemRepository
|
||||||
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.ItemRepository
|
||||||
import org.springframework.data.repository.findByIdOrNull
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class HuntService(private val huntRepository: HuntRepository) {
|
class HuntService(
|
||||||
|
private val huntRepository: HuntRepository,
|
||||||
|
private val itemRepository: ItemRepository,
|
||||||
|
private val huntItemRepository: HuntItemRepository
|
||||||
|
) {
|
||||||
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> {
|
||||||
@@ -30,7 +39,40 @@ class HuntService(private val huntRepository: HuntRepository) {
|
|||||||
return huntRepository.findAllOngoingByHunter(hunterId).map { it.toDomain() }
|
return huntRepository.findAllOngoingByHunter(hunterId).map { it.toDomain() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getHuntsByEmail(email: String, status: HuntStatus?): List<Hunt> {
|
||||||
|
val allHunts = huntRepository.findHuntsByEmail(email)
|
||||||
|
val filteredHunts = when (status) {
|
||||||
|
HuntStatus.ONGOING -> {
|
||||||
|
allHunts
|
||||||
|
.filter { !it.isTerminated && it.startDateTime < LocalDateTime.now() && it.endDateTime > LocalDateTime.now() }
|
||||||
|
.toList()
|
||||||
|
}
|
||||||
|
HuntStatus.CLOSED -> {
|
||||||
|
allHunts
|
||||||
|
.filter { it.isTerminated || it.endDateTime < LocalDateTime.now() }
|
||||||
|
}
|
||||||
|
HuntStatus.UNSTARTED -> {
|
||||||
|
allHunts
|
||||||
|
.filter { !it.isTerminated && it.startDateTime > LocalDateTime.now() }
|
||||||
|
}
|
||||||
|
else -> { allHunts }
|
||||||
|
}
|
||||||
|
return filteredHunts.map { it.toDomain() }
|
||||||
|
}
|
||||||
|
|
||||||
fun createHunt(hunt: Hunt): Hunt {
|
fun createHunt(hunt: Hunt): Hunt {
|
||||||
return huntRepository.save(hunt.toRecord()).toDomain()
|
return huntRepository.save(hunt.toRecord()).toDomain()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getItemsForHunt(huntId: HuntId): List<Item> {
|
||||||
|
huntRepository.findByIdOrNull(huntId) ?: throw NotFoundException("No hunt with id $huntId found")
|
||||||
|
return itemRepository.findAllByHuntId(huntId).map { it.toDomain() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addItemToHunt(huntId: HuntId, item: Item): Item {
|
||||||
|
huntRepository.findByIdOrNull(huntId) ?: throw NotFoundException("No hunt with id $huntId found")
|
||||||
|
val savedItem = itemRepository.save(item.toRecord()).toDomain()
|
||||||
|
huntItemRepository.save(HuntItem(huntId = huntId, itemId = savedItem.id).toRecord())
|
||||||
|
return savedItem
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class HunterService(private val hunterRepository: HunterRepository) {
|
||||||
|
fun getHunterByEmail(email: String): Hunter {
|
||||||
|
return hunterRepository.findByEmail(email)?.toDomain()
|
||||||
|
?: throw NotFoundException("No hunter with email $email found")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getHunterById(hunterId: HunterId): Hunter {
|
||||||
|
return hunterRepository.findById(hunterId).orElseThrow { NotFoundException("No hunter with id $hunterId found") }.toDomain()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.coobird.thumbnailator.Thumbnails
|
||||||
|
import net.coobird.thumbnailator.tasks.UnsupportedFormatException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.BadFileException
|
||||||
|
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.ItemId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
||||||
|
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.converter.toResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Photo
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.PhotoRepository
|
||||||
|
import org.springframework.http.MediaType
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import org.springframework.web.multipart.MultipartFile
|
||||||
|
import java.io.ByteArrayInputStream
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class PhotoService(
|
||||||
|
private val photoRepository: PhotoRepository,
|
||||||
|
private val hunterService: HunterService,
|
||||||
|
private val teamService: TeamService,
|
||||||
|
private val s3StorageService: S3StorageService,
|
||||||
|
private val fileProbeService: FileProbeService
|
||||||
|
) {
|
||||||
|
fun submitPhoto(huntId: HuntId, itemId: ItemId, email: String, file: MultipartFile) {
|
||||||
|
val originalBytes = file.bytes
|
||||||
|
val fileType = fileProbeService.getFileType(originalBytes)
|
||||||
|
|
||||||
|
if(!fileProbeService.isImageType(fileType)) throw BadFileException("Not an image")
|
||||||
|
|
||||||
|
val originalAsJpeg = try {
|
||||||
|
toJpeg(file.bytes)
|
||||||
|
} catch (_: UnsupportedFormatException) {
|
||||||
|
throw BadFileException("Image type is not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
val hunter = hunterService.getHunterByEmail(email)
|
||||||
|
val now = LocalDateTime.now()
|
||||||
|
val photo = Photo(
|
||||||
|
itemId = itemId,
|
||||||
|
huntId = huntId,
|
||||||
|
hunterId = hunter.id,
|
||||||
|
foundDateTime = now,
|
||||||
|
status = PhotoStatus.SUBMITTED,
|
||||||
|
statusChangeDateTime = now
|
||||||
|
)
|
||||||
|
val savedRecord = photoRepository.save(photo.toRecord())
|
||||||
|
val baseName = savedRecord.id.toString()
|
||||||
|
|
||||||
|
s3StorageService.upload("$baseName${fileProbeService.getFileExtension(fileType)}", originalBytes, fileType)
|
||||||
|
s3StorageService.upload("${baseName}_large.jpg", originalAsJpeg, MediaType.IMAGE_JPEG_VALUE)
|
||||||
|
s3StorageService.upload("${baseName}_medium.jpg", resize(originalBytes, 800), MediaType.IMAGE_JPEG_VALUE)
|
||||||
|
s3StorageService.upload("${baseName}_small.jpg", resize(originalBytes, 200), MediaType.IMAGE_JPEG_VALUE)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getPhotoInfo(huntId: HuntId, teamId: TeamId, itemId: ItemId, photoId: PhotoId, email: String): PhotoResponse {
|
||||||
|
val requestingHunter = hunterService.getHunterByEmail(email)
|
||||||
|
val photoRecord = photoRepository.findByIdAndItemIdAndHuntId(photoId, itemId, huntId)
|
||||||
|
?: throw NotFoundException("Photo not found")
|
||||||
|
|
||||||
|
if (!requestingHunter.isAdmin) {
|
||||||
|
val team = teamService.getTeamForHunterInHunt(huntId, email)
|
||||||
|
if (team.id != teamId) throw ForbiddenException("Access denied")
|
||||||
|
}
|
||||||
|
|
||||||
|
val submitter = hunterService.getHunterById(photoRecord.hunterId)
|
||||||
|
return photoRecord.toDomain().toResponse(submitter)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun toJpeg(bytes: ByteArray): ByteArray {
|
||||||
|
val output = ByteArrayOutputStream()
|
||||||
|
Thumbnails.of(ByteArrayInputStream(bytes))
|
||||||
|
.scale(1.0)
|
||||||
|
.outputFormat("jpg")
|
||||||
|
.toOutputStream(output)
|
||||||
|
return output.toByteArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun resize(bytes: ByteArray, width: Int): ByteArray {
|
||||||
|
val output = ByteArrayOutputStream()
|
||||||
|
Thumbnails.of(ByteArrayInputStream(bytes))
|
||||||
|
.width(width)
|
||||||
|
.outputFormat("jpg")
|
||||||
|
.toOutputStream(output)
|
||||||
|
return output.toByteArray()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import software.amazon.awssdk.core.sync.RequestBody
|
||||||
|
import software.amazon.awssdk.services.s3.S3Client
|
||||||
|
import software.amazon.awssdk.services.s3.model.PutObjectRequest
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class S3StorageService(
|
||||||
|
private val s3Client: S3Client,
|
||||||
|
@Value("\${minio.bucket}") private val bucket: String
|
||||||
|
) {
|
||||||
|
fun upload(key: String, bytes: ByteArray, contentType: String) {
|
||||||
|
s3Client.putObject(
|
||||||
|
PutObjectRequest.builder()
|
||||||
|
.bucket(bucket)
|
||||||
|
.key(key)
|
||||||
|
.contentType(contentType)
|
||||||
|
.contentLength(bytes.size.toLong())
|
||||||
|
.build(),
|
||||||
|
RequestBody.fromBytes(bytes)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,60 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.service
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
import net.halfbinary.scavengerhuntapi.model.HuntId
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
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.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.record.HunterTeamRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.TeamRequest
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.HunterTeamRepository
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.TeamHuntRepository
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.TeamRepository
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class TeamService {
|
class TeamService(
|
||||||
|
private val teamRepository: TeamRepository,
|
||||||
|
private val teamHuntRepository: TeamHuntRepository,
|
||||||
|
private val hunterRepository: HunterRepository,
|
||||||
|
private val hunterTeamRepository: HunterTeamRepository,
|
||||||
|
) {
|
||||||
fun getListOfTeamsForHunt(huntId: HuntId): List<Team> {
|
fun getListOfTeamsForHunt(huntId: HuntId): List<Team> {
|
||||||
TODO()
|
return getTeamsForHunt(huntId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createTeam(name: String): Team {
|
fun createTeam(name: String): Team {
|
||||||
TODO()
|
return teamRepository.save(TeamRequest(name).toDomain().toRecord()).toDomain()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addTeamToHunt(huntId: HuntId, teamId: TeamId) {
|
fun addTeamToHunt(huntId: HuntId, teamId: TeamId) {
|
||||||
TODO()
|
teamHuntRepository.save(TeamHunt(teamId = teamId, huntId = huntId).toRecord()).toDomain()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTeamFromHunt(huntId: HuntId, teamId: TeamId): Team {
|
||||||
|
return getTeamsForHunt(huntId)
|
||||||
|
.filter { it.id == teamId }
|
||||||
|
.elementAt(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTeamForHunterInHunt(huntId: HuntId, email: String): Team {
|
||||||
|
val hunter = hunterRepository.findByEmail(email) ?: throw NotFoundException("No hunter with email $email found")
|
||||||
|
val hunterTeamIds = hunterTeamRepository.findByHunterId(hunter.id).map { it.teamId }.toSet()
|
||||||
|
return getTeamsForHunt(huntId)
|
||||||
|
.firstOrNull { it.id in hunterTeamIds }
|
||||||
|
?: throw NotFoundException("No team found for hunter $email in hunt $huntId")
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getTeamsForHunt(huntId: HuntId): List<Team> {
|
||||||
|
return teamHuntRepository.findTeamsByHuntId(huntId).map { it.toDomain() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,10 +4,23 @@ spring.jpa.hibernate.ddl-auto=update
|
|||||||
|
|
||||||
spring.jpa.properties.hibernate.type.preferred_uuid_jdbc_type=CHAR
|
spring.jpa.properties.hibernate.type.preferred_uuid_jdbc_type=CHAR
|
||||||
|
|
||||||
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
|
spring.datasource.driverClassName=org.mariadb.jdbc.Driver
|
||||||
spring.datasource.url=${DB_URL}
|
spring.datasource.url=${DB_URL}
|
||||||
spring.datasource.username=${DB_USER}
|
spring.datasource.username=${DB_USER}
|
||||||
spring.datasource.password=${DB_PASSWORD}
|
spring.datasource.password=${DB_PASSWORD}
|
||||||
|
|
||||||
jwt.secret=${JWT_SECRET}
|
jwt.secret=${JWT_SECRET}
|
||||||
jwt.expiration=30000
|
jwt.expiration=300000
|
||||||
|
|
||||||
|
minio.endpoint=${MINIO_ENDPOINT}
|
||||||
|
minio.access-key=${MINIO_ACCESS_KEY}
|
||||||
|
minio.secret-key=${MINIO_SECRET_KEY}
|
||||||
|
minio.bucket=${MINIO_BUCKET}
|
||||||
|
|
||||||
|
spring.servlet.multipart.max-file-size=25MB
|
||||||
|
spring.servlet.multipart.max-request-size=25MB
|
||||||
|
|
||||||
|
springdoc.api-docs.enabled=true
|
||||||
|
springdoc.api-docs.path=/docs/api-docs
|
||||||
|
springdoc.swagger-ui.enabled=true
|
||||||
|
springdoc.swagger-ui.path=/docs/swagger-ui.html
|
||||||
Reference in New Issue
Block a user