fix(user-settings): fix settings loading errors (#527)
* refactor(user): optimize detail fetching with custom repository implementation * refactor(fonts): fix font loading and improve custom font handling * refactor(user-data): update userSettings to use input() for better reactivity * refactor(fonts): improve font loading logic and improve readi ness state management again * refactor(user-data): simplify user fetching logic and improve batch loading * refactor(custom-fonts): improve font loading logic with run ID for better state management
This commit is contained in:
@@ -2,69 +2,18 @@ package org.booklore.repository;
|
||||
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.enums.ProvisioningMethod;
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<BookLoreUserEntity, Long> {
|
||||
public interface UserRepository extends JpaRepository<BookLoreUserEntity, Long>, UserRepositoryCustom {
|
||||
|
||||
Optional<BookLoreUserEntity> findByUsername(String username);
|
||||
|
||||
Optional<BookLoreUserEntity> findByEmail(String email);
|
||||
|
||||
Optional<BookLoreUserEntity> findById(@NonNull Long id);
|
||||
|
||||
/**
|
||||
* Fetch a user with all eagerly-needed associations (settings, libraries, permissions)
|
||||
* in a single query to prevent N+1 problems after OSIV is disabled.
|
||||
*/
|
||||
@EntityGraph(attributePaths = {"settings", "libraries", "libraries.libraryPaths", "permissions"})
|
||||
@Query("SELECT u FROM BookLoreUserEntity u WHERE u.id = :id")
|
||||
Optional<BookLoreUserEntity> findByIdWithDetails(@Param("id") Long id);
|
||||
|
||||
/**
|
||||
* Fetch a user with settings and permissions (without libraries) for settings-related operations.
|
||||
*/
|
||||
@EntityGraph(attributePaths = {"settings", "permissions"})
|
||||
@Query("SELECT u FROM BookLoreUserEntity u WHERE u.id = :id")
|
||||
Optional<BookLoreUserEntity> findByIdWithSettings(@Param("id") Long id);
|
||||
|
||||
/**
|
||||
* Fetch a user with libraries and permissions for authorization/library-access operations.
|
||||
*/
|
||||
@EntityGraph(attributePaths = {"libraries", "libraries.libraryPaths", "permissions"})
|
||||
@Query("SELECT u FROM BookLoreUserEntity u WHERE u.id = :id")
|
||||
Optional<BookLoreUserEntity> findByIdWithLibraries(@Param("id") Long id);
|
||||
|
||||
/**
|
||||
* Fetch a user with just permissions for security checks or simple profile updates.
|
||||
*/
|
||||
@EntityGraph(attributePaths = {"permissions"})
|
||||
@Query("SELECT u FROM BookLoreUserEntity u WHERE u.id = :id")
|
||||
Optional<BookLoreUserEntity> findByIdWithPermissions(@Param("id") Long id);
|
||||
|
||||
/**
|
||||
* Fetch all users with their settings, libraries, and permissions in a single query.
|
||||
* Prevents N+1 when listing all users (admin panel).
|
||||
*/
|
||||
@EntityGraph(attributePaths = {"settings", "libraries", "libraries.libraryPaths", "permissions"})
|
||||
@Query("SELECT DISTINCT u FROM BookLoreUserEntity u")
|
||||
List<BookLoreUserEntity> findAllWithDetails();
|
||||
|
||||
/**
|
||||
* Fetch a user by username with all associations needed for authentication/DTO mapping.
|
||||
*/
|
||||
@EntityGraph(attributePaths = {"settings", "libraries", "libraries.libraryPaths", "permissions"})
|
||||
@Query("SELECT u FROM BookLoreUserEntity u WHERE u.username = :username")
|
||||
Optional<BookLoreUserEntity> findByUsernameWithDetails(@Param("username") String username);
|
||||
|
||||
long countByProvisioningMethod(ProvisioningMethod provisioningMethod);
|
||||
|
||||
Optional<BookLoreUserEntity> findByOidcIssuerAndOidcSubject(String oidcIssuer, String oidcSubject);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.booklore.repository;
|
||||
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepositoryCustom {
|
||||
|
||||
Optional<BookLoreUserEntity> findByIdWithDetails(Long id);
|
||||
|
||||
List<BookLoreUserEntity> findAllWithDetails();
|
||||
|
||||
Optional<BookLoreUserEntity> findByIdWithSettings(Long id);
|
||||
|
||||
Optional<BookLoreUserEntity> findByIdWithLibraries(Long id);
|
||||
|
||||
Optional<BookLoreUserEntity> findByIdWithPermissions(Long id);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.booklore.repository;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class UserRepositoryImpl implements UserRepositoryCustom {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<BookLoreUserEntity> findByIdWithDetails(Long id) {
|
||||
List<BookLoreUserEntity> users = em.createQuery(
|
||||
"SELECT DISTINCT u FROM BookLoreUserEntity u " +
|
||||
"LEFT JOIN FETCH u.settings " +
|
||||
"LEFT JOIN FETCH u.permissions " +
|
||||
"WHERE u.id = :id",
|
||||
BookLoreUserEntity.class)
|
||||
.setParameter("id", id)
|
||||
.getResultList();
|
||||
if (users.isEmpty()) return Optional.empty();
|
||||
|
||||
BookLoreUserEntity user = users.getFirst();
|
||||
Hibernate.initialize(user.getLibraries());
|
||||
user.getLibraries().forEach(lib -> Hibernate.initialize(lib.getLibraryPaths()));
|
||||
|
||||
return Optional.of(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookLoreUserEntity> findAllWithDetails() {
|
||||
List<BookLoreUserEntity> users = em.createQuery(
|
||||
"SELECT DISTINCT u FROM BookLoreUserEntity u " +
|
||||
"LEFT JOIN FETCH u.settings " +
|
||||
"LEFT JOIN FETCH u.permissions",
|
||||
BookLoreUserEntity.class)
|
||||
.getResultList();
|
||||
if (users.isEmpty()) return users;
|
||||
|
||||
users.forEach(user -> {
|
||||
Hibernate.initialize(user.getLibraries());
|
||||
user.getLibraries().forEach(lib -> Hibernate.initialize(lib.getLibraryPaths()));
|
||||
});
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<BookLoreUserEntity> findByIdWithSettings(Long id) {
|
||||
BookLoreUserEntity user = em.find(BookLoreUserEntity.class, id);
|
||||
if (user == null) return Optional.empty();
|
||||
Hibernate.initialize(user.getSettings());
|
||||
Hibernate.initialize(user.getPermissions());
|
||||
return Optional.of(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<BookLoreUserEntity> findByIdWithLibraries(Long id) {
|
||||
BookLoreUserEntity user = em.find(BookLoreUserEntity.class, id);
|
||||
if (user == null) return Optional.empty();
|
||||
Hibernate.initialize(user.getLibraries());
|
||||
user.getLibraries().forEach(lib -> Hibernate.initialize(lib.getLibraryPaths()));
|
||||
Hibernate.initialize(user.getPermissions());
|
||||
return Optional.of(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public Optional<BookLoreUserEntity> findByIdWithPermissions(Long id) {
|
||||
BookLoreUserEntity user = em.find(BookLoreUserEntity.class, id);
|
||||
if (user == null) return Optional.empty();
|
||||
Hibernate.initialize(user.getPermissions());
|
||||
return Optional.of(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user