diff --git a/src/main/kotlin/com/doongjun/commitmon/animation/AdventureController.kt b/src/main/kotlin/com/doongjun/commitmon/animation/AdventureController.kt index 10cbd66..031b63a 100644 --- a/src/main/kotlin/com/doongjun/commitmon/animation/AdventureController.kt +++ b/src/main/kotlin/com/doongjun/commitmon/animation/AdventureController.kt @@ -3,7 +3,10 @@ package com.doongjun.commitmon.animation import com.doongjun.commitmon.app.AdventureFacade import com.doongjun.commitmon.app.Theme import com.doongjun.commitmon.app.UserFetchType +import com.doongjun.commitmon.domain.Commitmon +import com.doongjun.commitmon.extension.currentUserId import com.doongjun.commitmon.extension.findBy +import io.swagger.v3.oas.annotations.Operation import jakarta.servlet.http.HttpServletResponse import org.springframework.http.HttpHeaders import org.springframework.web.bind.annotation.GetMapping @@ -14,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController class AdventureController( private val adventureFacade: AdventureFacade, ) { + @Operation(summary = "유저 애니메이션 SVG") @GetMapping("/adventure", produces = ["image/svg+xml"]) fun getAdventure( @RequestParam username: String, @@ -31,4 +35,19 @@ class AdventureController( userFetchType = UserFetchType::title.findBy(userFetchType?.lowercase()), ) } + + @Operation(summary = "로그인 유저 애니메이션 미리보기 SVG") + @GetMapping("/adventure/preview", produces = ["image/svg+xml"]) + fun getAdventurePreview( + @RequestParam(required = false) commitmon: Commitmon?, + @RequestParam(defaultValue = "GRASSLAND") theme: Theme, + @RequestParam(defaultValue = "SOLO") userFetchType: UserFetchType, + response: HttpServletResponse, + ): String = + adventureFacade.getAnimationPreview( + userId = currentUserId!!, + commitmon = commitmon, + theme = theme, + userFetchType = userFetchType, + ) } diff --git a/src/main/kotlin/com/doongjun/commitmon/app/AdventureFacade.kt b/src/main/kotlin/com/doongjun/commitmon/app/AdventureFacade.kt index 48836b7..0987d35 100644 --- a/src/main/kotlin/com/doongjun/commitmon/app/AdventureFacade.kt +++ b/src/main/kotlin/com/doongjun/commitmon/app/AdventureFacade.kt @@ -3,6 +3,7 @@ package com.doongjun.commitmon.app import com.doongjun.commitmon.app.data.CreateUserDto import com.doongjun.commitmon.app.data.GetUserDto import com.doongjun.commitmon.core.AdventureGenerator +import com.doongjun.commitmon.domain.Commitmon import com.doongjun.commitmon.event.UpdateUserInfo import org.slf4j.LoggerFactory import org.springframework.context.ApplicationEventPublisher @@ -35,6 +36,26 @@ class AdventureFacade( theme = theme ?: Theme.GRASSLAND, ) + fun getAnimationPreview( + userId: Long, + commitmon: Commitmon?, + theme: Theme, + userFetchType: UserFetchType, + ): String { + val user = + userService.get( + id = userId, + userFetchType = userFetchType, + ) + + user.commitmon = commitmon ?: user.commitmon + + return createAnimation( + user = user, + theme = theme, + ) + } + private fun getOrCreateUser( username: String, userFetchType: UserFetchType, diff --git a/src/main/kotlin/com/doongjun/commitmon/app/UserService.kt b/src/main/kotlin/com/doongjun/commitmon/app/UserService.kt index 6dc69d5..104ed03 100644 --- a/src/main/kotlin/com/doongjun/commitmon/app/UserService.kt +++ b/src/main/kotlin/com/doongjun/commitmon/app/UserService.kt @@ -7,6 +7,7 @@ import com.doongjun.commitmon.app.data.UpdateUserDto import com.doongjun.commitmon.domain.Commitmon import com.doongjun.commitmon.domain.User import com.doongjun.commitmon.domain.UserRepository +import org.springframework.cache.CacheManager import org.springframework.cache.annotation.Cacheable import org.springframework.data.repository.findByIdOrNull import org.springframework.stereotype.Service @@ -16,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional @Transactional class UserService( private val userRepository: UserRepository, + private val cacheManager: CacheManager, ) { @Transactional(readOnly = true) fun existsByName(name: String) = userRepository.existsByName(name) @@ -83,5 +85,14 @@ class UserService( user.changeCommitmon(commitmon) userRepository.save(user) + + clearCache(user.name) + } + + private fun clearCache(name: String) { + val cache = cacheManager.getCache("userInfo") + UserFetchType.entries.forEach { userFetchType -> + cache?.evictIfPresent(name + '-' + userFetchType.title) + } } } diff --git a/src/main/kotlin/com/doongjun/commitmon/app/data/GetUserDto.kt b/src/main/kotlin/com/doongjun/commitmon/app/data/GetUserDto.kt index 19a987b..995daf8 100644 --- a/src/main/kotlin/com/doongjun/commitmon/app/data/GetUserDto.kt +++ b/src/main/kotlin/com/doongjun/commitmon/app/data/GetUserDto.kt @@ -12,7 +12,7 @@ data class GetUserDto( val id: Long, val name: String, val totalCommitCount: Long, - val commitmon: Commitmon, + var commitmon: Commitmon, val exp: Int = 0, val fetchedUsers: List, private val followerIds: List, diff --git a/src/main/kotlin/com/doongjun/commitmon/config/SwaggerConfig.kt b/src/main/kotlin/com/doongjun/commitmon/config/SwaggerConfig.kt index d57647d..bc7c26f 100644 --- a/src/main/kotlin/com/doongjun/commitmon/config/SwaggerConfig.kt +++ b/src/main/kotlin/com/doongjun/commitmon/config/SwaggerConfig.kt @@ -38,4 +38,12 @@ class SwaggerConfig { .group("api-v1-definition") .pathsToMatch("/api/**") .build() + + @Bean + fun animation(): GroupedOpenApi = + GroupedOpenApi + .builder() + .group("animation-definition") + .pathsToMatch("/adventure/**") + .build() }