Implement Public Shelves (#1969)

* Implement Public Shelves

Multiple changes to support shelves being marked as public.

- **Backend:**
    - Added 'isPublic' field the Shelves
    - Made it so that when looking for shelves it includes public ones
    - Allow admins to mark shelves are public
    - Added 'bookCount' to 'Shelf' so it shows for public shelves as
well
    - Added 'canReadShelf' to 'SecurityUtil' for checking read access to
public shelves.
    - Updated ShelfController to use this for getting shelf data

- **Frontend:**
    - Updated Shelf to include 'publicShelf', 'userId', and 'bookCount'
    - Updated components to allow admins to toggle shelf visibility.
    - Modified 'ShelfService' to:
        - Add 'getBooksOnShelf' method to fetch books from the API.
        - Update /getBookCount' to use 'Shelf.bookCount' for non-owners.
    - Restricted edit/delete options to shelf owners.

* Renumber Add_is_public_to_shelf.sql

Conflicted with another

---------

Co-authored-by: ACX <8075870+acx10@users.noreply.github.com>
This commit is contained in:
Chris Debenham
2026-01-15 21:19:05 -07:00
committed by GitHub
co-authored by ACX
parent c20b1972f8
commit 36d9f74864
18 changed files with 139 additions and 18 deletions
@@ -129,4 +129,14 @@ public class SecurityUtil {
var user = getCurrentUser();
return user != null && user.getPermissions().isCanAccessTaskManager();
}
public boolean canReadShelf(Long shelfId) {
var user = getCurrentUser();
if (user != null) {
return shelfRepository.findById(shelfId)
.map(shelf -> shelf.isPublic() || shelf.getUser().getId().equals(user.getId()))
.orElse(false);
}
return false;
}
}
@@ -39,7 +39,7 @@ public class ShelfController {
@ApiResponse(responseCode = "403", description = "Forbidden")
})
@GetMapping("/{shelfId}")
@PreAuthorize("@securityUtil.isShelfOwner(#shelfId)")
@PreAuthorize("@securityUtil.canReadShelf(#shelfId)")
public ResponseEntity<Shelf> getShelf(
@Parameter(description = "ID of the shelf") @PathVariable Long shelfId) {
return ResponseEntity.ok(shelfService.getShelf(shelfId));
@@ -76,7 +76,7 @@ public class ShelfController {
@Operation(summary = "Get books on a shelf", description = "Retrieve all books assigned to a specific shelf.")
@ApiResponse(responseCode = "200", description = "Books returned successfully")
@GetMapping("/{shelfId}/books")
@PreAuthorize("@securityUtil.isShelfOwner(#shelfId)")
@PreAuthorize("@securityUtil.canReadShelf(#shelfId)")
public ResponseEntity<List<Book>> getShelfBooks(
@Parameter(description = "ID of the shelf") @PathVariable Long shelfId) {
return ResponseEntity.ok(shelfService.getShelfBooks(shelfId));
@@ -9,5 +9,7 @@ import org.mapstruct.Mapping;
public interface ShelfMapper {
@Mapping(source = "user.id", target = "userId")
@Mapping(source = "public", target = "publicShelf")
@Mapping(target = "bookCount", expression = "java(shelfEntity.getBookEntities() != null ? shelfEntity.getBookEntities().size() : 0)")
Shelf toShelf(ShelfEntity shelfEntity);
}
@@ -15,4 +15,6 @@ public class Shelf {
private IconType iconType;
private Sort sort;
private Long userId;
private boolean publicShelf;
private int bookCount;
}
@@ -21,4 +21,6 @@ public class ShelfCreateRequest {
@NotNull(message = "Shelf icon type must not be null.")
private IconType iconType;
private boolean publicShelf;
}
@@ -40,6 +40,10 @@ public class ShelfEntity {
@Builder.Default
private IconType iconType = IconType.PRIME_NG;
@Column(name = "is_public", nullable = false)
@Builder.Default
private boolean isPublic = false;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "book_shelf_mapping",
@@ -16,5 +16,8 @@ public interface ShelfRepository extends JpaRepository<ShelfEntity, Long> {
Optional<ShelfEntity> findByUserIdAndName(Long id, String name);
@org.springframework.data.jpa.repository.Query("SELECT s FROM ShelfEntity s WHERE s.user.id = :userId OR s.isPublic = true")
List<ShelfEntity> findByUserIdOrPublicShelfTrue(@org.springframework.data.repository.query.Param("userId") Long userId);
List<ShelfEntity> findByUserIdInAndName(List<Long> userIds, String name);
}
@@ -37,10 +37,14 @@ public class ShelfService {
if (shelfRepository.existsByUserIdAndName(userId, request.getName())) {
throw ApiError.SHELF_ALREADY_EXISTS.createException(request.getName());
}
if (request.isPublicShelf() && !authenticationService.getAuthenticatedUser().getPermissions().isAdmin()) {
throw new org.springframework.security.access.AccessDeniedException("Only admins can create public shelves");
}
ShelfEntity shelfEntity = ShelfEntity.builder()
.icon(request.getIcon())
.name(request.getName())
.iconType(request.getIconType())
.isPublic(request.isPublicShelf())
.user(fetchUserEntityById(userId))
.build();
return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
@@ -48,15 +52,19 @@ public class ShelfService {
public Shelf updateShelf(Long id, ShelfCreateRequest request) {
ShelfEntity shelfEntity = findShelfByIdOrThrow(id);
if (request.isPublicShelf() && !authenticationService.getAuthenticatedUser().getPermissions().isAdmin()) {
throw new org.springframework.security.access.AccessDeniedException("Only admins can create public shelves");
}
shelfEntity.setName(request.getName());
shelfEntity.setIcon(request.getIcon());
shelfEntity.setIconType(request.getIconType());
shelfEntity.setPublic(request.isPublicShelf());
return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
}
public List<Shelf> getShelves() {
Long userId = getAuthenticatedUserId();
return shelfRepository.findByUserId(userId).stream()
return shelfRepository.findByUserIdOrPublicShelfTrue(userId).stream()
.map(shelfMapper::toShelf)
.toList();
}
@@ -0,0 +1 @@
ALTER TABLE shelf ADD COLUMN is_public BOOLEAN NOT NULL DEFAULT FALSE;