Skip to content

Commit

Permalink
Merge pull request #118 from Kerosene-Labs/build-expenses
Browse files Browse the repository at this point in the history
Initial expenses work, switch to Kotlin
  • Loading branch information
hlafaille authored Dec 25, 2024
2 parents a3614ca + 54606ee commit 7fd0c01
Show file tree
Hide file tree
Showing 67 changed files with 803 additions and 866 deletions.
48 changes: 29 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.36</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -135,6 +129,16 @@
<version>0.12.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>2.0.0</version> <!-- Use your Kotlin version -->
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -148,6 +152,25 @@
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>2.0.0</version> <!-- Use the latest version -->
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
Expand All @@ -160,10 +183,6 @@
<source>21</source>
<target>21</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
Expand All @@ -175,15 +194,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.kerosenelabs.billtracker.argumentresolver

import com.kerosenelabs.billtracker.entity.UserEntity
import com.kerosenelabs.billtracker.exception.AuthException
import com.kerosenelabs.billtracker.service.UserService
import org.springframework.core.MethodParameter
import org.springframework.lang.Nullable
import org.springframework.stereotype.Component
import org.springframework.web.bind.support.WebDataBinderFactory
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer
import java.util.*

@Component
class UserArgumentResolver(private val userService: UserService) : HandlerMethodArgumentResolver {
override fun supportsParameter(parameter: MethodParameter): Boolean {
return (parameter.parameterType == UserEntity::class.java)
&& (parameter.parameterName != null && parameter.parameterName == "user")
}

@Throws(Exception::class)
override fun resolveArgument(
parameter: MethodParameter,
@Nullable mavContainer: ModelAndViewContainer?,
webRequest: NativeWebRequest,
@Nullable binderFactory: WebDataBinderFactory?
): Any {
// parse auth jwt
var authHeader = webRequest.getHeader("Authorization")
?: throw AuthException("Authorization header is required")
authHeader = authHeader.replace("Bearer ", "")
val userId = userService.getIdFromJwt(authHeader)
return userService.getUserById(UUID.fromString(userId))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.kerosenelabs.billtracker.config

import com.kerosenelabs.billtracker.exception.AuthException
import com.kerosenelabs.billtracker.exception.UnconfirmedUserException
import com.kerosenelabs.billtracker.model.response.ErrorResponse
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler

@ControllerAdvice
class GlobalExceptionHandler {
@ExceptionHandler(AuthException::class)
fun handleAuthException(e: AuthException): ResponseEntity<ErrorResponse> {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ErrorResponse(e.message.toString()))
}

@ExceptionHandler(UnconfirmedUserException::class)
fun handleUnconfirmedUserException(e: UnconfirmedUserException): ResponseEntity<ErrorResponse> {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ErrorResponse(e.message.toString()))
}
}

This file was deleted.

45 changes: 0 additions & 45 deletions src/main/java/com/kerosenelabs/billtracker/config/WebConfig.java

This file was deleted.

40 changes: 40 additions & 0 deletions src/main/java/com/kerosenelabs/billtracker/config/WebConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.kerosenelabs.billtracker.config

import com.kerosenelabs.billtracker.argumentresolver.UserArgumentResolver
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
open class WebConfig(private val userArgumentResolver: UserArgumentResolver) : WebMvcConfigurer {
override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) {
resolvers.add(userArgumentResolver)
}

@Bean
open fun corsConfigurer(): WebMvcConfigurer {
return object : WebMvcConfigurer {
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/**")
.allowedOrigins(
"http://localhost:5173",
"https://localhost",
"https://dev.billtracker.kerosenelabs.com",
"https://billtracker.kerosenelabs.com"
)
.allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600)
}
}
}

override fun configureContentNegotiation(configurer: ContentNegotiationConfigurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kerosenelabs.billtracker.controller

import org.springframework.web.bind.annotation.RestController

@RestController
class ExpensesController

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.kerosenelabs.billtracker.controller

import com.kerosenelabs.billtracker.exception.AuthException
import com.kerosenelabs.billtracker.model.request.HandleTokenRequest
import com.kerosenelabs.billtracker.model.response.HandleOAuth2TokenResponse
import com.kerosenelabs.billtracker.service.GoogleOAuth2ProviderService
import com.kerosenelabs.billtracker.service.UserService
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import java.io.IOException

@RestController
@Tag(name = "OAuth2")
class OAuth2Controller(
private val googleOAuth2ProviderService: GoogleOAuth2ProviderService,
private val userService: UserService
) {
@PostMapping("/oauth/google")
@ResponseStatus(HttpStatus.OK)
@Throws(
IOException::class,
AuthException::class
)
fun handleToken(@RequestBody request: HandleTokenRequest): HandleOAuth2TokenResponse {
val user = googleOAuth2ProviderService.handleCode(request.code)
val jwt = userService.establishJwt(user)
return HandleOAuth2TokenResponse(jwt)
}
}
Loading

0 comments on commit 7fd0c01

Please sign in to comment.