feat(api): get latest activity by user

This commit is contained in:
Gauthier Roebroeck 2021-06-28 14:09:33 +08:00
parent 622f725896
commit b118959775
3 changed files with 21 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import java.time.LocalDateTime
interface AuthenticationActivityRepository {
fun findAll(pageable: Pageable): Page<AuthenticationActivity>
fun findAllByUser(user: KomgaUser, pageable: Pageable): Page<AuthenticationActivity>
fun findMostRecentByUser(user: KomgaUser): AuthenticationActivity?
fun insert(activity: AuthenticationActivity)

View File

@ -43,6 +43,15 @@ class AuthenticationActivityDao(
return findAll(conditions, pageable)
}
override fun findMostRecentByUser(user: KomgaUser): AuthenticationActivity? =
dsl.selectFrom(aa)
.where(aa.USER_ID.eq(user.id))
.or(aa.EMAIL.eq(user.email))
.orderBy(aa.DATE_TIME.desc())
.limit(1)
.fetchOne()
?.toDomain()
private fun findAll(conditions: Condition, pageable: Pageable): PageImpl<AuthenticationActivity> {
val count = dsl.fetchCount(aa, conditions)

View File

@ -31,7 +31,6 @@ import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
@ -198,4 +197,15 @@ class UserController(
return authenticationActivityRepository.findAll(pageRequest).map { it.toDto() }
}
@GetMapping("{id}/authentication-activity/latest")
@PreAuthorize("hasRole('$ROLE_ADMIN') or #principal.user.id == #id")
fun getLatestAuthenticationActivityForUser(
@PathVariable id: String,
@AuthenticationPrincipal principal: KomgaPrincipal,
): AuthenticationActivityDto =
userRepository.findByIdOrNull(id)?.let { user ->
authenticationActivityRepository.findMostRecentByUser(user)?.toDto()
?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
} ?: throw ResponseStatusException(HttpStatus.NOT_FOUND)
}