View Javadoc
1   /*
2    * Copyright (C) 2021 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.repository;
7   
8   import java.util.Optional;
9   import org.springframework.data.jpa.repository.JpaRepository;
10  import org.springframework.data.jpa.repository.Query;
11  import org.springframework.data.repository.query.Param;
12  import org.springframework.data.rest.core.annotation.RepositoryRestResource;
13  import org.springframework.security.access.prepost.PreAuthorize;
14  import org.tailormap.api.persistence.Configuration;
15  import org.tailormap.api.security.annotation.PreAuthorizeAdmin;
16  
17  @PreAuthorizeAdmin
18  @RepositoryRestResource(path = "configs", collectionResourceRel = "configs", itemResourceRel = "config")
19  public interface ConfigurationRepository extends JpaRepository<Configuration, String> {
20    @PreAuthorize("permitAll()")
21    Optional<Configuration> findByKey(String key);
22  
23    @PreAuthorize("permitAll()")
24    default String get(String key) {
25      return get(key, null);
26    }
27  
28    @PreAuthorize("permitAll()")
29    default String get(String key, String defaultValue) {
30      return findByKey(key).map(Configuration::getValue).orElse(defaultValue);
31    }
32  
33    @PreAuthorize("permitAll()")
34    @Query("from Configuration c where c.key = :key and c.availableForViewer = true")
35    Optional<Configuration> getAvailableForViewer(@Param("key") String key);
36  
37    void deleteConfigurationByKey(String configKey);
38  }