Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
)
}
21 changes: 21 additions & 0 deletions src/main/kotlin/com/doongjun/commitmon/app/AdventureFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/doongjun/commitmon/app/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetSimpleUserDto>,
private val followerIds: List<Long>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}