package de.fred4jupiter.fredbet.repository;

import de.fred4jupiter.fredbet.domain.AppUser;
import de.fred4jupiter.fredbet.domain.ImageGroup;
import de.fred4jupiter.fredbet.domain.ImageMetaData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface ImageMetaDataRepository extends JpaRepository<ImageMetaData, Long> {

    @Query("select a from ImageMetaData a where a.owner.username = :username and a.imageGroup.userProfileImageGroup = true")
    ImageMetaData findImageMetaDataOfUserProfileImage(@Param("username") String username);

    @Query("delete from ImageMetaData a where a.owner.id = :userId")
    @Modifying
    @Transactional
    void deleteMetaDataByOwner(@Param("userId") Long userId);

    ImageMetaData findByImageKey(String imageKey);

    @Query("select a from ImageMetaData a where a.imageGroup.userProfileImageGroup = true")
    List<ImageMetaData> loadImageMetaDataOfUserProfileImageSet();

    @Query("select a from ImageMetaData a where a.imageGroup.userProfileImageGroup = false")
    List<ImageMetaData> findImageMetaDataWithoutProfileImages();

    @Query("select a from ImageMetaData a where a.owner.username = :username and a.imageGroup.userProfileImageGroup = false")
    List<ImageMetaData> findImageMetaDataForUser(@Param("username") String username);

    ImageMetaData findByOwnerAndImageGroup(AppUser owner, ImageGroup imageGroup);

    List<ImageMetaData> findByOwner(AppUser owner);

}

@@ -260,67 +260,8 @@
 y;%0D%0A
-import org.springframework.data.jpa.repository.Modifying;%0D%0A
 impo
@@ -371,74 +371,8 @@
 am;%0D
-%0Aimport org.springframework.transaction.annotation.Transactional;%0D
 %0A%0D%0Ai
@@ -709,179 +709,8 @@
 %0D%0A%0D%0A
-    @Query(%22delete from ImageMetaData a where a.owner.id = :userId%22)%0D%0A    @Modifying%0D%0A    @Transactional%0D%0A    void deleteMetaDataByOwner(@Param(%22userId%22) Long userId);%0D%0A%0D%0A
     

delete user images when user itself will be deleted