Skip to content

Commit

Permalink
Add context object for selected context. Add more params to fetching …
Browse files Browse the repository at this point in the history
…single node.
  • Loading branch information
gunnarvelle committed Aug 9, 2024
1 parent 3047249 commit 9816ec5
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 50 deletions.
8 changes: 7 additions & 1 deletion src/main/java/no/ndla/taxonomy/rest/v1/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,16 @@ public NodeDTO getNode(
Optional<URI> rootId,
@Parameter(description = "Id to parent id in context.") @RequestParam(value = "parentId", required = false)
Optional<URI> parentId,
@Parameter(name = "includeContexts", description = "Include all contexts")
@RequestParam(value = "includeContexts", required = false, defaultValue = "true")
Optional<Boolean> includeContexts,
@Parameter(description = "Filter out programme contexts")
@RequestParam(value = "filterProgrammes", required = false, defaultValue = "false")
boolean filterProgrammes,
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(true), rootId, parentId);
return nodeService.getNode(id, language, rootId, parentId, includeContexts, filterProgrammes);
}

@PostMapping
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/ndla/taxonomy/rest/v1/Resources.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public NodeDTO getResource(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(false), Optional.empty(), Optional.empty());
return nodeService.getNode(id, language, Optional.empty(), Optional.empty(), Optional.of(false), false);
}

@Deprecated
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/ndla/taxonomy/rest/v1/Subjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public NodeDTO getSubject(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(false), Optional.empty(), Optional.empty());
return nodeService.getNode(id, language, Optional.empty(), Optional.empty(), Optional.of(false), false);
}

@Deprecated
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/ndla/taxonomy/rest/v1/Topics.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public NodeDTO getTopic(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
return nodeService.getNode(id, language, Optional.of(false), Optional.empty(), Optional.empty());
return nodeService.getNode(id, language, Optional.empty(), Optional.empty(), Optional.of(false), false);
}

@Deprecated
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/no/ndla/taxonomy/service/NodeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ public List<NodeChildDTO> getFilteredChildConnections(URI nodePublicId, String l
public NodeDTO getNode(
URI publicId,
Optional<String> language,
Optional<Boolean> includeContexts,
Optional<URI> rootId,
Optional<URI> parentId) {
Optional<URI> parentId,
Optional<Boolean> includeContexts,
boolean filterProgrammes) {
var node = getNode(publicId);
var root = rootId.map(this::getNode);
var parent = parentId.map(this::getNode);
Expand All @@ -182,7 +183,7 @@ public NodeDTO getNode(
language.orElse(Constants.DefaultLanguage),
Optional.empty(),
includeContexts,
false,
filterProgrammes,
newUrlSeparator);
}

Expand Down
85 changes: 46 additions & 39 deletions src/main/java/no/ndla/taxonomy/service/dtos/NodeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public class NodeDTO {
@Schema(description = "A list of all contexts this node is part of")
private List<TaxonomyContextDTO> contexts = new ArrayList<>();

@JsonProperty
@Schema(description = "The context object selected when fetching node")
private Optional<TaxonomyContextDTO> context = Optional.empty();

@Schema(description = "The language code for which name is returned", example = "nb")
private String language;

Expand Down Expand Up @@ -112,6 +116,7 @@ public NodeDTO(

Optional<Relevance> relevance =
entity.getParentConnections().stream().findFirst().flatMap(NodeConnection::getRelevance);
var relevanceName = relevance.map(LanguageField::fromNode).orElse(new LanguageField<String>());
this.relevanceId = relevance.map(Relevance::getPublicId);

this.translations = entity.getTranslations().stream()
Expand All @@ -138,8 +143,8 @@ public NodeDTO(

this.nodeType = entity.getNodeType();

Optional<TaxonomyContext> context = entity.pickContext(contextId, parent, root);
context.ifPresent(ctx -> {
Optional<TaxonomyContext> selected = entity.pickContext(contextId, parent, root);
selected.ifPresent(ctx -> {
this.path = ctx.path();
// TODO: this changes the content in context breadcrumbs
LanguageField<List<String>> breadcrumbList =
Expand All @@ -156,52 +161,54 @@ public NodeDTO(
ctx.contextId(),
entity.getNodeType(),
newUrlSeparator);

this.context = Optional.of(getTaxonomyContextDTO(entity, newUrlSeparator, ctx, relevanceName));
});

includeContexts.filter(Boolean::booleanValue).ifPresent(includeCtx -> {
var relevanceName = new LanguageField<String>();
if (relevance.isPresent()) {
relevanceName = LanguageField.fromNode(relevance.get());
}
LanguageField<String> finalRelevanceName = relevanceName;
this.contexts = entity.getContexts().stream()
.filter(ctx -> !filterProgrammes || !ctx.rootId().contains(NodeType.PROGRAMME.getName()))
.map(ctx -> new TaxonomyContextDTO(
entity.getPublicId(),
URI.create(ctx.rootId()),
LanguageFieldDTO.fromLanguageField(ctx.rootName()),
ctx.path(),
LanguageFieldDTO.fromLanguageFieldList(ctx.breadcrumbs()),
entity.getContextType(),
URI.create(ctx.relevanceId()),
LanguageFieldDTO.fromLanguageField(finalRelevanceName),
entity.getResourceTypes().stream()
.sorted((o1, o2) -> {
if (o1.getParent().isEmpty()) return -1;
if (o2.getParent().isEmpty()) return 1;
return 0;
})
.map(SearchableTaxonomyResourceType::new)
.toList(),
ctx.parentIds().stream().map(URI::create).toList(),
ctx.parentContextIds(),
ctx.isPrimary(),
ctx.isActive(),
ctx.isVisible(),
ctx.contextId(),
ctx.rank(),
ctx.connectionId(),
PrettyUrlUtil.createPrettyUrl(
Optional.of(ctx.rootName()),
LanguageField.fromNode(entity),
this.language,
ctx.contextId(),
entity.getNodeType(),
newUrlSeparator)))
.map(ctx -> getTaxonomyContextDTO(entity, newUrlSeparator, ctx, relevanceName))
.toList();
});
}

private TaxonomyContextDTO getTaxonomyContextDTO(
Node entity, boolean newUrlSeparator, TaxonomyContext ctx, LanguageField<String> finalRelevanceName) {
return new TaxonomyContextDTO(
entity.getPublicId(),
URI.create(ctx.rootId()),
LanguageFieldDTO.fromLanguageField(ctx.rootName()),
ctx.path(),
LanguageFieldDTO.fromLanguageFieldList(ctx.breadcrumbs()),
entity.getContextType(),
URI.create(ctx.relevanceId()),
LanguageFieldDTO.fromLanguageField(finalRelevanceName),
entity.getResourceTypes().stream()
.sorted((o1, o2) -> {
if (o1.getParent().isEmpty()) return -1;
if (o2.getParent().isEmpty()) return 1;
return 0;
})
.map(SearchableTaxonomyResourceType::new)
.toList(),
ctx.parentIds().stream().map(URI::create).toList(),
ctx.parentContextIds(),
ctx.isPrimary(),
ctx.isActive(),
ctx.isVisible(),
ctx.contextId(),
ctx.rank(),
ctx.connectionId(),
PrettyUrlUtil.createPrettyUrl(
Optional.of(ctx.rootName()),
LanguageField.fromNode(entity),
this.language,
ctx.contextId(),
entity.getNodeType(),
newUrlSeparator));
}

public URI getId() {
return id;
}
Expand Down
8 changes: 4 additions & 4 deletions typescript/taxonomy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface Node {
baseName: string;
breadcrumbs: string[];
contentUri?: string;
/**
* The context object selected when fetching node
*/
context?: TaxonomyContext;
/**
* An id unique for this context.
*/
Expand Down Expand Up @@ -65,10 +69,6 @@ export interface Node {
export interface NodeChild extends Node {
connectionId: string;
isPrimary: boolean;
/**
* @deprecated
*/
parent: string;
parentId: string;
rank: number;
}
Expand Down

0 comments on commit 9816ec5

Please sign in to comment.