Allow users to disable kobo shelf

This commit is contained in:
aditya.chandel
2025-08-28 21:12:45 -06:00
committed by Aditya Chandel
parent 8d04bc3498
commit 12b7aaa8e9
9 changed files with 154 additions and 77 deletions
@@ -143,7 +143,9 @@ public class KoboController {
public ResponseEntity<?> deleteBookFromLibrary(@PathVariable String bookId) {
if (StringUtils.isNumeric(bookId)) {
Shelf userKoboShelf = shelfService.getUserKoboShelf();
bookService.assignShelvesToBooks(Set.of(Long.valueOf(bookId)), Set.of(), Set.of(userKoboShelf.getId()));
if (userKoboShelf != null) {
bookService.assignShelvesToBooks(Set.of(Long.valueOf(bookId)), Set.of(), Set.of(userKoboShelf.getId()));
}
return ResponseEntity.ok().build();
} else {
return koboServerProxy.proxyCurrentRequest(null, false);
@@ -17,7 +17,6 @@ public class KoboSettingsController {
private final KoboSettingsService koboService;
@GetMapping
@PreAuthorize("@securityUtil.canSyncKobo() or @securityUtil.isAdmin()")
public ResponseEntity<KoboSyncSettings> getSettings() {
KoboSyncSettings settings = koboService.getCurrentUserSettings();
return ResponseEntity.ok(settings);
@@ -30,7 +29,7 @@ public class KoboSettingsController {
return ResponseEntity.ok(updated);
}
@PatchMapping("/sync")
@PutMapping("/sync")
@PreAuthorize("@securityUtil.canSyncKobo() or @securityUtil.isAdmin()")
public ResponseEntity<Void> toggleSync(@RequestParam boolean enabled) {
koboService.setSyncEnabled(enabled);
@@ -8,5 +8,5 @@ public class KoboSyncSettings {
private Long id;
private String userId;
private String token;
private boolean syncEnabled = true;
private boolean syncEnabled;
}
@@ -3,6 +3,7 @@ package com.adityachandel.booklore.service;
import com.adityachandel.booklore.config.security.service.AuthenticationService;
import com.adityachandel.booklore.model.dto.BookLoreUser;
import com.adityachandel.booklore.model.dto.KoboSyncSettings;
import com.adityachandel.booklore.model.dto.Shelf;
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
import com.adityachandel.booklore.model.entity.KoboUserSettingsEntity;
import com.adityachandel.booklore.model.entity.ShelfEntity;
@@ -44,6 +45,7 @@ public class KoboSettingsService {
.orElseGet(() -> KoboUserSettingsEntity.builder()
.userId(user.getId())
.token(newToken)
.syncEnabled(false)
.build());
ensureKoboShelfExists(user.getId());
@@ -55,9 +57,15 @@ public class KoboSettingsService {
@Transactional
public void setSyncEnabled(boolean enabled) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
KoboUserSettingsEntity entity = repository.findByUserId(user.getId())
.orElseThrow(() -> new IllegalStateException("Kobo settings not found for user"));
KoboUserSettingsEntity entity = repository.findByUserId(user.getId()).orElseThrow(() -> new IllegalStateException("Kobo settings not found for user"));
Shelf userKoboShelf = shelfService.getUserKoboShelf();
if (!enabled) {
if (userKoboShelf != null) {
shelfService.deleteShelf(userKoboShelf.getId());
}
} else {
ensureKoboShelfExists(user.getId());
}
entity.setSyncEnabled(enabled);
repository.save(entity);
}
@@ -66,6 +74,7 @@ public class KoboSettingsService {
ensureKoboShelfExists(userId);
KoboUserSettingsEntity entity = KoboUserSettingsEntity.builder()
.userId(userId)
.syncEnabled(false)
.token(generateToken())
.build();
return repository.save(entity);
@@ -75,10 +84,10 @@ public class KoboSettingsService {
Optional<ShelfEntity> shelf = shelfService.getShelf(userId, ShelfType.KOBO.getName());
if (shelf.isEmpty()) {
shelfService.createShelf(
ShelfCreateRequest.builder()
.name(ShelfType.KOBO.getName())
.icon(ShelfType.KOBO.getIcon())
.build()
ShelfCreateRequest.builder()
.name(ShelfType.KOBO.getName())
.icon(ShelfType.KOBO.getIcon())
.build()
);
}
}
@@ -92,6 +101,7 @@ public class KoboSettingsService {
dto.setId(entity.getId());
dto.setUserId(entity.getUserId().toString());
dto.setToken(entity.getToken());
dto.setSyncEnabled(entity.isSyncEnabled());
return dto;
}
}
@@ -64,18 +64,13 @@ public class ShelfService {
}
public void deleteShelf(Long shelfId) {
ShelfEntity shelfEntity = findShelfByIdOrThrow(shelfId);
if (shelfEntity.getName().equalsIgnoreCase(ShelfType.KOBO.getName())) {
throw ApiError.SHELF_CANNOT_BE_DELETED.createException(ShelfType.KOBO.getName());
}
shelfRepository.deleteById(shelfId);
}
public Shelf getUserKoboShelf() {
Long userId = getAuthenticatedUserId();
ShelfEntity koboShelf = shelfRepository.findByUserIdAndName(userId, ShelfType.KOBO.getName())
.orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(ShelfType.KOBO.getName()));
return shelfMapper.toShelf(koboShelf);
Optional<ShelfEntity> koboShelf = shelfRepository.findByUserIdAndName(userId, ShelfType.KOBO.getName());
return koboShelf.map(shelfMapper::toShelf).orElse(null);
}
public List<Book> getShelfBooks(Long shelfId) {