Skip to content

Commit

Permalink
Merge pull request #10 from mash-up-kr/feature/kt-lint
Browse files Browse the repository at this point in the history
Apply ktlint kikkik
  • Loading branch information
god9599 authored Jun 23, 2024
2 parents 00509db + 566e787 commit 9ce48eb
Show file tree
Hide file tree
Showing 35 changed files with 272 additions and 217 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[{*.gradle.kts,*.kt,*.kts,*.main.kts}]
ij_kotlin_allow_trailing_comma = true
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647

[*.md]
trim_trailing_whitespace = false

[{*.yml, *.yaml}]
indent_size = 2
5 changes: 4 additions & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ jobs:
format: YYYY-MM-DDTHH-mm-ss
utcOffset: "+09:00"

- name: Run ktlintFormat
run: chmod +x gradlew && ./gradlew ktlintFormat

- name: build test
run: chmod +x gradlew && ./gradlew build
run: ./gradlew build
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

plugins {
kotlin("jvm") version "1.9.24"
kotlin("plugin.spring") version "1.9.24" apply false
kotlin("plugin.jpa") version "1.9.24" apply false
id("org.springframework.boot") version "3.3.0" apply false
id("io.spring.dependency-management") version "1.1.5" apply false
id("com.google.cloud.tools.jib") version "3.4.3" apply false
id("org.jlleitschuh.gradle.ktlint") version "12.1.1" apply false
}

java.sourceCompatibility = JavaVersion.VERSION_21
Expand All @@ -25,6 +27,7 @@ subprojects {
apply(plugin = "org.jetbrains.kotlin.plugin.spring")
apply(plugin = "org.springframework.boot")
apply(plugin = "io.spring.dependency-management")
apply(plugin = "org.jlleitschuh.gradle.ktlint")

dependencies {
implementation("org.springframework.boot:spring-boot-starter")
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jjwtVersion=0.11.5
mysqlConnectorVersion=8.0.33
kotlin.code.style=official
springDocOpenApiVersion=2.0.2
jasyptVersion=3.0.5
28 changes: 15 additions & 13 deletions pic-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,26 @@ configure<JibExtension> {

container {
// TODO: 서버 스펙에 따라 Xmx/Xms, Initial/Min/MaxRAMFraction 설정
jvmFlags = listOf(
"-server",
"-XX:+UseContainerSupport",
"-XX:+UseStringDeduplication",
"-Dserver.port=8080",
"-Dfile.encoding=UTF-8",
"-Djava.awt.headless=true",
"-Dspring.profiles.active=${activeProfile}"
)
jvmFlags =
listOf(
"-server",
"-XX:+UseContainerSupport",
"-XX:+UseStringDeduplication",
"-Dserver.port=8080",
"-Dfile.encoding=UTF-8",
"-Djava.awt.headless=true",
"-Dspring.profiles.active=$activeProfile",
)
ports = listOf("8080")
environment = mapOf(
"TZ" to "Asia/Seoul"
)
environment =
mapOf(
"TZ" to "Asia/Seoul",
)
}
}

fun getProfileAndImageName(registryUsername: String?): Array<String> {
val containerImageName = "${registryUsername}/${project.name}"
val containerImageName = "$registryUsername/${project.name}"
if (project.hasProperty("release")) {
return arrayOf("release", containerImageName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.mashup.pic.auth.applicationService
import com.mashup.pic.auth.applicationService.dto.LoginServiceRequest
import com.mashup.pic.auth.controller.dto.LoginResponse
import com.mashup.pic.domain.user.User
import com.mashup.pic.security.jwt.JwtManager
import com.mashup.pic.domain.user.UserService
import com.mashup.pic.security.authentication.UserInfo
import com.mashup.pic.security.jwt.JwtManager
import com.mashup.pic.security.oidc.KakaoIdTokenValidator
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -15,32 +15,33 @@ import org.springframework.transaction.annotation.Transactional
class AuthApplicationService(
private val userService: UserService,
private val jwtTokenUtil: JwtManager,
private val idTokenValidator: KakaoIdTokenValidator
private val idTokenValidator: KakaoIdTokenValidator,
) {

@Transactional
fun login(request: LoginServiceRequest): LoginResponse {
val oAuthId = idTokenValidator.validateAndGetId(request.idToken, request.nickname)
val user = userService.findUserByOAuthIdOrNull(oAuthId)?: createUser(oAuthId, request)
val user = userService.findUserByOAuthIdOrNull(oAuthId) ?: createUser(oAuthId, request)

val authToken = jwtTokenUtil.generateAuthToken(user.toUserInfo())
return LoginResponse.from(user, authToken)
}

private fun createUser(oAuthId: Long, request: LoginServiceRequest) : User {
private fun createUser(
oAuthId: Long,
request: LoginServiceRequest,
): User {
return userService.create(
oAuthId = oAuthId,
nickname = request.nickname,
profileImage = request.profileImage
oAuthId = oAuthId,
nickname = request.nickname,
profileImage = request.profileImage,
)
}

fun User.toUserInfo(): UserInfo {
return UserInfo(
id = this.id,
nickname = this.nickname,
roles = this.roles
id = this.id,
nickname = this.nickname,
roles = this.roles,
)
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mashup.pic.auth.applicationService.dto

data class LoginServiceRequest(
val idToken: String,
val provider: LoginProvider,
val nickname: String,
val profileImage: String
val idToken: String,
val provider: LoginProvider,
val nickname: String,
val profileImage: String,
)

enum class LoginProvider {
KAKAO, NAVER, GOOGLE
KAKAO,
NAVER,
GOOGLE,
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package com.mashup.pic.auth.controller

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
import com.mashup.pic.auth.applicationService.AuthApplicationService
import com.mashup.pic.auth.controller.dto.LoginRequest
import com.mashup.pic.auth.controller.dto.LoginResponse
import com.mashup.pic.common.ApiResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
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

@Tag(name = "인증 컨트롤러")
@RestController
@RequestMapping("/api/v1/auth")
class AuthController(
private val authApplicationService: AuthApplicationService,
private val authApplicationService: AuthApplicationService,
) {

@Operation(summary = "로그인")
@PostMapping("/login")
fun login(
@Valid @RequestBody loginRequest: LoginRequest
@Valid @RequestBody loginRequest: LoginRequest,
): ApiResponse<LoginResponse> {
return ApiResponse.success(authApplicationService.login(loginRequest.toServiceRequest()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import com.mashup.pic.auth.applicationService.dto.LoginServiceRequest
import jakarta.validation.constraints.NotBlank

data class LoginRequest(
@NotBlank val idToken: String,
@NotBlank val provider: LoginProvider,
@NotBlank val nickname: String,
@NotBlank val profileImage: String
@NotBlank val idToken: String,
@NotBlank val provider: LoginProvider,
@NotBlank val nickname: String,
@NotBlank val profileImage: String,
) {

fun toServiceRequest(): LoginServiceRequest {
return LoginServiceRequest(
idToken = idToken,
provider = provider,
nickname = nickname,
profileImage = profileImage
idToken = idToken,
provider = provider,
nickname = nickname,
profileImage = profileImage,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package com.mashup.pic.auth.controller.dto
import com.mashup.pic.domain.user.User
import com.mashup.pic.security.authentication.AuthToken


data class LoginResponse(
val userId: Long,
val nickname: String,
val accessToken: String,
val refreshToken: String
val userId: Long,
val nickname: String,
val accessToken: String,
val refreshToken: String,
) {
companion object {
fun from(user: User, authToken: AuthToken): LoginResponse {
fun from(
user: User,
authToken: AuthToken,
): LoginResponse {
return LoginResponse(
userId = user.id,
nickname = user.nickname,
accessToken = authToken.accessToken,
refreshToken = authToken.refreshToken
userId = user.id,
nickname = user.nickname,
accessToken = authToken.accessToken,
refreshToken = authToken.refreshToken,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,43 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep

@RestControllerAdvice
class ApiExceptionHandler {

private val log: Logger = LoggerFactory.getLogger(ApiExceptionHandler::class.java)

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentTypeMismatchException::class)
private fun handlerMethodArgumentNotValidException(
exception: MethodArgumentTypeMismatchException
): ApiResponse<Any> {
private fun handlerMethodArgumentNotValidException(exception: MethodArgumentTypeMismatchException): ApiResponse<Any> {
log.error("MethodArgumentTypeMismatchException handler", exception)
return ApiResponse.fail(
exceptionType = PicExceptionType.METHOD_ARGUMENT_TYPE_MISMATCH_VALUE,
message = exception.message
message = exception.message,
)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException::class)
private fun handleMethodArgumentNotValidException(
exception: MethodArgumentNotValidException
): ApiResponse<Any> {
private fun handleMethodArgumentNotValidException(exception: MethodArgumentNotValidException): ApiResponse<Any> {
log.error("MethodArgumentNotValidException handler", exception)
val errorMessage = exception.allErrors.joinToString(" ,")
return ApiResponse.fail(PicExceptionType.ARGUMENT_NOT_VALID, errorMessage)
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException::class)
private fun handleMissingServletRequestParameterException(
exception: MissingServletRequestParameterException,
): ApiResponse<Any> {
private fun handleMissingServletRequestParameterException(exception: MissingServletRequestParameterException): ApiResponse<Any> {
log.error("MissingServletRequestParameterException handler", exception)
return ApiResponse.fail(
exceptionType = PicExceptionType.INVALID_INPUT,
message = exception.message
message = exception.message,
)
}

@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException::class)
private fun httpRequestMethodNotSupportedException(
exception: HttpRequestMethodNotSupportedException
): ApiResponse<Any> {
private fun httpRequestMethodNotSupportedException(exception: HttpRequestMethodNotSupportedException): ApiResponse<Any> {
log.error("MethodNotSupportedException handler", exception)
return ApiResponse.fail(
exceptionType = PicExceptionType.HTTP_REQUEST_METHOD_NOT_SUPPORTED,
message = exception.message
message = exception.message,
)
}

Expand All @@ -71,7 +62,7 @@ class ApiExceptionHandler {
log.error("AccessDeniedException handler", exception)
return ApiResponse.fail(
exceptionType = PicExceptionType.ACCESS_DENIED,
message = exception.message
message = exception.message,
)
}

Expand All @@ -89,12 +80,13 @@ class ApiExceptionHandler {
log.error("Exception handler", exception)
return ApiResponse.fail(
exceptionType = PicExceptionType.SYSTEM_FAIL,
message = exception.message
message = exception.message,
)
}

private fun PicException.toErrorResponse(): ErrorResponse = ErrorResponse(
code = errorCode,
message = message
)
}
private fun PicException.toErrorResponse(): ErrorResponse =
ErrorResponse(
code = errorCode,
message = message,
)
}
Loading

0 comments on commit 9ce48eb

Please sign in to comment.