added a search bar to the page with all of the files

This commit is contained in:
Rostislav Raykov
2024-10-17 21:13:42 +03:00
parent 67bd07d284
commit 9542776b79
4 changed files with 27 additions and 1 deletions
@@ -97,4 +97,11 @@ public class FileViewController {
return "redirect:/file/" + id;
}
}
@GetMapping("/search")
public String searchFiles(String query, Model model) {
List<FileEntity> files = fileService.searchFiles(query);
model.addAttribute("files", files);
return "listFiles";
}
}
@@ -13,7 +13,9 @@ public interface FileRepository extends JpaRepository<FileEntity, Long> {
@Query("SELECT f FROM FileEntity f WHERE f.uuid = :uuid")
Optional<FileEntity> findByUUID(@Param("uuid") String uuid);
//Get files that are not marked to be kept indefinitely and were uploaded more than maxFileAge days ago
@Query("SELECT f FROM FileEntity f WHERE f.keepIndefinitely = false AND f.uploadDate < :thresholdDate")
List<FileEntity> getFilesForDeletion(@Param("thresholdDate") LocalDate thresholdDate);
@Query("SELECT f FROM FileEntity f WHERE f.name LIKE %:searchString% OR f.description LIKE %:searchString% OR f.uuid LIKE %:searchString%")
List<FileEntity> searchFiles(@Param("searchString") String searchString);
}
@@ -201,4 +201,8 @@ public class FileService {
FileEntity fileEntity = referenceByUUID.get();
return passwordEncoder.matches(password, fileEntity.passwordHash);
}
public List<FileEntity> searchFiles(String query) {
return fileRepository.searchFiles(query);
}
}
@@ -39,6 +39,19 @@
<!-- Main Content -->
<div class="container mt-5">
<h1 class="text-center mb-4">All Files</h1>
<!-- Search Bar Section -->
<div class="row mb-4">
<div class="col-12 col-md-8 offset-md-2">
<form action="/file/search" method="GET">
<div class="input-group">
<input aria-describedby="search-button" aria-label="Search for files" class="form-control" name="query"
placeholder="Search for files..." type="text">
<button class="btn btn-primary" id="search-button" type="submit">Search</button>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-6 col-md-4 col-lg-3 mb-4" th:each="file : ${files}">
<div class="card h-100 shadow">