View Javadoc
1   /*
2    * Copyright (C) 2024 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   
7   package org.tailormap.api.repository;
8   
9   import java.time.OffsetDateTime;
10  import java.util.List;
11  import java.util.Optional;
12  import java.util.UUID;
13  import org.jspecify.annotations.NonNull;
14  import org.springframework.data.jpa.repository.EntityGraph;
15  import org.springframework.data.jpa.repository.JpaRepository;
16  import org.springframework.data.jpa.repository.Query;
17  import org.springframework.data.repository.history.RevisionRepository;
18  import org.springframework.security.access.prepost.PreAuthorize;
19  import org.tailormap.api.persistence.Upload;
20  
21  public interface UploadRepository extends JpaRepository<Upload, UUID>, RevisionRepository<Upload, UUID, Long> {
22    @PreAuthorize("permitAll()")
23    @NonNull @Query("select lastModified from Upload where id = :id")
24    Optional<OffsetDateTime> findLastModifiedById(@NonNull UUID id);
25  
26    @PreAuthorize("permitAll()")
27    @NonNull Optional<Upload> findByIdAndCategory(@NonNull UUID id, @NonNull String category);
28  
29    @PreAuthorize("permitAll()")
30    @NonNull @EntityGraph(attributePaths = {"content"})
31    Optional<Upload> findWithContentByCategoryAndFilename(@NonNull String category, @NonNull String filename);
32  
33    @PreAuthorize("permitAll()")
34    @NonNull @EntityGraph(attributePaths = {"content"})
35    Optional<Upload> findWithContentByIdAndCategory(@NonNull UUID id, @NonNull String category);
36  
37    @PreAuthorize(value = "permitAll()")
38    List<Upload> findByCategory(String category);
39  
40    @PreAuthorize("permitAll()")
41    @NonNull @EntityGraph(attributePaths = {"content"})
42    // Find the most recent upload for a specific category with its content
43    Optional<Upload> findFirstWithContentByCategoryOrderByLastModifiedDesc(@NonNull String category);
44  
45    @PreAuthorize("permitAll()")
46    @Query(
47        "select new org.tailormap.api.repository.UploadMatch(u.id, u.hash) from Upload u where u.category = :category and u.hash in :hashes")
48    List<UploadMatch> findByHashIn(@NonNull String category, @NonNull List<String> hashes);
49  
50    @PreAuthorize("permitAll()")
51    List<Upload> findByFilename(String filename);
52  }