diff --git a/src/main/kotlin/org/jaqpot/api/mapper/PageMapper.kt b/src/main/kotlin/org/jaqpot/api/mapper/PageMapper.kt index d76e5b7..58d40c9 100644 --- a/src/main/kotlin/org/jaqpot/api/mapper/PageMapper.kt +++ b/src/main/kotlin/org/jaqpot/api/mapper/PageMapper.kt @@ -7,9 +7,9 @@ import org.jaqpot.api.model.GetModels200ResponseDto import org.jaqpot.api.model.UserDto import org.springframework.data.domain.Page -fun Page.toGetModels200ResponseDto(creatorDto: UserDto?): GetModels200ResponseDto { +fun Page.toGetModels200ResponseDto(modelToUserMap: Map?): 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, diff --git a/src/main/kotlin/org/jaqpot/api/mapper/UserSettingsMapper.kt b/src/main/kotlin/org/jaqpot/api/mapper/UserSettingsMapper.kt index f22818b..5d7686f 100644 --- a/src/main/kotlin/org/jaqpot/api/mapper/UserSettingsMapper.kt +++ b/src/main/kotlin/org/jaqpot/api/mapper/UserSettingsMapper.kt @@ -9,7 +9,7 @@ fun UserSettings.toDto(isUpciUser: Boolean, isAdmin: Boolean): UserSettingsDto { darkMode = this.darkMode, collapseSidebar = this.collapseSidebar, isAdmin = isAdmin, - isUpci = isUpciUser + isUpciUser = isUpciUser ) } diff --git a/src/main/kotlin/org/jaqpot/api/service/authentication/UserCacheService.kt b/src/main/kotlin/org/jaqpot/api/service/authentication/UserCacheService.kt index b358da5..fcc2593 100644 --- a/src/main/kotlin/org/jaqpot/api/service/authentication/UserCacheService.kt +++ b/src/main/kotlin/org/jaqpot/api/service/authentication/UserCacheService.kt @@ -16,7 +16,7 @@ class UserCacheService( ) : UserService { private val usersCache: Cache = Caffeine.newBuilder() - .expireAfterWrite(1, TimeUnit.HOURS) + .expireAfterWrite(4, TimeUnit.HOURS) .maximumSize(10_000) .build() diff --git a/src/main/kotlin/org/jaqpot/api/service/model/ModelService.kt b/src/main/kotlin/org/jaqpot/api/service/model/ModelService.kt index 66191b6..e92e8e8 100644 --- a/src/main/kotlin/org/jaqpot/api/service/model/ModelService.kt +++ b/src/main/kotlin/org/jaqpot/api/service/model/ModelService.kt @@ -74,13 +74,28 @@ class ModelService( ) } + @PreAuthorize("hasAnyAuthority('admin', 'upci')") + override fun getAllModels(page: Int, size: Int, sort: List?): ResponseEntity { + 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?): ResponseEntity { 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( @@ -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) @@ -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) diff --git a/src/main/kotlin/org/jaqpot/api/service/usersettings/UserSettingsService.kt b/src/main/kotlin/org/jaqpot/api/service/usersettings/UserSettingsService.kt index dd51298..89f715d 100644 --- a/src/main/kotlin/org/jaqpot/api/service/usersettings/UserSettingsService.kt +++ b/src/main/kotlin/org/jaqpot/api/service/usersettings/UserSettingsService.kt @@ -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") diff --git a/src/main/resources/openapi.yaml b/src/main/resources/openapi.yaml index 27f32bc..0af33f6 100644 --- a/src/main/resources/openapi.yaml +++ b/src/main/resources/openapi.yaml @@ -35,28 +35,59 @@ 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 @@ -64,7 +95,7 @@ paths: - bearerAuth: [ ] tags: - model - operationId: getModels + operationId: getAllModels parameters: - name: page in: query @@ -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 @@ -1146,7 +1197,6 @@ paths: description: Unauthorized '404': description: Settings not found - post: summary: Create or update user settings operationId: saveUserSettings @@ -2135,7 +2185,7 @@ components: isAdmin: type: boolean readOnly: true - isUpci: + isUpciUser: type: boolean readOnly: true ApiKey: