Skip to content

Commit

Permalink
Merge pull request #271 from NDLANO/proxy-endpoints
Browse files Browse the repository at this point in the history
Proxy old endpoints to nodes controller
  • Loading branch information
gunnarvelle authored Oct 15, 2024
2 parents 8d7f9de + a52336b commit 75740c8
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 396 deletions.
13 changes: 5 additions & 8 deletions src/main/java/no/ndla/taxonomy/rest/v1/NodeTranslations.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.persistence.EntityManager;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import no.ndla.taxonomy.domain.Node;
import no.ndla.taxonomy.domain.exceptions.NotFoundException;
import no.ndla.taxonomy.repositories.NodeRepository;
Expand All @@ -24,17 +20,18 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(path = {"/v1/nodes/{id}/translations", "/v1/nodes/{id}/translations/"})
public class NodeTranslations {

private final NodeRepository nodeRepository;

private final EntityManager entityManager;

public NodeTranslations(NodeRepository nodeRepository, EntityManager entityManager) {
public NodeTranslations(NodeRepository nodeRepository) {
this.nodeRepository = nodeRepository;
this.entityManager = entityManager;
}

@GetMapping
Expand Down
5 changes: 4 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 @@ -213,6 +213,7 @@ public SearchResultDTO<NodeDTO> getNodePage(
Optional<String> language,
@Parameter(name = "page", description = "The page to fetch") Optional<Integer> page,
@Parameter(name = "pageSize", description = "Size of page to fetch") Optional<Integer> pageSize,
@Parameter(name = "nodeType", description = "Filter by nodeType") Optional<NodeType> nodeType,
@Parameter(name = "includeContexts", description = "Include all contexts")
@RequestParam(value = "includeContexts", required = false, defaultValue = "true")
Optional<Boolean> includeContexts,
Expand All @@ -227,7 +228,9 @@ public SearchResultDTO<NodeDTO> getNodePage(
}
if (page.get() < 1) throw new IllegalArgumentException("page parameter must be bigger than 0");

var ids = nodeRepository.findIdsPaginated(PageRequest.of(page.get() - 1, pageSize.get()));
var ids = nodeType.map(
nt -> nodeRepository.findIdsByTypePaginated(PageRequest.of(page.get() - 1, pageSize.get()), nt))
.orElseGet(() -> nodeRepository.findIdsPaginated(PageRequest.of(page.get() - 1, pageSize.get())));
var results = nodeRepository.findByIds(ids.getContent());
var contents = results.stream()
.map(node -> new NodeDTO(
Expand Down
45 changes: 7 additions & 38 deletions src/main/java/no/ndla/taxonomy/rest/v1/ResourceTranslations.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.persistence.EntityManager;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import no.ndla.taxonomy.domain.exceptions.NotFoundException;
import no.ndla.taxonomy.repositories.NodeRepository;
import no.ndla.taxonomy.rest.v1.dtos.TranslationPUT;
import no.ndla.taxonomy.service.dtos.TranslationDTO;
import org.springframework.http.HttpStatus;
Expand All @@ -27,30 +23,18 @@
@RequestMapping(path = {"/v1/resources/{id}/translations", "/v1/resources/{id}/translations/"})
public class ResourceTranslations {

private final NodeRepository nodeRepository;
private final NodeTranslations nodeTranslations;

private final EntityManager entityManager;

public ResourceTranslations(NodeRepository resourceRepository, EntityManager entityManager) {
this.nodeRepository = resourceRepository;
this.entityManager = entityManager;
public ResourceTranslations(NodeTranslations nodeTranslations) {
this.nodeTranslations = nodeTranslations;
}

@Deprecated
@GetMapping
@Operation(summary = "Gets all relevanceTranslations for a single resource")
@Transactional(readOnly = true)
public List<TranslationDTO> getAllResourceTranslations(@PathVariable("id") URI id) {
var resource = nodeRepository.getByPublicId(id);
List<TranslationDTO> result = new ArrayList<>();
resource.getTranslations()
.forEach(t -> result.add(new TranslationDTO() {
{
name = t.getName();
language = t.getLanguageCode();
}
}));
return result;
return nodeTranslations.getAllNodeTranslations(id);
}

@Deprecated
Expand All @@ -62,16 +46,7 @@ public TranslationDTO getResourceTranslation(
@Parameter(description = "ISO-639-1 language code", example = "nb", required = true)
@PathVariable("language")
String language) {
var resource = nodeRepository.getByPublicId(id);
var translation = resource.getTranslation(language)
.orElseThrow(() ->
new NotFoundException("translation with language code " + language + " for resource", id));
return new TranslationDTO() {
{
name = translation.getName();
language = translation.getLanguageCode();
}
};
return nodeTranslations.getNodeTranslation(id, language);
}

@Deprecated
Expand All @@ -89,9 +64,7 @@ public void createUpdateResourceTranslation(
String language,
@Parameter(name = "resource", description = "The new or updated translation") @RequestBody
TranslationPUT command) {
var resource = nodeRepository.getByPublicId(id);
resource.addTranslation(command.name, language);
entityManager.persist(resource);
nodeTranslations.createUpdateNodeTranslation(id, language, command);
}

@Deprecated
Expand All @@ -107,10 +80,6 @@ public void deleteResourceTranslation(
@Parameter(description = "ISO-639-1 language code", example = "nb", required = true)
@PathVariable("language")
String language) {
final var resource = nodeRepository.getByPublicId(id);
resource.getTranslation(language).ifPresent((translation) -> {
resource.removeTranslation(language);
nodeRepository.save(resource);
});
nodeTranslations.deleteNodeTranslation(id, language);
}
}
79 changes: 28 additions & 51 deletions src/main/java/no/ndla/taxonomy/rest/v1/Resources.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import no.ndla.taxonomy.config.Constants;
import no.ndla.taxonomy.domain.Node;
import no.ndla.taxonomy.domain.NodeType;
import no.ndla.taxonomy.repositories.NodeRepository;
import no.ndla.taxonomy.repositories.ResourceResourceTypeRepository;
import no.ndla.taxonomy.rest.v1.commands.ResourcePostPut;
import no.ndla.taxonomy.service.*;
import no.ndla.taxonomy.service.ContextUpdaterService;
import no.ndla.taxonomy.service.NodeService;
import no.ndla.taxonomy.service.QualityEvaluationService;
import no.ndla.taxonomy.service.dtos.NodeDTO;
import no.ndla.taxonomy.service.dtos.NodeWithParents;
import no.ndla.taxonomy.service.dtos.ResourceTypeWithConnectionDTO;
import no.ndla.taxonomy.service.dtos.SearchResultDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -37,28 +37,26 @@
@RestController
@RequestMapping(path = {"/v1/resources", "/v1/resources/"})
public class Resources extends CrudControllerWithMetadata<Node> {
private final Nodes nodes;
private final ResourceResourceTypeRepository resourceResourceTypeRepository;
private final NodeService nodeService;
private final NodeRepository nodeRepository;
private final SearchService searchService;

@Value(value = "${new.url.separator:false}")
private boolean newUrlSeparator;

public Resources(
Nodes nodes,
NodeRepository nodeRepository,
ResourceResourceTypeRepository resourceResourceTypeRepository,
ContextUpdaterService contextUpdaterService,
NodeService nodeService,
QualityEvaluationService qualityEvaluationService,
SearchService searchService) {
QualityEvaluationService qualityEvaluationService) {
super(nodeRepository, contextUpdaterService, nodeService, qualityEvaluationService);

this.nodes = nodes;
this.resourceResourceTypeRepository = resourceResourceTypeRepository;
this.repository = nodeRepository;
this.nodeRepository = nodeRepository;
this.nodeService = nodeService;
this.searchService = searchService;
}

@Override
Expand All @@ -82,18 +80,19 @@ public List<NodeDTO> getAllResources(
Optional<String> value,
@Parameter(description = "Filter by visible") @RequestParam(value = "isVisible", required = false)
Optional<Boolean> isVisible) {
MetadataFilters metadataFilters = new MetadataFilters(key, value, isVisible);
return nodeService.getNodesByType(
return nodes.getAllNodes(
Optional.of(List.of(NodeType.RESOURCE)),
language.orElse(Constants.DefaultLanguage),
Optional.empty(),
language,
contentUri,
Optional.empty(),
Optional.empty(),
Optional.empty(),
metadataFilters,
Optional.of(false),
false,
key,
value,
isVisible,
Optional.empty(),
Optional.of(true),
true,
Optional.empty(),
Optional.empty());
}
Expand All @@ -117,17 +116,16 @@ public SearchResultDTO<NodeDTO> searchResources(
@Parameter(description = "ContentURIs to fetch for query")
@RequestParam(value = "contentUris", required = false)
Optional<List<String>> contentUris) {
return searchService.searchByNodeType(
query,
ids,
contentUris,
return nodes.searchNodes(
language,
Optional.of(false),
false,
pageSize,
page,
query,
ids,
contentUris,
Optional.of(List.of(NodeType.RESOURCE)),
Optional.empty(),
Optional.of(true),
true,
Optional.empty(),
Optional.empty());
}
Expand All @@ -142,27 +140,8 @@ public SearchResultDTO<NodeDTO> getResourcePage(
Optional<String> language,
@Parameter(name = "page", description = "The page to fetch") Optional<Integer> page,
@Parameter(name = "pageSize", description = "Size of page to fetch") Optional<Integer> pageSize) {
if (page.isEmpty() || pageSize.isEmpty()) {
throw new IllegalArgumentException("Need both page and pageSize to return data");
}
if (page.get() < 1) throw new IllegalArgumentException("page parameter must be bigger than 0");

var pageRequest = PageRequest.of(page.get() - 1, pageSize.get());
var ids = nodeRepository.findIdsByTypePaginated(pageRequest, NodeType.RESOURCE);
var results = nodeRepository.findByIds(ids.getContent());
var contents = results.stream()
.map(node -> new NodeDTO(
Optional.empty(),
Optional.empty(),
node,
language.orElse("nb"),
Optional.empty(),
Optional.of(false),
false,
false,
newUrlSeparator))
.collect(Collectors.toList());
return new SearchResultDTO<>(ids.getTotalElements(), page.get(), pageSize.get(), contents);
return nodes.getNodePage(
language, page, pageSize, Optional.of(NodeType.RESOURCE), Optional.of(true), true, true);
}

@Deprecated
Expand All @@ -174,7 +153,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.empty(), Optional.empty(), Optional.of(false), false, true);
return nodes.getNode(id, Optional.empty(), Optional.empty(), Optional.of(true), true, true, language);
}

@Deprecated
Expand All @@ -193,7 +172,7 @@ public void updateResource(
@RequestBody
@Schema(name = "ResourcePOST")
ResourcePostPut command) {
updateEntity(id, command);
nodes.updateEntity(id, command);
}

@Deprecated
Expand All @@ -206,7 +185,7 @@ public void updateResource(
public ResponseEntity<Void> createResource(
@Parameter(name = "resource", description = "the new resource") @RequestBody @Schema(name = "ResourcePUT")
ResourcePostPut command) {
return createEntity(new Node(NodeType.RESOURCE), command);
return nodes.createEntity(new Node(NodeType.RESOURCE), command);
}

@Deprecated
Expand Down Expand Up @@ -254,9 +233,7 @@ public NodeWithParents getResourceFull(
@Parameter(description = "ISO-639-1 language code", example = "nb")
@RequestParam(value = "language", required = false, defaultValue = Constants.DefaultLanguage)
Optional<String> language) {
var node = nodeService.getNode(id);
return new NodeWithParents(
node, language.orElse(Constants.DefaultLanguage), Optional.of(false), newUrlSeparator);
return nodes.getNodeFull(id, language, Optional.of(true));
}

@Deprecated
Expand All @@ -268,6 +245,6 @@ public NodeWithParents getResourceFull(
@ResponseStatus(HttpStatus.NO_CONTENT)
@Transactional
public void deleteEntity(@PathVariable("id") URI id) {
nodeService.delete(id);
nodes.deleteEntity(id);
}
}
Loading

0 comments on commit 75740c8

Please sign in to comment.