Skip to content

Commit

Permalink
Merge branch 'master' into expanded-url
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarvelle committed Apr 24, 2024
2 parents 78f2ace + b9a7544 commit f773576
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
41 changes: 23 additions & 18 deletions src/main/java/no/ndla/taxonomy/rest/v1/Contexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import no.ndla.taxonomy.config.Constants;
import no.ndla.taxonomy.domain.Node;
import no.ndla.taxonomy.repositories.NodeRepository;
import no.ndla.taxonomy.rest.v1.dtos.ContextDTO;
Expand All @@ -38,19 +39,19 @@ public Contexts(NodeRepository nodeRepository, ContextUpdaterService contextUpda
}

@GetMapping
@Operation(summary = "Gets all contexts")
@Operation(summary = "Gets id of all nodes registered as context")
public List<ContextDTO> getAllContexts(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = "")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
String language) {

final var nodes = nodeRepository.findAllByContextIncludingCachedUrlsAndTranslations(true);

final var contextDocuments = new ArrayList<>(nodes.stream()
.map(topic -> new ContextDTO(
topic.getPublicId(),
topic.getTranslatedName(language),
topic.getPrimaryPath().orElse(null)))
.map(node -> new ContextDTO(
node.getPublicId(),
node.getTranslatedName(language),
node.getPrimaryPath().orElse(null)))
.toList());

contextDocuments.sort(Comparator.comparing(ContextDTO::getId));
Expand All @@ -60,35 +61,39 @@ public List<ContextDTO> getAllContexts(

@PostMapping
@Operation(
summary = "Adds a new context",
summary = "Registers a new node as context",
description =
"All subjects are already contexts and may not be added again. Only topics may be added as a context. The topic must exist already.",
"All subjects are already contexts and may not be added again. The node to register as context must exist already.",
security = {@SecurityRequirement(name = "oauth")})
@PreAuthorize("hasAuthority('TAXONOMY_WRITE')")
@Transactional
public ResponseEntity<Void> createContext(
@Parameter(name = "context", description = "the new context") @RequestBody ContextPOST command) {
Node topic = nodeRepository.getByPublicId(command.id);
topic.setContext(true);
URI location = URI.create("/v1/contexts/" + topic.getPublicId());
@Parameter(
name = "context",
description = "object containing public id of the node to be registered as context")
@RequestBody
ContextPOST command) {
Node node = nodeRepository.getByPublicId(command.id);
node.setContext(true);
URI location = URI.create("/v1/contexts/" + node.getPublicId());

contextUpdaterService.updateContexts(topic);
contextUpdaterService.updateContexts(node);

return ResponseEntity.created(location).build();
}

@DeleteMapping("/{id}")
@Operation(
summary = "Removes a context",
description = "Does not remove the underlying resource, only marks it as not being a context",
summary = "Removes context registration from node",
description = "Does not remove the underlying node, only marks it as not being a context",
security = {@SecurityRequirement(name = "oauth")})
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize("hasAuthority('TAXONOMY_WRITE')")
@Transactional
public void deleteContext(@PathVariable("id") URI id) {
Node topic = nodeRepository.getByPublicId(id);
topic.setContext(false);
Node node = nodeRepository.getByPublicId(id);
node.setContext(false);

contextUpdaterService.updateContexts(topic);
contextUpdaterService.updateContexts(node);
}
}
13 changes: 12 additions & 1 deletion src/main/java/no/ndla/taxonomy/rest/v1/Queries.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Queries(Topics topicController, Resources resourceController, NodeService
@GetMapping("/{contentURI}")
@Operation(summary = "Gets a list of contexts matching given contentURI, empty list if no matches are found.")
@Transactional(readOnly = true)
public List<TaxonomyContextDTO> queryFullNode(
public List<TaxonomyContextDTO> contextByContentURI(
@PathVariable("contentURI") Optional<URI> contentURI,
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", defaultValue = Constants.DefaultLanguage, required = false)
Expand All @@ -47,6 +47,17 @@ public List<TaxonomyContextDTO> queryFullNode(
contentURI, filterVisibles, language.orElse(Constants.DefaultLanguage));
}

@GetMapping("/contextId")
@Operation(summary = "Gets a list of contexts matching given contextId, empty list if no matches are found.")
@Transactional(readOnly = true)
public List<TaxonomyContextDTO> contextByContextId(
@RequestParam("contextId") Optional<String> contextId,
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", defaultValue = Constants.DefaultLanguage, required = false)
Optional<String> language) {
return nodeService.getContextByContextId(contextId, language.orElse(Constants.DefaultLanguage));
}

@GetMapping("/path")
@Operation(
summary =
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public List<TaxonomyContextDTO> getSearchableByContentUri(
.toList();
}

List<TaxonomyContextDTO> nodesToContexts(List<Node> nodes, boolean filterVisibles, String language) {
public List<TaxonomyContextDTO> nodesToContexts(List<Node> nodes, boolean filterVisibles, String language) {
return nodes.stream()
.flatMap(node -> {
var contexts = filterVisibles
Expand Down Expand Up @@ -519,15 +519,19 @@ private List<Node> buildAllContexts() {
}

public List<TaxonomyContextDTO> getContextByPath(Optional<String> path, String language) {
if (path.isPresent()) {
String contextId = PrettyUrlUtil.getHashFromPath(path.get());
List<Integer> ids = nodeRepository.findIdsByContextId(Optional.of(contextId));
var nodes = nodeRepository.findByIds(ids);
var contexts = nodesToContexts(nodes, false, language);
return contexts.stream()
.filter(c -> c.contextId().equals(contextId))
.toList();
return path.map(p -> getContextByContextId(Optional.of(PrettyUrlUtil.getHashFromPath(p)), language))
.orElse(List.of());
}

public List<TaxonomyContextDTO> getContextByContextId(Optional<String> contextId, String language) {
if (contextId.isEmpty()) {
return List.of();
}
return List.of();
List<Integer> ids = nodeRepository.findIdsByContextId(contextId);
var nodes = nodeRepository.findByIds(ids);
var contexts = nodesToContexts(nodes, false, language);
return contexts.stream()
.filter(c -> c.contextId().equals(contextId.get()))
.toList();
}
}

0 comments on commit f773576

Please sign in to comment.