View Javadoc
1   /*
2    * Copyright (C) 2023 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   
7   package org.tailormap.api.repository;
8   
9   import java.util.List;
10  import java.util.Optional;
11  import org.springframework.data.jpa.repository.JpaRepository;
12  import org.springframework.data.jpa.repository.Query;
13  import org.springframework.data.repository.history.RevisionRepository;
14  import org.springframework.data.repository.query.Param;
15  import org.springframework.data.rest.core.annotation.RepositoryRestResource;
16  import org.springframework.lang.NonNull;
17  import org.springframework.security.access.prepost.PreAuthorize;
18  import org.tailormap.api.persistence.TMFeatureSource;
19  import org.tailormap.api.security.annotation.PreAuthorizeAdmin;
20  
21  @PreAuthorizeAdmin
22  @RepositoryRestResource(
23      path = "feature-sources",
24      collectionResourceRel = "feature-sources",
25      itemResourceRel = "feature-source")
26  public interface FeatureSourceRepository
27      extends JpaRepository<TMFeatureSource, Long>, RevisionRepository<TMFeatureSource, Long, Long> {
28    TMFeatureSource findByUrl(String url);
29  
30    @Override
31    @PreAuthorize(value = "permitAll()")
32    @NonNull Optional<TMFeatureSource> findById(@NonNull Long id);
33  
34    @PreAuthorize(value = "permitAll()")
35    List<TMFeatureSource> findByLinkedServiceId(String id);
36  
37    @NonNull @PreAuthorize("permitAll()")
38    @Query("from TMFeatureSource fs where id in :ids")
39    List<TMFeatureSource> findByIds(@Param("ids") List<Long> ids);
40  
41    /**
42     * Find multiple feature-sources except some. Example URL:
43     * /api/admin/feature-sources/search/getAllExcludingIds?ids=1,2,3
44     *
45     * <p>No feature sources are returned if ids is an empty list.
46     *
47     * @param ids The ids not to include
48     * @return All feature sources except those matching the ids
49     */
50    @NonNull @PreAuthorize("permitAll()")
51    @Query("from TMFeatureSource fs where id not in :ids")
52    List<TMFeatureSource> getAllExcludingIds(@Param("ids") List<Long> ids);
53  
54    /**
55     * Find a feature-source by title. This is a non-deterministic operation since the title is not unique. Useful for
56     * testing.
57     *
58     * @param title The title of the feature-source
59     * @return The feature-source
60     */
61    @PreAuthorize(value = "permitAll()")
62    Optional<TMFeatureSource> getByTitle(String title);
63  }