Skip to content

Commit 668b8b2

Browse files
committed
cleanup
1 parent 4c63de2 commit 668b8b2

2 files changed

Lines changed: 19 additions & 30 deletions

File tree

taxonomy-api/src/main/kotlin/no/ndla/taxonomy/rest/v1/UrlResolver.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import no.ndla.taxonomy.service.UrlResolverService
2020
import no.ndla.taxonomy.service.dtos.ResolvedUrl
2121
import org.springframework.http.HttpStatus
2222
import org.springframework.security.access.prepost.PreAuthorize
23-
import org.springframework.transaction.annotation.Transactional
2423
import org.springframework.web.bind.annotation.GetMapping
2524
import org.springframework.web.bind.annotation.PutMapping
2625
import org.springframework.web.bind.annotation.RequestBody
@@ -34,7 +33,6 @@ import org.springframework.web.bind.annotation.RestController
3433
class UrlResolver(private val urlResolverService: UrlResolverService) {
3534

3635
@GetMapping("/resolve")
37-
@Transactional(readOnly = true)
3836
fun resolve(
3937
@RequestParam path: String,
4038
@RequestParam(value = "language", defaultValue = Constants.DefaultLanguage, required = false)
@@ -45,7 +43,6 @@ class UrlResolver(private val urlResolverService: UrlResolverService) {
4543

4644
@GetMapping("/mapping")
4745
@Operation(summary = "Returns paths for an url or HTTP 404")
48-
@Transactional(readOnly = true)
4946
fun getTaxonomyPathForUrl(
5047
@Parameter(
5148
description = "url in old rig except 'https://'",
@@ -64,15 +61,15 @@ class UrlResolver(private val urlResolverService: UrlResolverService) {
6461
)
6562
@ResponseStatus(HttpStatus.NO_CONTENT)
6663
@PreAuthorize("hasAuthority('TAXONOMY_WRITE')")
67-
@Transactional
68-
fun putTaxonomyNodeAndSubjectForOldUrl(@RequestBody urlMapping: UrlMapping): Unit =
69-
try {
70-
urlResolverService.putUrlMapping(
71-
urlMapping.url,
72-
URI.create(urlMapping.nodeId),
73-
if (urlMapping.subjectId != null) URI.create(urlMapping.subjectId) else null,
74-
)
75-
} catch (e: UrlResolverService.NodeIdNotFoundException) {
76-
throw NotFoundHttpResponseException(e.message)
77-
}
64+
fun putTaxonomyNodeAndSubjectForOldUrl(@RequestBody urlMapping: UrlMapping) {
65+
try {
66+
urlResolverService.putUrlMapping(
67+
urlMapping.url,
68+
URI.create(urlMapping.nodeId),
69+
urlMapping.subjectId?.let { URI.create(it) },
70+
)
71+
} catch (e: UrlResolverService.NodeIdNotFoundException) {
72+
throw NotFoundHttpResponseException(e.message)
73+
}
74+
}
7875
}

taxonomy-api/src/main/kotlin/no/ndla/taxonomy/service/UrlResolverService.kt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,13 @@ class UrlResolverService(
3636
* @return return a resolved URL or null
3737
*/
3838
fun resolveOldUrl(oldUrl: String): String? {
39-
val results = getCachedUrlOldRig(oldUrl)
40-
if (!results.isEmpty()) {
41-
val result = results.first()
42-
val subjectId = result.subjectId
43-
if (subjectId != null) {
44-
val allPaths = getAllPaths(result.publicId)
45-
val shortestPath = findShortestPathStartingWith(subjectId, allPaths)
46-
if (shortestPath != null) {
47-
return shortestPath
48-
}
39+
val result = getCachedUrlOldRig(oldUrl).firstOrNull() ?: return null
40+
result.subjectId?.let { subjectId ->
41+
findShortestPathStartingWith(subjectId, getAllPaths(result.publicId))?.let {
42+
return it
4943
}
50-
return getPrimaryPath(result.publicId)
51-
} else {
52-
return null
5344
}
45+
return getPrimaryPath(result.publicId)
5446
}
5547

5648
private fun findShortestPathStartingWith(subjectId: URI, allPaths: List<String>): String? =
@@ -80,8 +72,7 @@ class UrlResolverService(
8072
// url
8173
// e.g. oldUrl /node/54 should not match /node/54321 - therefore we add only if IDs
8274
// match
83-
val mappingOldUrl = mapping.oldUrl
84-
getNodeId(mappingOldUrl) == nodeId
75+
getNodeId(mapping.oldUrl) == nodeId
8576
}
8677
}
8778

@@ -102,6 +93,7 @@ class UrlResolverService(
10293
* @param subjectId subjectID to be associated with this URL (optional)
10394
* @throws NodeIdNotFoundException if node id not found in taxonomy
10495
*/
96+
@Transactional
10597
@Throws(NodeIdNotFoundException::class)
10698
fun putUrlMapping(oldUrl: String, nodeId: URI, subjectId: URI?) {
10799
val canonified = canonifier.canonify(oldUrl)
@@ -149,7 +141,7 @@ class UrlResolverService(
149141
ctx.nodeType,
150142
)
151143

152-
return ResolvedUrl(
144+
ResolvedUrl(
153145
exactMatch = context != null,
154146
contentUri = leafNode.contentUri,
155147
id = URI.create(ctx.publicId),

0 commit comments

Comments
 (0)