Files
luminic-space-back-v2/src/main/kotlin/space/luminic/finance/services/UserService.kt
2025-10-31 17:11:40 +03:00

43 lines
1.3 KiB
Kotlin

package space.luminic.finance.services
import org.slf4j.LoggerFactory
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
import space.luminic.finance.models.NotFoundException
import space.luminic.finance.models.User
import space.luminic.finance.repos.UserRepo
@Service
class UserService(val userRepo: UserRepo) {
val logger = LoggerFactory.getLogger(javaClass)
@Cacheable("users", key = "#username")
fun getByUsername(username: String): User {
return userRepo.findByUsername(username) ?: throw NotFoundException("User with username: $username not found")
}
fun getById(id: Int): User {
return userRepo.findById(id) ?: throw NotFoundException("User with id: $id not found")
}
fun getUserByTelegramId(telegramId: Long): User {
return userRepo.findByTgId(telegramId)?: throw NotFoundException("User with telegramId: $telegramId not found")
}
@Cacheable("users", key = "#username")
fun getByUserNameWoPass(username: String): User {
return userRepo.findByUsername(username) ?: throw NotFoundException("User with username: $username not found")
}
@Cacheable("usersList")
fun getUsers(): List<User> {
return userRepo.findAll()
}
}