Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter Param #77

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/main/java/org/jacksonlaboratory/controller/TermController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.jacksonlaboratory.controller;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Pattern;
import org.jacksonlaboratory.model.dto.SimpleOntologyTerm;
import org.jacksonlaboratory.model.entity.OntologyTerm;
import org.jacksonlaboratory.service.GraphService;
import org.jacksonlaboratory.service.TermService;
import org.monarchinitiative.phenol.ontology.data.TermId;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Controller("${api-url.prefix}/${ontology}/terms")
public class TermController {
Expand All @@ -28,8 +33,18 @@ public TermController(TermService termService, GraphService graphService) {
* @return List of all ontology terms
*/
@Get(uri="/", produces="application/json")
public List<OntologyTerm> all() {
return this.termService.getAllOntologyTerms();
public List<OntologyTerm> all(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test hitting this endpoint?

@QueryValue(value = "filter")
@Nullable
@Parameter(schema = @Schema(maxLength = 250, type = "string", pattern = "^(HP:\\d{7})(,HP:\\d{7})*$"), required = false)
@Pattern(regexp = "^(HP:\\d{7})(,HP:\\d{7})*$", message = "message = Invalid format. Expected format: HP:0001166 or HP:0001166,HP:0001234") String filter) {

if (filter == null || filter.isBlank()){
return this.termService.getAllOntologyTerms(List.of());
} else {
List<TermId> termIds = Arrays.stream(filter.split(",")).map(TermId::of).collect(Collectors.toUnmodifiableList());
return this.termService.getAllOntologyTerms(termIds);
}
}

/**
Expand All @@ -38,7 +53,8 @@ public List<OntologyTerm> all() {
* @return The term or null.
*/
@Get(uri="/{id}", produces="application/json")
public HttpResponse<?> details(@Schema(minLength = 1, maxLength = 20, type = "string", pattern = ".*") @PathVariable String id) {
public HttpResponse<?> details(@Schema(minLength = 1, maxLength = 20, type = "string", pattern = ".*")
@PathVariable String id) {
TermId termId = TermId.of(id);
Optional<TermId> replacement = this.graphService.getMostRecentTermId(termId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,21 @@ public TermRepositoryImpl(EntityManager entityManager) {

@Override
@ReadOnly
@Transactional
public List<OntologyTerm> findAll(){
return entityManager.createQuery("SELECT t FROM OntologyTerm t", OntologyTerm.class).getResultStream().collect(Collectors.toList());
}

@Override
@ReadOnly
@Transactional
public Optional<OntologyTerm> findByTermId(TermId id) {
Optional<OntologyTerm> term = entityManager.createQuery("SELECT t FROM OntologyTerm t WHERE t.id = :param1", OntologyTerm.class).setParameter("param1", id).getResultStream().findFirst();
return term;
}

@Override
@Transactional
public List<OntologyTerm> findByTermIdIn(List<TermId> ids) {
List<OntologyTerm> terms = entityManager.createQuery("SELECT t FROM OntologyTerm t WHERE t.id in :param1", OntologyTerm.class).setParameter("param1", ids).getResultList();
terms.sort(Comparator.comparing(item -> ids.indexOf(TermId.of(item.getId()))));
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/jacksonlaboratory/service/GraphService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import org.jacksonlaboratory.model.entity.OntologyTermBuilder;
import org.jacksonlaboratory.repository.TermRepository;
import org.jacksonlaboratory.repository.TranslationRepository;
import org.monarchinitiative.phenol.io.MinimalOntologyLoader;
import org.monarchinitiative.phenol.io.OntologyLoader;
import org.monarchinitiative.phenol.io.OntologyLoaderOptions;
import org.monarchinitiative.phenol.io.utils.CurieUtilBuilder;
import org.monarchinitiative.phenol.ontology.data.Identified;
import org.monarchinitiative.phenol.ontology.data.MinimalOntology;
import org.monarchinitiative.phenol.ontology.data.Term;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/jacksonlaboratory/service/TermService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public Optional<OntologyTerm> getOntologyTermByTermId(TermId id){
return Optional.empty();
}

public List<OntologyTerm> getAllOntologyTerms(){
return this.termRepository.findAll();
public List<OntologyTerm> getAllOntologyTerms(List<TermId> termIdList){
if (termIdList.isEmpty()){
return this.termRepository.findAll();
}
return this.termRepository.findByTermIdIn(termIdList);
}

public List<OntologyTerm> searchOntologyTerm(String q){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TermControllerSpec extends Specification {
when:
def response = client.toBlocking().exchange(HttpRequest.GET('/api/hp/terms/'), Argument.listOf(Map.class))
then:
1 * termService.getAllOntologyTerms() >> res
1 * termService.getAllOntologyTerms(List.of()) >> res
response.body().size() == res.size()
response.status().getCode().toInteger() == 200
where:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TranslationRepositorySpec extends Specification {

void "test find all by term"() {
when:
def term = termService.getAllOntologyTerms().find {it -> it.id == "HP:0000002"}.collect()
def term = termService.getAllOntologyTerms([]).find {it -> it.id == "HP:0000002"}.collect()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Groovy? Yew

def res = translationRepository.findAllByTerm(term[0])
then:
res.size() == 1
Expand Down
Loading