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  import org.tailormap.api.persistence.UploadCategory;
21  
22  public interface UploadRepository extends JpaRepository<Upload, UUID>, RevisionRepository<Upload, UUID, Long> {
23    @PreAuthorize("permitAll()")
24    @NonNull @Query("select lastModified from Upload where id = :id")
25    Optional<OffsetDateTime> findLastModifiedById(@NonNull UUID id);
26  
27    @PreAuthorize("permitAll()")
28    @NonNull Optional<Upload> findByIdAndCategory(@NonNull UUID id, @NonNull UploadCategory category);
29  
30    @PreAuthorize("permitAll()")
31    @NonNull @EntityGraph(attributePaths = {"content"})
32    Optional<Upload> findWithContentByCategoryAndFilename(@NonNull UploadCategory category, @NonNull String filename);
33  
34    @PreAuthorize("permitAll()")
35    @NonNull @EntityGraph(attributePaths = {"content"})
36    Optional<Upload> findWithContentByIdAndCategory(@NonNull UUID id, @NonNull UploadCategory category);
37  
38    @PreAuthorize(value = "permitAll()")
39    List<Upload> findByCategory(@NonNull UploadCategory category);
40  
41    @PreAuthorize("permitAll()")
42    @NonNull @EntityGraph(attributePaths = {"content"})
43    // Find the most recent upload for a specific category with its content
44    Optional<Upload> findFirstWithContentByCategoryOrderByLastModifiedDesc(@NonNull UploadCategory category);
45  
46    @PreAuthorize("permitAll()")
47    @Query(
48        "select new org.tailormap.api.repository.UploadMatch(u.id, u.hash) from Upload u where u.category = :category and u.hash in :hashes")
49    List<UploadMatch> findByHashIn(@NonNull UploadCategory category, @NonNull List<String> hashes);
50  
51    @PreAuthorize("permitAll()")
52    List<Upload> findByFilename(String filename);
53  
54    /** get all uploads with the given ids, including their content, e.g. for downloading as a zip. */
55    @PreAuthorize("permitAll()")
56    @NonNull @EntityGraph(attributePaths = {"content"})
57    List<Upload> findAllWithContentByIdIn(@NonNull List<UUID> ids);
58  }