Add ability to mark magic shelves as public (#1489)
Adds a checkbox to the create/edit magic shelf dialog to set the shelf as public. If public then it shows up for all users. Only admins can edit public shelves (edit options disabled for non-admin)
This commit is contained in:
@@ -21,4 +21,6 @@ public class MagicShelf {
|
||||
@NotNull(message = "Filter JSON must not be null")
|
||||
@Size(min = 2, message = "Filter JSON must not be empty")
|
||||
private String filterJson;
|
||||
|
||||
private Boolean isPublic = false;
|
||||
}
|
||||
|
||||
+4
@@ -32,6 +32,10 @@ public class MagicShelfEntity {
|
||||
@Column(name = "filter_json", columnDefinition = "json", nullable = false)
|
||||
private String filterJson;
|
||||
|
||||
@Column(name = "is_public", nullable = false)
|
||||
@lombok.Builder.Default
|
||||
private boolean isPublic = false;
|
||||
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt = LocalDateTime.now();
|
||||
|
||||
|
||||
+2
@@ -10,6 +10,8 @@ public interface MagicShelfRepository extends JpaRepository<MagicShelfEntity, Lo
|
||||
|
||||
List<MagicShelfEntity> findAllByUserId(Long userId);
|
||||
|
||||
List<MagicShelfEntity> findAllByIsPublicIsTrue();
|
||||
|
||||
Optional<MagicShelfEntity> findByUserIdAndName(Long userId, String name);
|
||||
|
||||
boolean existsByUserIdAndName(Long userId, String name);
|
||||
|
||||
+20
-2
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
@@ -19,9 +20,20 @@ public class MagicShelfService {
|
||||
|
||||
public List<MagicShelf> getUserShelves() {
|
||||
Long userId = authenticationService.getAuthenticatedUser().getId();
|
||||
return repository.findAllByUserId(userId).stream()
|
||||
|
||||
List<MagicShelf> shelves = repository.findAllByUserId(userId).stream()
|
||||
.map(this::toDto)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<Long> userShelfIds = shelves.stream().map(MagicShelf::getId).toList();
|
||||
|
||||
List<MagicShelf> publicShelves = repository.findAllByIsPublicIsTrue().stream()
|
||||
.map(this::toDto)
|
||||
.filter(shelf -> !userShelfIds.contains(shelf.getId()))
|
||||
.toList();
|
||||
|
||||
shelves.addAll(publicShelves);
|
||||
return shelves;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@@ -32,9 +44,13 @@ public class MagicShelfService {
|
||||
if (!existing.getUserId().equals(userId)) {
|
||||
throw new SecurityException("You are not authorized to update this shelf");
|
||||
}
|
||||
if (existing.isPublic() && !authenticationService.getAuthenticatedUser().getPermissions().isAdmin()) {
|
||||
throw new SecurityException("You are not authorized to update a public shelf");
|
||||
}
|
||||
existing.setName(dto.getName());
|
||||
existing.setIcon(dto.getIcon());
|
||||
existing.setFilterJson(dto.getFilterJson());
|
||||
existing.setPublic(dto.getIsPublic());
|
||||
return toDto(repository.save(existing));
|
||||
}
|
||||
if (repository.existsByUserIdAndName(userId, dto.getName())) {
|
||||
@@ -59,6 +75,7 @@ public class MagicShelfService {
|
||||
dto.setName(entity.getName());
|
||||
dto.setIcon(entity.getIcon());
|
||||
dto.setFilterJson(entity.getFilterJson());
|
||||
dto.setIsPublic(entity.isPublic());
|
||||
return dto;
|
||||
}
|
||||
|
||||
@@ -68,6 +85,7 @@ public class MagicShelfService {
|
||||
entity.setName(dto.getName());
|
||||
entity.setIcon(dto.getIcon());
|
||||
entity.setFilterJson(dto.getFilterJson());
|
||||
entity.setPublic(dto.getIsPublic());
|
||||
entity.setUserId(userId);
|
||||
return entity;
|
||||
}
|
||||
@@ -76,4 +94,4 @@ public class MagicShelfService {
|
||||
MagicShelfEntity shelf = repository.findById(id).orElseThrow(() -> new IllegalArgumentException("Shelf not found"));
|
||||
return toDto(shelf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE magic_shelf ADD COLUMN is_public BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
Reference in New Issue
Block a user