Skip to content

Commit

Permalink
feat(JAQPOT-426): add admin routes (#121)
Browse files Browse the repository at this point in the history
* feat(JAQPOT-426): add admin routes

* feat: show creator on models pages
  • Loading branch information
alarv authored Nov 22, 2024
1 parent 6e0aba1 commit c3c5f02
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/org/jaqpot/api/mapper/PageMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import org.jaqpot.api.model.GetModels200ResponseDto
import org.jaqpot.api.model.UserDto
import org.springframework.data.domain.Page

fun Page<Model>.toGetModels200ResponseDto(creatorDto: UserDto?): GetModels200ResponseDto {
fun Page<Model>.toGetModels200ResponseDto(modelToUserMap: Map<Long, UserDto>?): GetModels200ResponseDto {
return GetModels200ResponseDto(
this.content.map { it.toModelSummaryDto(creatorDto) },
this.content.map { it.toModelSummaryDto(modelToUserMap?.get(it.id)) },
this.totalElements.toInt(),
this.totalPages,
this.pageable.pageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fun UserSettings.toDto(isUpciUser: Boolean, isAdmin: Boolean): UserSettingsDto {
darkMode = this.darkMode,
collapseSidebar = this.collapseSidebar,
isAdmin = isAdmin,
isUpci = isUpciUser
isUpciUser = isUpciUser
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UserCacheService(
) : UserService {

private val usersCache: Cache<UserId, UserDto> = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.expireAfterWrite(4, TimeUnit.HOURS)
.maximumSize(10_000)
.build()

Expand Down
33 changes: 29 additions & 4 deletions src/main/kotlin/org/jaqpot/api/service/model/ModelService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,28 @@ class ModelService(
)
}

@PreAuthorize("hasAnyAuthority('admin', 'upci')")
override fun getAllModels(page: Int, size: Int, sort: List<String>?): ResponseEntity<GetModels200ResponseDto> {
val pageable = PageRequest.of(page, size, Sort.by(parseSortParameters(sort)))
val modelsPage = modelRepository.findAll(pageable)
val modelIdToUserMap = modelsPage.content.associateBy(
{ it.id!! },
{ userService.getUserById(it.creatorId).orElse(UserDto(it.creatorId)) }
)

return ResponseEntity.ok().body(modelsPage.toGetModels200ResponseDto(modelIdToUserMap))
}

override fun getModels(page: Int, size: Int, sort: List<String>?): ResponseEntity<GetModels200ResponseDto> {
val creatorId = authenticationFacade.userId
val pageable = PageRequest.of(page, size, Sort.by(parseSortParameters(sort)))
val modelsPage = modelRepository.findAllByCreatorId(creatorId, pageable)
val creator = userService.getUserById(creatorId).orElse(UserDto(creatorId))
val modelIdToUserMap = modelsPage.content.associateBy(
{ it.id!! },
{ userService.getUserById(it.creatorId).orElse(UserDto(it.creatorId)) }
)

return ResponseEntity.ok().body(modelsPage.toGetModels200ResponseDto(creator))
return ResponseEntity.ok().body(modelsPage.toGetModels200ResponseDto(modelIdToUserMap))
}

override fun getSharedModels(
Expand All @@ -98,7 +113,12 @@ class ModelService(
modelRepository.findAllSharedWithUserByOrganizationId(userId, pageable, organizationId)
}

return ResponseEntity.ok().body(sharedModelsPage.toGetModels200ResponseDto(null))
val modelIdToUserMap = sharedModelsPage.content.associateBy(
{ it.id!! },
{ userService.getUserById(it.creatorId).orElse(UserDto(it.creatorId)) }
)

return ResponseEntity.ok().body(sharedModelsPage.toGetModels200ResponseDto(modelIdToUserMap))
}

@CacheEvict("searchModels", allEntries = true)
Expand Down Expand Up @@ -352,7 +372,12 @@ class ModelService(
val transformedQuery = FullTextUtil.transformSearchQuery(query)
val pageable = PageRequest.of(page, size)
val modelsPage = modelRepository.searchModelsBy(transformedQuery, pageable)
return ResponseEntity.ok(modelsPage.toGetModels200ResponseDto(null))
val modelIdToUserMap = modelsPage.content.associateBy(
{ it.id!! },
{ userService.getUserById(it.creatorId).orElse(UserDto(it.creatorId)) }
)

return ResponseEntity.ok(modelsPage.toGetModels200ResponseDto(modelIdToUserMap))
}

@CacheEvict("searchModels", allEntries = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class UserSettingsService(

return userSettingsRepository.findByUserId(authenticationFacade.userId)
.map { ResponseEntity.ok(it.toDto(isUpciUser = isUpciUser, isAdmin = isAdmin)) }
.orElseGet { ResponseEntity.notFound().build() }
.orElseGet { ResponseEntity.ok(UserSettingsDto(isUpciUser = isUpciUser, isAdmin = isAdmin)) }
}

@CacheEvict(value = [CacheKeys.USER_SETTINGS], key = "#root.target.authenticationFacade.userId")
Expand Down
86 changes: 68 additions & 18 deletions src/main/resources/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,67 @@ paths:
description: JWT is valid
'401':
description: Unauthorized
/v1/models:
post:
summary: Create a new model
/v1/user/models:
get:
x-spring-paginated: true
summary: Get paginated models
security:
- bearerAuth: [ ]
tags:
- model
operationId: createModel
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Model'
operationId: getModels
parameters:
- name: page
in: query
required: false
schema:
type: integer
default: 0
- name: size
in: query
required: false
schema:
type: integer
default: 10
- name: sort
in: query
required: false
schema:
type: array
items:
type: string
example: [ "field1|asc", "field2|desc" ]
responses:
'201':
description: Model created successfully
'200':
description: Paginated list of models
content:
application/json:
schema:
type: object
properties:
content:
type: array
items:
$ref: '#/components/schemas/ModelSummary'
totalElements:
type: integer
totalPages:
type: integer
pageSize:
type: integer
pageNumber:
type: integer
'400':
description: Invalid input
x-stoplight:
id: 9ffjy7o77jc41
/v1/user/models:
/v1/models:
get:
x-spring-paginated: true
summary: Get paginated models
security:
- bearerAuth: [ ]
tags:
- model
operationId: getModels
operationId: getAllModels
parameters:
- name: page
in: query
Expand Down Expand Up @@ -108,6 +139,26 @@ paths:
type: integer
'400':
description: Invalid input
post:
summary: Create a new model
security:
- bearerAuth: [ ]
tags:
- model
operationId: createModel
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Model'
responses:
'201':
description: Model created successfully
'400':
description: Invalid input
x-stoplight:
id: 9ffjy7o77jc41
/v1/models/search:
get:
x-spring-paginated: true
Expand Down Expand Up @@ -1146,7 +1197,6 @@ paths:
description: Unauthorized
'404':
description: Settings not found

post:
summary: Create or update user settings
operationId: saveUserSettings
Expand Down Expand Up @@ -2135,7 +2185,7 @@ components:
isAdmin:
type: boolean
readOnly: true
isUpci:
isUpciUser:
type: boolean
readOnly: true
ApiKey:
Expand Down

0 comments on commit c3c5f02

Please sign in to comment.