Add method to fetch all photo paths and optimize database population logic

This commit is contained in:
Kiyan 2025-10-04 19:06:25 +02:00
parent 3e67226b67
commit e84f597f18
3 changed files with 12 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package com.example.PhotoGallery.Repos;
import java.util.List;
import java.util.Set;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
@ -15,4 +16,7 @@ public interface PhotoRepo extends JpaRepository<Photo, Long> {
@Query("SELECT p.thumbnail_path FROM Photo p")
List<String> findAllThumbnailPaths();
@Query("SELECT p.path FROM Photo p")
Set<String> findAllPaths();
}

View File

@ -6,8 +6,10 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.Collectors;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
@ -40,10 +42,14 @@ public class DatabasePopulation {
private String originalImgPath;
public void populateDatabase() {
//Fetch all existing photo paths from the DB in one query.
Set<String> existingPaths = photoRepo.findAllPaths();
for (String path : getImagePaths()) {
//Get all image paths from the file system.
List<String> allImagePaths = getImagePaths();
if (!photoRepo.existsByPath(path)) {
for (String path : allImagePaths) {
if (!existingPaths.contains(path)) {
if (!imageCompressor.compressImage(path, compressedImgPath)) {
continue;

View File

@ -4,7 +4,6 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import com.example.PhotoGallery.Models.Photo;
import com.example.PhotoGallery.Repos.PhotoRepo;