Skip to content

Commit

Permalink
feat: Add endpoint returning all project keys with disabled languages (
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar authored Nov 5, 2024
1 parent 104467f commit d48333f
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import io.tolgee.hateoas.key.KeyModel
import io.tolgee.hateoas.key.KeyModelAssembler
import io.tolgee.hateoas.key.disabledLanguages.KeyDisabledLanguagesModel
import io.tolgee.hateoas.key.disabledLanguages.KeyDisabledLanguagesModelAssembler
import io.tolgee.model.enums.Scope
import io.tolgee.openApiDocs.OpenApiOrderExtension
import io.tolgee.security.ProjectHolder
import io.tolgee.security.authentication.AllowApiAccess
import io.tolgee.security.authorization.RequiresProjectPermissions
Expand All @@ -21,16 +24,18 @@ import org.springframework.web.bind.annotation.RestController
@CrossOrigin(origins = ["*"])
@RequestMapping(
value = [
"/v2/projects/{projectId}/all-keys",
"/v2/projects/{projectId}",
],
)
@Tag(name = "All localization keys", description = "All localization keys in the project")
class AllKeysController(
private val keyService: KeyService,
private val projectHolder: ProjectHolder,
private val keyModelAssembler: KeyModelAssembler,
private val keyDisabledLanguagesModelAssembler: KeyDisabledLanguagesModelAssembler,
) : IController {
@GetMapping(value = [""])
@OpenApiOrderExtension(order = 0)
@GetMapping(value = ["/all-keys"])
@Transactional
@Operation(summary = "Get all keys in project")
@RequiresProjectPermissions([ Scope.TRANSLATIONS_VIEW ])
Expand All @@ -39,4 +44,19 @@ class AllKeysController(
val allKeys = keyService.getAllSortedById(projectHolder.project.id)
return keyModelAssembler.toCollectionModel(allKeys)
}

@OpenApiOrderExtension(order = 1)
@GetMapping("/all-keys-with-disabled-languages")
@AllowApiAccess
@RequiresProjectPermissions([Scope.KEYS_VIEW])
@Operation(
summary = "Get disabled languages for all keys in project",
description =
"Returns all project key with any disabled language.\n\n" +
"If key has no disabled language, it is not returned.",
)
fun getDisabledLanguages(): CollectionModel<KeyDisabledLanguagesModel> {
val result = keyService.getDisabledLanguages(projectHolder.project.id)
return keyDisabledLanguagesModelAssembler.toCollectionModel(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.tolgee.hateoas.key.disabledLanguages

class KeyDisabledLanguageModel(
val id: Long,
val tag: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.tolgee.hateoas.key.disabledLanguages

import io.swagger.v3.oas.annotations.media.Schema
import org.springframework.hateoas.RepresentationModel
import org.springframework.hateoas.server.core.Relation
import java.io.Serializable

@Suppress("unused")
@Relation(collectionRelation = "keys", itemRelation = "key")
open class KeyDisabledLanguagesModel(
@Schema(description = "Id of key record")
val id: Long,
@Schema(description = "Name of key", example = "this_is_super_key")
val name: String,
@Schema(description = "Namespace of key", example = "homepage")
val namespace: String?,
@Schema(description = "Disabled languages")
val disabledLanguages: List<KeyDisabledLanguageModel>,
) : RepresentationModel<KeyDisabledLanguagesModel>(), Serializable
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.tolgee.hateoas.key.disabledLanguages

import io.tolgee.dtos.queryResults.keyDisabledLanguages.KeyDisabledLanguagesView
import org.springframework.hateoas.server.RepresentationModelAssembler
import org.springframework.stereotype.Component

@Component
class KeyDisabledLanguagesModelAssembler :
RepresentationModelAssembler<KeyDisabledLanguagesView, KeyDisabledLanguagesModel> {
override fun toModel(view: KeyDisabledLanguagesView): KeyDisabledLanguagesModel {
return KeyDisabledLanguagesModel(
id = view.id,
name = view.name,
namespace = view.namespace,
disabledLanguages =
view.disabledLanguages.map {
KeyDisabledLanguageModel(
id = it.id,
tag = it.tag,
)
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.tolgee.fixtures.andAssertThatJson
import io.tolgee.fixtures.andHasErrorMessage
import io.tolgee.fixtures.andIsBadRequest
import io.tolgee.fixtures.andIsOk
import io.tolgee.fixtures.node
import io.tolgee.model.enums.TranslationState
import io.tolgee.testing.annotations.ProjectJWTAuthTestMethod
import io.tolgee.testing.assert
Expand Down Expand Up @@ -39,6 +40,19 @@ class KeyControllerLanguageDisablingTest : ProjectAuthControllerTest("/v2/projec
}
}

@Test
@ProjectJWTAuthTestMethod
fun `get all disabled languages`() {
performProjectAuthGet("all-keys-with-disabled-languages")
.andIsOk.andAssertThatJson {
node("_embedded.keys") {
isArray.hasSize(1)
node("[0].disabledLanguages[0].id").isEqualTo(testData.german.self.id)
node("[0].disabledLanguages[1].id").isEqualTo(testData.czech.self.id)
}
}
}

@Test
@ProjectJWTAuthTestMethod
fun `set disabled languages`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,11 @@ class KeyLanguageDisablingTestData {
text = null
state = TranslationState.DISABLED
}

init {
project.addKey("notDisabledKey").addTranslation {
language = english.self
text = "text"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.tolgee.dtos.queryResults.keyDisabledLanguages

class KeyDisabledLanguagesQueryResultView(
val id: Long,
val name: String,
val namespace: String?,
val languageId: Long,
val languageTag: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.tolgee.dtos.queryResults.keyDisabledLanguages

class KeyDisabledLanguagesView(
val id: Long,
val name: String,
val namespace: String?,
val disabledLanguages: List<KeyDisabledLanguageModel>,
) {
class KeyDisabledLanguageModel(
val id: Long,
val tag: String,
)
}
16 changes: 16 additions & 0 deletions backend/data/src/main/kotlin/io/tolgee/repository/KeyRepository.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.tolgee.repository

import io.tolgee.dtos.queryResults.KeyView
import io.tolgee.dtos.queryResults.keyDisabledLanguages.KeyDisabledLanguagesQueryResultView
import io.tolgee.model.Language
import io.tolgee.model.key.Key
import io.tolgee.service.key.KeySearchResultView
Expand Down Expand Up @@ -215,6 +216,21 @@ interface KeyRepository : JpaRepository<Key, Long> {
keyId: Long,
): List<Language>

@Query(
"""
select new io.tolgee.dtos.queryResults.keyDisabledLanguages.KeyDisabledLanguagesQueryResultView(
k.id, k.name, ns.name, l.id, l.tag
)
from Key k
left join k.namespace ns
join k.translations t on t.state = io.tolgee.model.enums.TranslationState.DISABLED
join t.language l
where k.project.id = :projectId
order by k.id, l.id
""",
)
fun getDisabledLanguages(projectId: Long): List<KeyDisabledLanguagesQueryResultView>

fun findByProjectIdAndId(
projectId: Long,
keyId: Long,
Expand Down
18 changes: 18 additions & 0 deletions backend/data/src/main/kotlin/io/tolgee/service/key/KeyService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.tolgee.constants.Message
import io.tolgee.dtos.KeyImportResolvableResult
import io.tolgee.dtos.cacheable.ProjectDto
import io.tolgee.dtos.queryResults.KeyView
import io.tolgee.dtos.queryResults.keyDisabledLanguages.KeyDisabledLanguagesView
import io.tolgee.dtos.request.GetKeysRequestDto
import io.tolgee.dtos.request.key.CreateKeyDto
import io.tolgee.dtos.request.key.EditKeyDto
Expand Down Expand Up @@ -431,6 +432,23 @@ class KeyService(
return keyRepository.getDisabledLanguages(projectId, keyId)
}

@Transactional
fun getDisabledLanguages(projectId: Long): List<KeyDisabledLanguagesView> {
val queryResult = keyRepository.getDisabledLanguages(projectId)
return queryResult.groupBy {
it.id
}.map {
KeyDisabledLanguagesView(
it.key,
it.value.first().name,
it.value.first().namespace,
it.value.map { disabledLanguage ->
KeyDisabledLanguagesView.KeyDisabledLanguageModel(disabledLanguage.languageId, disabledLanguage.languageTag)
},
)
}
}

@Transactional
fun setDisabledLanguages(
projectId: Long,
Expand Down

0 comments on commit d48333f

Please sign in to comment.