Compare commits
36 Commits
8b808bfd34
...
feature/do
| Author | SHA1 | Date | |
|---|---|---|---|
| 6288cc6249 | |||
| b4f72a318d | |||
| eed5a0dd56 | |||
| 6327e0d034 | |||
| ac6f3a7014 | |||
| dbd988a573 | |||
| 67fb801812 | |||
| bc1bcf6e8d | |||
| aff0872e38 | |||
| 63e015400b | |||
| b349380c93 | |||
| 1585b6eb7d | |||
| 1dd904055c | |||
| 5ca7a685dd | |||
| 863c824421 | |||
| 9324cf2eb0 | |||
| 30c66527b9 | |||
| 46132bb4fd | |||
| b2ba9ce676 | |||
| 4a1077833e | |||
| 1ed64cadd9 | |||
| aff7cd1e28 | |||
| fd754a7ee7 | |||
| 46a78bfc08 | |||
| ab34f16a45 | |||
| 2e0244e1ee | |||
| 0c01c5dbcc | |||
| 69e874c9f2 | |||
| 9633d95e75 | |||
| 3a53769421 | |||
| 7dce3e38b4 | |||
| db001dc5a7 | |||
| 457021aeec | |||
| 5905882763 | |||
| 04b61485ea | |||
| 302feeab1e |
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
.git/
|
||||||
|
.gradle/
|
||||||
|
build/
|
||||||
|
Claude Notes/
|
||||||
|
*.md
|
||||||
|
.gitignore
|
||||||
15
.woodpecker.yaml
Normal file
15
.woodpecker.yaml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
when:
|
||||||
|
branch: main
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
- manual
|
||||||
|
steps:
|
||||||
|
- name: build
|
||||||
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
|
settings:
|
||||||
|
repo: git.halfbinary.net/${CI_REPO_OWNER}/scavengerhunt-api
|
||||||
|
registry: git.halfbinary.net
|
||||||
|
tags: ${CI_PIPELINE_NUMBER}
|
||||||
|
username: ${CI_REPO_OWNER}
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
14
Dockerfile
Normal file
14
Dockerfile
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
FROM amazoncorretto:21-alpine-jdk AS build
|
||||||
|
WORKDIR /app
|
||||||
|
COPY gradlew .
|
||||||
|
COPY gradle/ gradle/
|
||||||
|
COPY build.gradle.kts settings.gradle.kts ./
|
||||||
|
RUN ./gradlew dependencies --no-daemon
|
||||||
|
COPY src/ src/
|
||||||
|
RUN ./gradlew bootJar --no-daemon
|
||||||
|
|
||||||
|
FROM amazoncorretto:21-alpine-jdk
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=build /app/build/libs/*.jar app.jar
|
||||||
|
EXPOSE 8080
|
||||||
|
ENTRYPOINT ["java", "-jar", "app.jar"]
|
||||||
39
README.md
39
README.md
@@ -1,3 +1,42 @@
|
|||||||
# Scavenger Hunt API
|
# Scavenger Hunt API
|
||||||
|
|
||||||
REST API to support a community scavenger hunt app.
|
REST API to support a community scavenger hunt app.
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- Java 21
|
||||||
|
- MariaDB
|
||||||
|
- MinIO
|
||||||
|
|
||||||
|
## Environment Variables
|
||||||
|
|
||||||
|
| Variable | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| `DB_URL` | JDBC URL for the MariaDB database |
|
||||||
|
| `DB_USER` | Database username |
|
||||||
|
| `DB_PASSWORD` | Database password |
|
||||||
|
| `JWT_SECRET` | Secret key used to sign JWTs |
|
||||||
|
| `MINIO_ENDPOINT` | MinIO server URL (e.g. `http://localhost:9000`) |
|
||||||
|
| `MINIO_ACCESS_KEY` | MinIO access key |
|
||||||
|
| `MINIO_SECRET_KEY` | MinIO secret key |
|
||||||
|
| `MINIO_BUCKET` | MinIO bucket name for photo storage |
|
||||||
|
|
||||||
|
## Running
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew bootRun
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Documentation
|
||||||
|
|
||||||
|
Swagger UI is available at `/docs/swagger-ui.html` when the application is running.
|
||||||
|
|
||||||
|
## Authentication
|
||||||
|
|
||||||
|
All endpoints except `/signup` and `/login` require a JWT bearer token.
|
||||||
|
|
||||||
|
1. Create an account: `POST /signup`
|
||||||
|
2. Log in: `POST /login` — returns an access token and a refresh token
|
||||||
|
3. Include the access token in requests: `Authorization: Bearer <token>`
|
||||||
|
4. Refresh an expired token: `POST /refresh`
|
||||||
|
5. Log out: `POST /logout`
|
||||||
|
|||||||
@@ -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,13 +27,32 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
val mysqlConnectorJ = "9.5.0"
|
val mariaDriver = "3.5.8"
|
||||||
|
val commonsValidator = "1.10.1"
|
||||||
|
val jakartaValidation = "3.1.1"
|
||||||
|
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("com.mysql:mysql-connector-j:${mysqlConnectorJ}")
|
implementation("org.springframework.boot:spring-boot-starter-validation")
|
||||||
|
implementation("org.springframework.boot:spring-boot-starter-security")
|
||||||
|
implementation("jakarta.validation:jakarta.validation-api:$jakartaValidation")
|
||||||
|
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("io.jsonwebtoken:jjwt-api:$jsonWebToken")
|
||||||
|
implementation("io.jsonwebtoken:jjwt-impl:$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")
|
||||||
|
|||||||
78
docker-compose.yml
Normal file
78
docker-compose.yml
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
# All services use host networking so inter-service traffic goes over loopback with no bridge overhead.
|
||||||
|
# Ports (all bound directly on the host):
|
||||||
|
# API: 8080
|
||||||
|
# MariaDB: 3306
|
||||||
|
# Adminer: 8888
|
||||||
|
# MinIO API: 9000
|
||||||
|
# MinIO Console: 9001
|
||||||
|
|
||||||
|
services:
|
||||||
|
mariadb:
|
||||||
|
image: mariadb:11
|
||||||
|
network_mode: host
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
|
||||||
|
MYSQL_DATABASE: ${DB_NAME}
|
||||||
|
MYSQL_USER: ${DB_USER}
|
||||||
|
MYSQL_PASSWORD: ${DB_PASSWORD}
|
||||||
|
volumes:
|
||||||
|
- mariadb_data:/var/lib/mysql
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
|
||||||
|
start_period: 10s
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
adminer:
|
||||||
|
image: adminer
|
||||||
|
network_mode: host
|
||||||
|
command: php -S [::]:8888 -t /var/www/html
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
minio:
|
||||||
|
image: minio/minio
|
||||||
|
network_mode: host
|
||||||
|
command: server /data --console-address :9001
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER: ${MINIO_ACCESS_KEY}
|
||||||
|
MINIO_ROOT_PASSWORD: ${MINIO_SECRET_KEY}
|
||||||
|
volumes:
|
||||||
|
- minio_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
|
||||||
|
start_period: 10s
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
api:
|
||||||
|
build: .
|
||||||
|
network_mode: host
|
||||||
|
environment:
|
||||||
|
DB_URL: jdbc:mariadb://localhost:3306/${DB_NAME}
|
||||||
|
DB_USER: ${DB_USER}
|
||||||
|
DB_PASSWORD: ${DB_PASSWORD}
|
||||||
|
JWT_SECRET: ${JWT_SECRET}
|
||||||
|
MINIO_ENDPOINT: http://localhost:9000
|
||||||
|
MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY}
|
||||||
|
MINIO_SECRET_KEY: ${MINIO_SECRET_KEY}
|
||||||
|
MINIO_BUCKET: ${MINIO_BUCKET}
|
||||||
|
depends_on:
|
||||||
|
mariadb:
|
||||||
|
condition: service_healthy
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
|
||||||
|
start_period: 30s
|
||||||
|
interval: 15s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mariadb_data:
|
||||||
|
minio_data:
|
||||||
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/.
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.config
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest
|
||||||
|
import jakarta.servlet.http.HttpServletResponse
|
||||||
|
import org.springframework.security.core.AuthenticationException
|
||||||
|
import org.springframework.security.web.AuthenticationEntryPoint
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class AuthEntrypointJwt: AuthenticationEntryPoint {
|
||||||
|
override fun commence(
|
||||||
|
request: HttpServletRequest,
|
||||||
|
response: HttpServletResponse,
|
||||||
|
authException: AuthenticationException
|
||||||
|
) {
|
||||||
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.config
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain
|
||||||
|
import jakarta.servlet.http.HttpServletRequest
|
||||||
|
import jakarta.servlet.http.HttpServletResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.service.HunterDetailsService
|
||||||
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails
|
||||||
|
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter
|
||||||
|
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class AuthTokenFilter(private val jwtUtils: JwtUtil, private val hunterDetailsService: HunterDetailsService): OncePerRequestFilter() {
|
||||||
|
override fun doFilterInternal(
|
||||||
|
request: HttpServletRequest,
|
||||||
|
response: HttpServletResponse,
|
||||||
|
filterChain: FilterChain
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
val jwt: String? = parseJwt(request)
|
||||||
|
if (jwt != null && jwtUtils.validateJwtToken(jwt)) {
|
||||||
|
val username = jwtUtils.getUsernameFromToken(jwt)
|
||||||
|
val userDetails: UserDetails = hunterDetailsService.loadUserByUsername(username)
|
||||||
|
val authentication =
|
||||||
|
UsernamePasswordAuthenticationToken(
|
||||||
|
userDetails,
|
||||||
|
null,
|
||||||
|
userDetails.authorities
|
||||||
|
)
|
||||||
|
authentication.details = WebAuthenticationDetailsSource().buildDetails(request)
|
||||||
|
val context = SecurityContextHolder.createEmptyContext()
|
||||||
|
context.authentication = authentication
|
||||||
|
SecurityContextHolder.setContext(context)
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Cannot set user authentication: $e")
|
||||||
|
}
|
||||||
|
filterChain.doFilter(request, response)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseJwt(request: HttpServletRequest): String? {
|
||||||
|
val headerAuth = request.getHeader("Authorization")
|
||||||
|
if (headerAuth != null && headerAuth.startsWith("Bearer ")) {
|
||||||
|
return headerAuth.substring(7)
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.config
|
||||||
|
|
||||||
|
import io.jsonwebtoken.JwtException
|
||||||
|
import io.jsonwebtoken.Jwts
|
||||||
|
import jakarta.annotation.PostConstruct
|
||||||
|
import org.springframework.beans.factory.annotation.Value
|
||||||
|
import org.springframework.stereotype.Component
|
||||||
|
import java.util.*
|
||||||
|
import javax.crypto.SecretKey
|
||||||
|
import javax.crypto.spec.SecretKeySpec
|
||||||
|
|
||||||
|
@Component
|
||||||
|
class JwtUtil {
|
||||||
|
@Value($$"${jwt.secret}")
|
||||||
|
private val jwtSecret: String = ""
|
||||||
|
|
||||||
|
@Value($$"${jwt.expiration}")
|
||||||
|
private val jwtExpirationMs = 0
|
||||||
|
|
||||||
|
private var key: SecretKey? = null
|
||||||
|
|
||||||
|
// Initializes the key after the class is instantiated and the jwtSecret is injected,
|
||||||
|
// preventing the repeated creation of the key and enhancing performance
|
||||||
|
@PostConstruct
|
||||||
|
fun init() {
|
||||||
|
this.key = SecretKeySpec(jwtSecret.toByteArray(Charsets.UTF_8), "HmacSHA256")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate JWT token
|
||||||
|
fun generateToken(email: String): String {
|
||||||
|
return Jwts.builder()
|
||||||
|
.subject(email)
|
||||||
|
.issuedAt(Date())
|
||||||
|
.expiration(Date(System.currentTimeMillis() + jwtExpirationMs))
|
||||||
|
.signWith(key)
|
||||||
|
.compact()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get username from JWT token
|
||||||
|
fun getUsernameFromToken(token: String): String {
|
||||||
|
return Jwts.parser()
|
||||||
|
.verifyWith(key)
|
||||||
|
.build()
|
||||||
|
.parseSignedClaims(token)
|
||||||
|
.payload
|
||||||
|
.subject
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate JWT token
|
||||||
|
fun validateJwtToken(token: String?): Boolean {
|
||||||
|
try {
|
||||||
|
Jwts.parser().verifyWith(key).build().parseSignedClaims(token)
|
||||||
|
return true
|
||||||
|
} catch (e: SecurityException) {
|
||||||
|
println("Invalid JWT signature: " + e.message)
|
||||||
|
} catch (e: JwtException) {
|
||||||
|
println("Invalid JWT token: " + e.message)
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
println("JWT claims string is empty: " + e.message)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.config
|
||||||
|
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager
|
||||||
|
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.configurers.CorsConfigurer
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.ExceptionHandlingConfigurer
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.SessionManagementConfigurer
|
||||||
|
import org.springframework.security.config.http.SessionCreationPolicy
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder
|
||||||
|
import org.springframework.security.web.SecurityFilterChain
|
||||||
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableMethodSecurity
|
||||||
|
class SecurityConfig(private val authEntrypointJwt: AuthEntrypointJwt,
|
||||||
|
private val authTokenFilter: AuthTokenFilter) {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun authenticationJwtTokenFilter(): AuthTokenFilter {
|
||||||
|
return authTokenFilter
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun authTokenFilterRegistration(): FilterRegistrationBean<AuthTokenFilter> {
|
||||||
|
val registration = FilterRegistrationBean(authTokenFilter)
|
||||||
|
registration.isEnabled = false
|
||||||
|
return registration
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun authenticationManager(
|
||||||
|
authenticationConfiguration: AuthenticationConfiguration
|
||||||
|
): AuthenticationManager? {
|
||||||
|
return authenticationConfiguration.getAuthenticationManager()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun passwordEncoder(): PasswordEncoder {
|
||||||
|
return BCryptPasswordEncoder()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Throws(Exception::class)
|
||||||
|
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain? {
|
||||||
|
// Updated configuration for Spring Security 6.x
|
||||||
|
http
|
||||||
|
.csrf { csrf: CsrfConfigurer<HttpSecurity> -> csrf.disable() } // Disable CSRF
|
||||||
|
.cors { cors: CorsConfigurer<HttpSecurity> -> cors.disable() } // Disable CORS (or configure if needed)
|
||||||
|
.exceptionHandling { exceptionHandling: ExceptionHandlingConfigurer<HttpSecurity> ->
|
||||||
|
exceptionHandling.authenticationEntryPoint(
|
||||||
|
authEntrypointJwt
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.sessionManagement { sessionManagement: SessionManagementConfigurer<HttpSecurity> ->
|
||||||
|
sessionManagement.sessionCreationPolicy(
|
||||||
|
SessionCreationPolicy.STATELESS
|
||||||
|
)
|
||||||
|
}
|
||||||
|
.authorizeHttpRequests { authorizeRequests ->
|
||||||
|
authorizeRequests
|
||||||
|
.requestMatchers("/auth/**", "/signup", "/docs/**")
|
||||||
|
.permitAll()
|
||||||
|
.anyRequest().authenticated()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the JWT Token filter before the UsernamePasswordAuthenticationFilter
|
||||||
|
http.addFilterBefore(
|
||||||
|
authenticationJwtTokenFilter(),
|
||||||
|
UsernamePasswordAuthenticationFilter::class.java
|
||||||
|
)
|
||||||
|
return http.build()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
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.PhotoId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.ReviewPhotoRequest
|
||||||
|
import net.halfbinary.scavengerhuntapi.service.PhotoService
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize
|
||||||
|
import org.springframework.web.bind.annotation.PatchMapping
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("admin")
|
||||||
|
class AdminController(private val photoService: PhotoService) {
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@Tag(name = "Admin")
|
||||||
|
@PatchMapping("/photo/{photoId}")
|
||||||
|
@Operation(summary = "Sets a review status for the specified photo")
|
||||||
|
fun reviewPhoto(@PathVariable photoId: PhotoId, @Valid @RequestBody request: ReviewPhotoRequest) {
|
||||||
|
photoService.updatePhotoStatus(photoId, request.status)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
|
import jakarta.validation.Valid
|
||||||
|
import net.halfbinary.scavengerhuntapi.config.JwtUtil
|
||||||
|
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.LogoutRequest
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.RefreshRequest
|
||||||
|
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.RefreshTokenService
|
||||||
|
import org.springframework.http.ResponseEntity
|
||||||
|
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("/auth")
|
||||||
|
class AuthController(private val loginService: LoginService, private val jwtUtils: JwtUtil, private val refreshTokenService: RefreshTokenService) {
|
||||||
|
@PostMapping("/login")
|
||||||
|
fun login(@Valid @RequestBody body: LoginRequest): ResponseEntity<LoginResponse> {
|
||||||
|
val result = loginService.login(body.toDomain())
|
||||||
|
val accessToken = jwtUtils.generateToken(result.email)
|
||||||
|
val refreshToken = refreshTokenService.generateRefreshToken(result.email)
|
||||||
|
val loginResponse = LoginResponse(accessToken, refreshToken)
|
||||||
|
return ResponseEntity.ok(loginResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/refresh")
|
||||||
|
fun refresh(@RequestBody body: RefreshRequest): ResponseEntity<RefreshResponse> {
|
||||||
|
return ResponseEntity.ok(refreshTokenService.getAccessToken(body.refreshToken).toRefreshResponse())
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/logout")
|
||||||
|
fun logout(@RequestBody body: LogoutRequest): ResponseEntity<String> {
|
||||||
|
refreshTokenService.removeToken(body.refreshToken)
|
||||||
|
return ResponseEntity.ok().build()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
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.HunterId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.HuntCreateRequest
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.HuntStatus
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.HuntResponse
|
||||||
|
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.RequestParam
|
||||||
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("hunt")
|
||||||
|
class HuntController(private val huntService: HuntService) {
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
@Operation(summary = "Gets the specified hunt information")
|
||||||
|
fun getHunt(@PathVariable("id") huntId: HuntId): ResponseEntity<HuntResponse> {
|
||||||
|
return ResponseEntity.ok(huntService.getHunt(huntId).toResponse())
|
||||||
|
}
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@Tag(name = "Admin")
|
||||||
|
@GetMapping
|
||||||
|
@Operation(summary = "Gets all Hunts")
|
||||||
|
fun getAllHunts(@RequestParam status: HuntStatus?): ResponseEntity<List<HuntResponse>> {
|
||||||
|
return ResponseEntity.ok(huntService.getAllHunts(status).map { it.toResponse() })
|
||||||
|
}
|
||||||
|
|
||||||
|
@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> {
|
||||||
|
return ResponseEntity.ok(huntService.createHunt(huntRequest.toDomain()).toResponse())
|
||||||
|
}
|
||||||
|
|
||||||
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
|
@Tag(name = "Admin")
|
||||||
|
@GetMapping("/hunter/{hunterId}")
|
||||||
|
@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() })
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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("Maybe not needed: 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())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ImageVersion
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoId
|
||||||
|
import net.halfbinary.scavengerhuntapi.service.PhotoService
|
||||||
|
import org.springframework.core.io.InputStreamSource
|
||||||
|
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.RequestMapping
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam
|
||||||
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("photo")
|
||||||
|
class PhotoController(private val photoService: PhotoService) {
|
||||||
|
@GetMapping("/{photoId}/file")
|
||||||
|
@Operation(summary = "Get the binary image information for the specified Photo")
|
||||||
|
fun getPhoto(authentication: Authentication,
|
||||||
|
@PathVariable photoId: PhotoId,
|
||||||
|
@RequestParam(defaultValue = "LARGE") version: ImageVersion): ResponseEntity<InputStreamSource> {
|
||||||
|
val photoFile = photoService.getPhotoFile(photoId, authentication.name, version)
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.contentType(photoFile.contentType)
|
||||||
|
.body(photoFile.resource)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.controller
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
|
import jakarta.validation.Valid
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||||
import net.halfbinary.scavengerhuntapi.model.request.HunterSignupRequest
|
import net.halfbinary.scavengerhuntapi.model.request.HunterSignupRequest
|
||||||
import net.halfbinary.scavengerhuntapi.service.SignupService
|
import net.halfbinary.scavengerhuntapi.service.SignupService
|
||||||
|
import org.springframework.http.ResponseEntity
|
||||||
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.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
@@ -10,7 +12,8 @@ import org.springframework.web.bind.annotation.RestController
|
|||||||
@RestController
|
@RestController
|
||||||
class SignupController(private val signupService: SignupService) {
|
class SignupController(private val signupService: SignupService) {
|
||||||
@PostMapping("/signup")
|
@PostMapping("/signup")
|
||||||
fun hunterSignup(@RequestBody body: HunterSignupRequest) {
|
fun hunterSignup(@Valid @RequestBody body: HunterSignupRequest): ResponseEntity<Any> {
|
||||||
signupService.createNewHunter(body.toDomain())
|
signupService.createNewHunter(body.toDomain())
|
||||||
|
return ResponseEntity.ok().build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.controller
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.converter.toResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.HunterLeaderboardResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.TeamLeaderboardResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.service.StatsService
|
||||||
|
import org.springframework.http.ResponseEntity
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping
|
||||||
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("stats/lead/hunt/{huntId}")
|
||||||
|
class StatsController(private val statsService: StatsService) {
|
||||||
|
|
||||||
|
@GetMapping("/team")
|
||||||
|
@Operation(summary = "Ranked teams with current total scores for a hunt")
|
||||||
|
fun getTeamLeaderboard(@PathVariable huntId: HuntId): ResponseEntity<List<TeamLeaderboardResponse>> {
|
||||||
|
return ResponseEntity.ok(statsService.getTeamLeaderboard(huntId).map { it.toResponse() })
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/hunter")
|
||||||
|
@Operation(summary = "Ranked hunters with current total scores for a hunt")
|
||||||
|
fun getHunterLeaderboard(@PathVariable huntId: HuntId): ResponseEntity<List<HunterLeaderboardResponse>> {
|
||||||
|
return ResponseEntity.ok(statsService.getHunterLeaderboard(huntId).map { it.toResponse() })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
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<List<TeamResponse>> {
|
||||||
|
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<TeamResponse> {
|
||||||
|
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<TeamItemResponse> {
|
||||||
|
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<List<PhotoResponse>> {
|
||||||
|
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<PhotoResponse> {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.BadFileException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.ConflictException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.ForbiddenException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.InvalidEmailException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.LoginFailedException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.PreexistingAccountException
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.http.HttpStatus
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException
|
||||||
|
import org.springframework.validation.FieldError
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice
|
||||||
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
|
||||||
|
import org.springframework.web.multipart.MaxUploadSizeExceededException
|
||||||
|
import java.net.SocketTimeoutException
|
||||||
|
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
class ExceptionHandler {
|
||||||
|
companion object {
|
||||||
|
private val log = LoggerFactory.getLogger(net.halfbinary.scavengerhuntapi.error.ExceptionHandler::class.java)
|
||||||
|
}
|
||||||
|
@ExceptionHandler(PreexistingAccountException::class)
|
||||||
|
@ResponseStatus(HttpStatus.CONFLICT)
|
||||||
|
fun preexistingAccountException(e: PreexistingAccountException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(LoginFailedException::class)
|
||||||
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
||||||
|
fun loginFailedException(e: LoginFailedException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(InvalidEmailException::class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
fun invalidEmailException(e: InvalidEmailException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(NotFoundException::class)
|
||||||
|
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||||
|
fun notFoundException(e: NotFoundException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(ForbiddenException::class)
|
||||||
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||||
|
fun forbiddenException(e: ForbiddenException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(ConflictException::class)
|
||||||
|
@ResponseStatus(HttpStatus.CONFLICT)
|
||||||
|
fun conflictException(e: ConflictException): String? {
|
||||||
|
return e.message
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(HttpMessageNotReadableException::class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
fun httpMessageNotReadableException(e: HttpMessageNotReadableException): Map<String, String?> {
|
||||||
|
if (e.message?.contains("body is missing")?:false) {
|
||||||
|
return simpleMap("body","Body is missing")
|
||||||
|
}
|
||||||
|
if (e.message?.contains("parameter")?:false) {
|
||||||
|
val missingParameter = e.message?.split("parameter ")[1]
|
||||||
|
return simpleMap(missingParameter?:"","Missing required parameter $missingParameter")
|
||||||
|
}
|
||||||
|
log.debug("JSON parsing issue", e)
|
||||||
|
return simpleMap("body", "Parsing error")
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException::class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
fun handleValidationExceptions(e: MethodArgumentNotValidException): Map<String, String?> {
|
||||||
|
return e.bindingResult.allErrors.associate { error ->
|
||||||
|
Pair(
|
||||||
|
(error as FieldError).field,
|
||||||
|
error.defaultMessage
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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."
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentTypeMismatchException::class)
|
||||||
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||||
|
fun argumentMismatchException(): String? {
|
||||||
|
return "Invalid parameter value."
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun simpleMap(key: String, value: String?): Map<String, String?> {
|
||||||
|
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 ConflictException(override val message: String) : RuntimeException(message)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
|
||||||
|
class ExpiredRefreshTokenException(token: RefreshId): RuntimeException("The refresh token $token is expired.")
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
class ForbiddenException: RuntimeException("Access Denied.")
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
class InvalidEmailException(email: String): RuntimeException("The email ${email} is not valid.")
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
|
||||||
|
class InvalidRefreshTokenException(token: RefreshId): RuntimeException("The refresh token $token is not valid.")
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
class LoginFailedException : RuntimeException("The email and password combination is not correct.")
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
class NotFoundException(override val message: String): RuntimeException(message)
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.error.exception
|
||||||
|
|
||||||
|
class PreexistingAccountException: RuntimeException("An account with that email already exists.")
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.model
|
package net.halfbinary.scavengerhuntapi.model
|
||||||
|
|
||||||
enum class FoundStatus {
|
enum class FoundStatus {
|
||||||
|
NOT_FOUND,
|
||||||
SUBMITTED,
|
SUBMITTED,
|
||||||
APPROVED,
|
APPROVED,
|
||||||
REJECTED,
|
REJECTED
|
||||||
REMOVED
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model
|
||||||
|
|
||||||
|
enum class ImageVersion {
|
||||||
|
ORIGINAL, LARGE, MEDIUM, SMALL
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model
|
||||||
|
|
||||||
|
enum class PhotoStatus {
|
||||||
|
SUBMITTED,
|
||||||
|
APPROVED,
|
||||||
|
REJECTED,
|
||||||
|
REMOVED
|
||||||
|
}
|
||||||
@@ -2,8 +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 TeamHuntId = UUID
|
||||||
|
typealias PhotoId = UUID
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Hunt
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.HuntRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.HuntCreateRequest
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.HuntResponse
|
||||||
|
|
||||||
|
fun HuntRecord.toDomain(): Hunt {
|
||||||
|
return Hunt(id, title, startDateTime, endDateTime, isTerminated)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Hunt.toResponse(): HuntResponse {
|
||||||
|
return HuntResponse(id, title, startDateTime, endDateTime, isTerminated)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun HuntCreateRequest.toDomain(): Hunt {
|
||||||
|
return Hunt(title = title, startDateTime = startDateTime, endDateTime = endDateTime, isTerminated = false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Hunt.toRecord(): HuntRecord {
|
||||||
|
return HuntRecord(id, title, startDateTime, endDateTime, isTerminated)
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
@@ -16,3 +16,7 @@ fun HunterSignupRequest.toDomain(): Hunter {
|
|||||||
fun Hunter.toRecord(): HunterRecord {
|
fun Hunter.toRecord(): HunterRecord {
|
||||||
return HunterRecord(id, email, name, password, isAdmin)
|
return HunterRecord(id, email, name, password, isAdmin)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun HunterRecord.toDomain(): Hunter {
|
||||||
|
return Hunter(id, email, name, password, isAdmin)
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.HunterLeaderboardEntry
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.HunterLeaderboardResponse
|
||||||
|
|
||||||
|
fun HunterLeaderboardEntry.toResponse() = HunterLeaderboardResponse(
|
||||||
|
rank = rank,
|
||||||
|
hunterName = hunterName,
|
||||||
|
score = score
|
||||||
|
)
|
||||||
@@ -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,8 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Login
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.LoginRequest
|
||||||
|
|
||||||
|
fun LoginRequest.toDomain(): Login {
|
||||||
|
return Login(email, password)
|
||||||
|
}
|
||||||
@@ -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,22 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Team
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.TeamRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.request.TeamRequest
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.TeamResponse
|
||||||
|
|
||||||
|
fun TeamRequest.toDomain(): Team {
|
||||||
|
return Team(name = name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Team.toRecord(): TeamRecord {
|
||||||
|
return TeamRecord(id, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun TeamRecord.toDomain(): Team {
|
||||||
|
return Team(id, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Team.toResponse(): TeamResponse {
|
||||||
|
return TeamResponse(id, name)
|
||||||
|
}
|
||||||
@@ -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,10 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.converter
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.TeamLeaderboardEntry
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.TeamLeaderboardResponse
|
||||||
|
|
||||||
|
fun TeamLeaderboardEntry.toResponse() = TeamLeaderboardResponse(
|
||||||
|
rank = rank,
|
||||||
|
teamName = teamName,
|
||||||
|
score = score
|
||||||
|
)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
data class Hunt(
|
||||||
|
val id: HuntId = UUID.randomUUID(),
|
||||||
|
val title: String,
|
||||||
|
val startDateTime: LocalDateTime,
|
||||||
|
val endDateTime: LocalDateTime,
|
||||||
|
val isTerminated: Boolean
|
||||||
|
)
|
||||||
@@ -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
|
||||||
|
)
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.model.domain
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
import java.util.UUID
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
data class Hunter(
|
data class Hunter(
|
||||||
val id: UUID = UUID.randomUUID(),
|
val id: HunterId = UUID.randomUUID(),
|
||||||
val email: String,
|
val email: String,
|
||||||
val name: String,
|
val name: String,
|
||||||
val password: String,
|
val password: String,
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
data class HunterLeaderboardEntry(
|
||||||
|
val rank: Int,
|
||||||
|
val hunterName: String,
|
||||||
|
val score: Int
|
||||||
|
)
|
||||||
@@ -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,6 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
data class Login(
|
||||||
|
val email: String,
|
||||||
|
val password: String
|
||||||
|
)
|
||||||
@@ -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.*
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
import org.springframework.core.io.InputStreamResource
|
||||||
|
import org.springframework.http.MediaType
|
||||||
|
|
||||||
|
data class PhotoFile(val resource: InputStreamResource, val contentType: MediaType)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
data class Team(
|
||||||
|
val id: TeamId = UUID.randomUUID(),
|
||||||
|
val name: String
|
||||||
|
)
|
||||||
@@ -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
|
||||||
|
)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.domain
|
||||||
|
|
||||||
|
data class TeamLeaderboardEntry(
|
||||||
|
val rank: Int,
|
||||||
|
val teamName: String,
|
||||||
|
val score: Int
|
||||||
|
)
|
||||||
@@ -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,
|
||||||
|
)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.record
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity
|
||||||
|
import jakarta.persistence.Id
|
||||||
|
import jakarta.persistence.Table
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "refresh_token")
|
||||||
|
data class RefreshTokenRecord(
|
||||||
|
@Id
|
||||||
|
val token: RefreshId,
|
||||||
|
val email: String,
|
||||||
|
val expiryDateTime: 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,14 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Future
|
||||||
|
import jakarta.validation.constraints.NotBlank
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
data class HuntCreateRequest(
|
||||||
|
@field:NotBlank(message = "Hunt title is required")
|
||||||
|
val title: String,
|
||||||
|
@field:Future
|
||||||
|
val startDateTime: LocalDateTime,
|
||||||
|
@field:Future
|
||||||
|
val endDateTime: LocalDateTime,
|
||||||
|
)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
enum class HuntStatus {
|
||||||
|
UNSTARTED,
|
||||||
|
ONGOING,
|
||||||
|
CLOSED
|
||||||
|
}
|
||||||
@@ -1,7 +1,14 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.model.request
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email
|
||||||
|
import jakarta.validation.constraints.NotBlank
|
||||||
|
|
||||||
data class HunterSignupRequest(
|
data class HunterSignupRequest(
|
||||||
|
@field:Email(message = "Must be a valid email address")
|
||||||
|
@field:NotBlank(message = "Email must not be blank")
|
||||||
val email: String,
|
val email: String,
|
||||||
|
@field:NotBlank(message = "Name cannot be blank")
|
||||||
val name: String,
|
val name: String,
|
||||||
|
@field:NotBlank(message = "Password cannot be blank")
|
||||||
val password: String
|
val password: String
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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,10 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank
|
||||||
|
|
||||||
|
data class LoginRequest(
|
||||||
|
@field:NotBlank(message = "Email cannot be blank")
|
||||||
|
val email: String,
|
||||||
|
@field:NotBlank(message = "Password cannot be blank")
|
||||||
|
val password: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
|
||||||
|
data class LogoutRequest(
|
||||||
|
@field:NotBlank(message = "You must provide a refresh token.")
|
||||||
|
val refreshToken: RefreshId
|
||||||
|
)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
|
||||||
|
data class RefreshRequest(
|
||||||
|
@field:NotBlank(message = "Refresh token cannot be blank")
|
||||||
|
val refreshToken: RefreshId,
|
||||||
|
)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.PhotoStatus
|
||||||
|
|
||||||
|
data class ReviewPhotoRequest(
|
||||||
|
@field:NotBlank(message = "Status must not be blank")
|
||||||
|
val status: PhotoStatus
|
||||||
|
)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.request
|
||||||
|
|
||||||
|
data class TeamRequest(
|
||||||
|
val name: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
data class HuntResponse(
|
||||||
|
val id: HuntId,
|
||||||
|
val title: String,
|
||||||
|
val startDateTime: LocalDateTime,
|
||||||
|
val endDateTime: LocalDateTime,
|
||||||
|
val isTerminated: Boolean
|
||||||
|
)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
data class HunterLeaderboardResponse(
|
||||||
|
val rank: Int,
|
||||||
|
val hunterName: String,
|
||||||
|
val score: Int
|
||||||
|
)
|
||||||
@@ -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,8 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
|
||||||
|
data class LoginResponse(
|
||||||
|
val accessToken: String,
|
||||||
|
val refreshToken: RefreshId
|
||||||
|
)
|
||||||
@@ -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,9 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.FoundStatus
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ItemId
|
||||||
|
|
||||||
|
data class TeamItemResponse(
|
||||||
|
val id: ItemId,
|
||||||
|
val itemFoundStatus: FoundStatus
|
||||||
|
)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
data class TeamLeaderboardResponse(
|
||||||
|
val rank: Int,
|
||||||
|
val teamName: String,
|
||||||
|
val score: Int
|
||||||
|
)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.model.response
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
|
|
||||||
|
data class TeamResponse(
|
||||||
|
val id: TeamId,
|
||||||
|
val name: String
|
||||||
|
)
|
||||||
@@ -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>
|
||||||
@@ -1,9 +1,60 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.repository
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
import net.halfbinary.scavengerhuntapi.model.HuntId
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
import net.halfbinary.scavengerhuntapi.model.record.HuntRecord
|
import net.halfbinary.scavengerhuntapi.model.record.HuntRecord
|
||||||
import org.springframework.data.jpa.repository.JpaRepository
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
import org.springframework.data.jpa.repository.Query
|
||||||
import org.springframework.stereotype.Repository
|
import org.springframework.stereotype.Repository
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
interface HuntRepository : JpaRepository<HuntRecord, HuntId>
|
interface HuntRepository : JpaRepository<HuntRecord, HuntId> {
|
||||||
|
@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.id = :hunterId
|
||||||
|
AND h.is_terminated = FALSE
|
||||||
|
AND h.start_date_time < NOW()
|
||||||
|
AND h.end_date_time > NOW()
|
||||||
|
""", nativeQuery = true)
|
||||||
|
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("""
|
||||||
|
SELECT h.*
|
||||||
|
FROM hunt h
|
||||||
|
WHERE h.is_terminated = FALSE
|
||||||
|
AND h.start_date_time < NOW()
|
||||||
|
AND h.end_date_time > NOW()
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun findAllOngoing(): List<HuntRecord>
|
||||||
|
|
||||||
|
@Query("""
|
||||||
|
SELECT h.*
|
||||||
|
FROM hunt h
|
||||||
|
WHERE h.is_terminated = FALSE
|
||||||
|
AND h.start_date_time > NOW()
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun findAllUnstarted(): List<HuntRecord>
|
||||||
|
|
||||||
|
@Query("""
|
||||||
|
SELECT h.*
|
||||||
|
FROM hunt h
|
||||||
|
WHERE h.is_terminated = TRUE
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun findAllClosed(): List<HuntRecord>
|
||||||
|
}
|
||||||
@@ -3,7 +3,18 @@ package net.halfbinary.scavengerhuntapi.repository
|
|||||||
import net.halfbinary.scavengerhuntapi.model.HunterId
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
import net.halfbinary.scavengerhuntapi.model.record.HunterRecord
|
import net.halfbinary.scavengerhuntapi.model.record.HunterRecord
|
||||||
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 HunterRepository : JpaRepository<HunterRecord, HunterId>
|
interface HunterRepository : JpaRepository<HunterRecord, HunterId> {
|
||||||
|
fun findByEmail(email: String): HunterRecord?
|
||||||
|
|
||||||
|
@Query("""
|
||||||
|
SELECT h.*
|
||||||
|
FROM hunter h
|
||||||
|
WHERE h.email = :email
|
||||||
|
AND h.password = :password
|
||||||
|
""", nativeQuery = true)
|
||||||
|
fun login(email: String, password: String): HunterRecord?
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.TeamId
|
||||||
|
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>
|
||||||
|
fun findByTeamId(teamId: TeamId): 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,16 @@
|
|||||||
|
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.PhotoStatus
|
||||||
|
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 findByHuntIdAndItemId(huntId: HuntId, itemId: ItemId): List<PhotoRecord>
|
||||||
|
fun findByIdAndItemIdAndHuntId(id: PhotoId, itemId: ItemId, huntId: HuntId): PhotoRecord?
|
||||||
|
fun findByHuntIdAndStatus(huntId: HuntId, status: PhotoStatus): List<PhotoRecord>
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.repository
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.RefreshTokenRecord
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository
|
||||||
|
|
||||||
|
interface RefreshTokenRepository: JpaRepository<RefreshTokenRecord, RefreshId>
|
||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HunterId
|
||||||
|
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.repository.HuntItemRepository
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.HuntRepository
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.ItemRepository
|
||||||
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class HuntService(
|
||||||
|
private val huntRepository: HuntRepository,
|
||||||
|
private val itemRepository: ItemRepository,
|
||||||
|
private val huntItemRepository: HuntItemRepository
|
||||||
|
) {
|
||||||
|
fun getHunt(huntId: HuntId): Hunt {
|
||||||
|
return huntRepository.findByIdOrNull(huntId)?.toDomain() ?: throw NotFoundException("No hunt with id $huntId found")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getAllHunts(status: HuntStatus?): List<Hunt> {
|
||||||
|
return when(status) {
|
||||||
|
HuntStatus.UNSTARTED -> huntRepository.findAllUnstarted().map { it.toDomain() }
|
||||||
|
HuntStatus.ONGOING -> huntRepository.findAllOngoing().map { it.toDomain() }
|
||||||
|
HuntStatus.CLOSED -> huntRepository.findAllClosed().map { it.toDomain() }
|
||||||
|
else -> huntRepository.findAll().map { it.toDomain() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getHuntsByHunter(hunterId: HunterId): List<Hunt> {
|
||||||
|
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 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
||||||
|
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
||||||
|
import org.springframework.security.core.userdetails.User
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class HunterDetailsService(private val hunterRepository: HunterRepository): UserDetailsService {
|
||||||
|
override fun loadUserByUsername(username: String): UserDetails {
|
||||||
|
hunterRepository.findByEmail(username)
|
||||||
|
?.let { hunter ->
|
||||||
|
val hunterAuthorities =
|
||||||
|
if (hunter.isAdmin) {
|
||||||
|
SimpleGrantedAuthority("ROLE_ADMIN")
|
||||||
|
} else {
|
||||||
|
SimpleGrantedAuthority("ROLE_USER")
|
||||||
|
}
|
||||||
|
return User(
|
||||||
|
hunter.email,
|
||||||
|
hunter.password,
|
||||||
|
Collections.singleton(hunterAuthorities)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
throw UsernameNotFoundException("User Not Found with username: $username")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,20 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.LoginFailedException
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.converter.toDomain
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.domain.Login
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class LoginService(private val hunterRepository: HunterRepository) {
|
||||||
|
companion object {
|
||||||
|
private val log = LoggerFactory.getLogger(LoginService::class.java)
|
||||||
|
}
|
||||||
|
fun login(login: Login): Hunter {
|
||||||
|
log.info("Logging in with email: ${login.email}")
|
||||||
|
return hunterRepository.login(login.email, login.password)?.toDomain()?:throw LoginFailedException()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,201 @@
|
|||||||
|
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.ConflictException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.ForbiddenException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.NotFoundException
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.FoundStatus
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.HuntId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.ImageVersion
|
||||||
|
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.domain.PhotoFile
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.response.PhotoResponse
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.PhotoRepository
|
||||||
|
import org.springframework.core.io.InputStreamResource
|
||||||
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
|
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
|
||||||
|
|
||||||
|
private const val PHOTO_NOT_FOUND = "Photo not found"
|
||||||
|
|
||||||
|
@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}_original${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 = try {
|
||||||
|
teamService.getTeamForHunterInHunt(huntId, email)
|
||||||
|
} catch (_: NotFoundException) {
|
||||||
|
throw ForbiddenException()
|
||||||
|
}
|
||||||
|
if (team.id != teamId) throw ForbiddenException()
|
||||||
|
}
|
||||||
|
|
||||||
|
val submitter = hunterService.getHunterById(photoRecord.hunterId)
|
||||||
|
return photoRecord.toDomain().toResponse(submitter)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getPhotoFile(photoId: PhotoId, email: String, version: ImageVersion = ImageVersion.LARGE): PhotoFile {
|
||||||
|
val requestingHunter = hunterService.getHunterByEmail(email)
|
||||||
|
val photoRecord = photoRepository.findByIdOrNull(photoId)
|
||||||
|
?: throw NotFoundException(PHOTO_NOT_FOUND)
|
||||||
|
|
||||||
|
if (!requestingHunter.isAdmin) {
|
||||||
|
val submitter = hunterService.getHunterById(photoRecord.hunterId)
|
||||||
|
try {
|
||||||
|
val requestingTeam =
|
||||||
|
teamService.getTeamForHunterInHunt(photoRecord.huntId, requestingHunter.email)
|
||||||
|
val submitterTeam =
|
||||||
|
teamService.getTeamForHunterInHunt(photoRecord.huntId, submitter.email)
|
||||||
|
if (requestingTeam.id != submitterTeam.id) throw ForbiddenException()
|
||||||
|
} catch (_: NotFoundException) {
|
||||||
|
throw ForbiddenException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val key = when (version) {
|
||||||
|
ImageVersion.ORIGINAL -> s3StorageService.findKeyByPrefix("${photoId}_original")
|
||||||
|
?: throw NotFoundException("Photo file not found")
|
||||||
|
ImageVersion.LARGE -> "${photoId}_large.jpg"
|
||||||
|
ImageVersion.MEDIUM -> "${photoId}_medium.jpg"
|
||||||
|
ImageVersion.SMALL -> "${photoId}_small.jpg"
|
||||||
|
}
|
||||||
|
val (stream, contentType) = s3StorageService.download(key)
|
||||||
|
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 removePhoto(huntId: HuntId, teamId: TeamId, itemId: ItemId, photoId: PhotoId, email: String) {
|
||||||
|
val photoRecord = photoRepository.findByIdAndItemIdAndHuntId(photoId, itemId, huntId)
|
||||||
|
?: throw NotFoundException(PHOTO_NOT_FOUND)
|
||||||
|
|
||||||
|
val team = try {
|
||||||
|
teamService.getTeamForHunterInHunt(huntId, email)
|
||||||
|
} catch (_: NotFoundException) {
|
||||||
|
throw ForbiddenException()
|
||||||
|
}
|
||||||
|
if (team.id != teamId) throw ForbiddenException()
|
||||||
|
|
||||||
|
if (photoRecord.status == PhotoStatus.APPROVED) throw ConflictException("Cannot remove an approved photo")
|
||||||
|
|
||||||
|
photoRepository.save(photoRecord.copy(status = PhotoStatus.REMOVED, statusChangeDateTime = LocalDateTime.now()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getItemPhotos(huntId: HuntId, teamId: TeamId, itemId: ItemId, email: String): List<PhotoResponse> {
|
||||||
|
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)
|
||||||
|
return photoRepository.findByHuntIdAndItemId(huntId, itemId)
|
||||||
|
.filter { it.hunterId in teamHunterIds && it.status != PhotoStatus.REMOVED }
|
||||||
|
.map { it.toDomain().toResponse(hunterService.getHunterById(it.hunterId)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fun updatePhotoStatus(photoId: PhotoId, status: PhotoStatus) {
|
||||||
|
val record = photoRepository.findByIdOrNull(photoId)
|
||||||
|
?: throw NotFoundException(PHOTO_NOT_FOUND)
|
||||||
|
photoRepository.save(record.copy(status = status, statusChangeDateTime = LocalDateTime.now()))
|
||||||
|
}
|
||||||
|
|
||||||
|
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,49 @@
|
|||||||
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.config.JwtUtil
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.ExpiredRefreshTokenException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.InvalidRefreshTokenException
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.RefreshId
|
||||||
|
import net.halfbinary.scavengerhuntapi.model.record.RefreshTokenRecord
|
||||||
|
import net.halfbinary.scavengerhuntapi.repository.RefreshTokenRepository
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
import java.time.LocalDateTime
|
||||||
|
import java.time.temporal.ChronoUnit
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class RefreshTokenService(private val refreshTokenRepository: RefreshTokenRepository, private val jwtUtil: JwtUtil) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val log = LoggerFactory.getLogger(RefreshTokenService::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getAccessToken(tokenId: RefreshId): String {
|
||||||
|
return getToken(tokenId)?.let { refreshToken ->
|
||||||
|
if (isTokenExpired(refreshToken)) {
|
||||||
|
removeToken(tokenId)
|
||||||
|
throw ExpiredRefreshTokenException(tokenId)
|
||||||
|
} else {
|
||||||
|
jwtUtil.generateToken(refreshToken.email)
|
||||||
|
}
|
||||||
|
}?: throw InvalidRefreshTokenException(tokenId)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun generateRefreshToken(email: String): RefreshId {
|
||||||
|
return refreshTokenRepository.save(RefreshTokenRecord(RefreshId.randomUUID(), email, LocalDateTime.now().plus(1, ChronoUnit.MONTHS))).token
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isTokenExpired(token: RefreshTokenRecord): Boolean {
|
||||||
|
return token.expiryDateTime.isBefore(LocalDateTime.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getToken(token: RefreshId): RefreshTokenRecord? {
|
||||||
|
return refreshTokenRepository.findByIdOrNull(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeToken(token: RefreshId) {
|
||||||
|
log.debug("Removing refresh token: $token")
|
||||||
|
refreshTokenRepository.deleteById(token)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
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.GetObjectRequest
|
||||||
|
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request
|
||||||
|
import software.amazon.awssdk.services.s3.model.PutObjectRequest
|
||||||
|
import java.io.InputStream
|
||||||
|
|
||||||
|
@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)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun findKeyByPrefix(prefix: String): String? {
|
||||||
|
val response = s3Client.listObjectsV2(
|
||||||
|
ListObjectsV2Request.builder()
|
||||||
|
.bucket(bucket)
|
||||||
|
.prefix(prefix)
|
||||||
|
.maxKeys(1)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
return response.contents().firstOrNull()?.key()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun download(key: String): Pair<InputStream, String> {
|
||||||
|
val response = s3Client.getObject(
|
||||||
|
GetObjectRequest.builder()
|
||||||
|
.bucket(bucket)
|
||||||
|
.key(key)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
return Pair(response, response.response().contentType())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,31 @@
|
|||||||
package net.halfbinary.scavengerhuntapi.service
|
package net.halfbinary.scavengerhuntapi.service
|
||||||
|
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.InvalidEmailException
|
||||||
|
import net.halfbinary.scavengerhuntapi.error.exception.PreexistingAccountException
|
||||||
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
import net.halfbinary.scavengerhuntapi.model.converter.toRecord
|
||||||
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
|
import net.halfbinary.scavengerhuntapi.model.domain.Hunter
|
||||||
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
import net.halfbinary.scavengerhuntapi.repository.HunterRepository
|
||||||
|
import org.apache.commons.validator.routines.EmailValidator
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class SignupService(private val hunterRepository: HunterRepository) {
|
class SignupService(private val hunterRepository: HunterRepository) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private val log = LoggerFactory.getLogger(SignupService::class.java)
|
||||||
|
}
|
||||||
fun createNewHunter(hunter: Hunter) {
|
fun createNewHunter(hunter: Hunter) {
|
||||||
|
log.info("Creating new Hunter with email: ${hunter.email}...")
|
||||||
|
if (!EmailValidator.getInstance().isValid(hunter.email)) {
|
||||||
|
log.error("Invalid email ${hunter.email}")
|
||||||
|
throw InvalidEmailException(hunter.email)
|
||||||
|
}
|
||||||
|
if (hunterRepository.findByEmail(hunter.email) != null) {
|
||||||
|
log.error("Hunter ${hunter.email} already exists")
|
||||||
|
throw PreexistingAccountException()
|
||||||
|
}
|
||||||
hunterRepository.save(hunter.toRecord())
|
hunterRepository.save(hunter.toRecord())
|
||||||
|
log.info("...Created new Hunter with email: ${hunter.email}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user