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.query.Param;
14  import org.springframework.data.rest.core.annotation.RepositoryRestResource;
15  import org.springframework.lang.NonNull;
16  import org.springframework.security.access.prepost.PreAuthorize;
17  import org.tailormap.api.persistence.TMFeatureSource;
18  import org.tailormap.api.security.annotation.PreAuthorizeAdmin;
19  
20  @PreAuthorizeAdmin
21  @RepositoryRestResource(
22      path = "feature-sources",
23      collectionResourceRel = "feature-sources",
24      itemResourceRel = "feature-source")
25  public interface FeatureSourceRepository extends JpaRepository<TMFeatureSource, Long> {
26    TMFeatureSource findByUrl(String url);
27  
28    @Override
29    @PreAuthorize(value = "permitAll()")
30    @NonNull
31    Optional<TMFeatureSource> findById(@NonNull Long id);
32  
33    @PreAuthorize(value = "permitAll()")
34    List<TMFeatureSource> findByLinkedServiceId(String id);
35  
36    @NonNull
37    @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
51    @PreAuthorize("permitAll()")
52    @Query("from TMFeatureSource fs where id not in :ids")
53    List<TMFeatureSource> getAllExcludingIds(@Param("ids") List<Long> ids);
54  
55    /**
56     * Find a feature-source by title. This is a non-deterministic operation since the title is not
57     * unique. Useful for testing.
58     *
59     * @param title The title of the feature-source
60     * @return The feature-source
61     */
62    @PreAuthorize(value = "permitAll()")
63    Optional<TMFeatureSource> getByTitle(String title);
64  }