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 Optional<TMFeatureSource> findById(@NonNull Long id);
31  
32    @PreAuthorize(value = "permitAll()")
33    List<TMFeatureSource> findByLinkedServiceId(String id);
34  
35    @NonNull @PreAuthorize("permitAll()")
36    @Query("from TMFeatureSource fs where id in :ids")
37    List<TMFeatureSource> findByIds(@Param("ids") List<Long> ids);
38  
39    /**
40     * Find multiple feature-sources except some. Example URL:
41     * /api/admin/feature-sources/search/getAllExcludingIds?ids=1,2,3
42     *
43     * <p>No feature sources are returned if ids is an empty list.
44     *
45     * @param ids The ids not to include
46     * @return All feature sources except those matching the ids
47     */
48    @NonNull @PreAuthorize("permitAll()")
49    @Query("from TMFeatureSource fs where id not in :ids")
50    List<TMFeatureSource> getAllExcludingIds(@Param("ids") List<Long> ids);
51  
52    /**
53     * Find a feature-source by title. This is a non-deterministic operation since the title is not unique. Useful for
54     * testing.
55     *
56     * @param title The title of the feature-source
57     * @return The feature-source
58     */
59    @PreAuthorize(value = "permitAll()")
60    Optional<TMFeatureSource> getByTitle(String title);
61  }