Per user book preferences
This commit is contained in:
+7
-1
@@ -1,8 +1,10 @@
|
||||
package com.adityachandel.booklore.config.security;
|
||||
|
||||
import com.adityachandel.booklore.model.BookPreferences;
|
||||
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||
import com.adityachandel.booklore.model.entity.UserPermissionsEntity;
|
||||
import com.adityachandel.booklore.repository.UserRepository;
|
||||
import com.adityachandel.booklore.service.UserService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
@@ -13,9 +15,10 @@ import org.springframework.stereotype.Component;
|
||||
@AllArgsConstructor
|
||||
@Component
|
||||
public class ApplicationStartupRunner implements CommandLineRunner {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
@@ -34,6 +37,9 @@ public class ApplicationStartupRunner implements CommandLineRunner {
|
||||
permissions.setPermissionEditMetadata(true);
|
||||
permissions.setPermissionAdmin(true);
|
||||
|
||||
BookPreferences bookPreferences = userService.buildDefaultBookPreferences();
|
||||
admin.setBookPreferences(bookPreferences);
|
||||
|
||||
admin.setPermissions(permissions);
|
||||
userRepository.save(admin);
|
||||
|
||||
|
||||
+8
@@ -20,6 +20,14 @@ public class SecurityUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isSelf(Long userId) {
|
||||
var authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.getPrincipal() instanceof BookLoreUser user) {
|
||||
return user.getId().equals(userId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean canUpload() {
|
||||
var authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication != null && authentication.getPrincipal() instanceof BookLoreUser user) {
|
||||
|
||||
+3
-2
@@ -23,8 +23,9 @@ public class AuthController {
|
||||
|
||||
@PostMapping("/register")
|
||||
@PreAuthorize("@securityUtil.isAdmin()")
|
||||
public ResponseEntity<Map<String, String>> registerUser(@RequestBody @Valid UserCreateRequest userCreateRequest) {
|
||||
return userService.registerUser(userCreateRequest);
|
||||
public ResponseEntity<?> registerUser(@RequestBody @Valid UserCreateRequest userCreateRequest) {
|
||||
userService.registerUser(userCreateRequest);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.BookPreferences;
|
||||
import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
import com.adityachandel.booklore.model.dto.UserCreateRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.UserLoginRequest;
|
||||
@@ -46,4 +47,11 @@ public class UserController {
|
||||
public void deleteUser(@PathVariable Long id) {
|
||||
userService.deleteUser(id);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}/book-preferences")
|
||||
@PreAuthorize("@securityUtil.isSelf(#id)")
|
||||
public void updateUserBookPreferences(@PathVariable Long id, @RequestBody BookPreferences bookPreferences) {
|
||||
userService.updateBookPreferences(id, bookPreferences);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.adityachandel.booklore.convertor;
|
||||
|
||||
import com.adityachandel.booklore.model.BookPreferences;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
import jakarta.persistence.Converter;
|
||||
|
||||
@Converter(autoApply = true)
|
||||
public class BookPreferencesConverter implements AttributeConverter<BookPreferences, String> {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(BookPreferences preferences) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(preferences);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException("Error converting BookPreferences to JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BookPreferences convertToEntityAttribute(String json) {
|
||||
try {
|
||||
return objectMapper.readValue(json, BookPreferences.class);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error converting JSON to BookPreferences", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.adityachandel.booklore.model;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BookPreferences {
|
||||
|
||||
private PerBookSetting perBookSetting;
|
||||
private PdfReaderSetting pdfReaderSetting;
|
||||
private EpubReaderSetting epubReaderSetting;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class EpubReaderSetting {
|
||||
private String theme;
|
||||
private String font;
|
||||
private int fontSize;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class PdfReaderSetting {
|
||||
private String pageSpread;
|
||||
private String pageZoom;
|
||||
private boolean showSidebar;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class PerBookSetting {
|
||||
private GlobalOrIndividual pdf;
|
||||
private GlobalOrIndividual epub;
|
||||
}
|
||||
|
||||
public enum GlobalOrIndividual {
|
||||
Global, Individual
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import com.adityachandel.booklore.model.BookPreferences;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
@@ -12,6 +13,7 @@ public class BookLoreUser {
|
||||
private String email;
|
||||
private List<Library> assignedLibraries;
|
||||
private UserPermissions permissions;
|
||||
private BookPreferences bookPreferences;
|
||||
|
||||
@Data
|
||||
public static class UserPermissions {
|
||||
|
||||
+7
@@ -1,5 +1,8 @@
|
||||
package com.adityachandel.booklore.model.entity;
|
||||
|
||||
import com.adityachandel.booklore.convertor.BookPreferencesConverter;
|
||||
import com.adityachandel.booklore.convertor.SortConverter;
|
||||
import com.adityachandel.booklore.model.BookPreferences;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
@@ -39,6 +42,10 @@ public class BookLoreUserEntity {
|
||||
@Column(name = "updated_at")
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
@Column(name = "book_preferences")
|
||||
@Convert(converter = BookPreferencesConverter.class)
|
||||
private BookPreferences bookPreferences;
|
||||
|
||||
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private UserPermissionsEntity permissions;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.adityachandel.booklore.service;
|
||||
import com.adityachandel.booklore.config.security.JwtUtils;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.mapper.BookLoreUserMapper;
|
||||
import com.adityachandel.booklore.model.BookPreferences;
|
||||
import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
import com.adityachandel.booklore.model.dto.UserCreateRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.UserLoginRequest;
|
||||
@@ -35,7 +36,7 @@ public class UserService {
|
||||
private final JwtUtils jwtUtils;
|
||||
|
||||
@Transactional
|
||||
public ResponseEntity<Map<String, String>> registerUser(UserCreateRequest request) {
|
||||
public void registerUser(UserCreateRequest request) {
|
||||
Optional<BookLoreUserEntity> existingUser = userRepository.findByUsername(request.getUsername());
|
||||
if (existingUser.isPresent()) {
|
||||
throw ApiError.USERNAME_ALREADY_TAKEN.createException(request.getUsername());
|
||||
@@ -55,19 +56,14 @@ public class UserService {
|
||||
permissions.setPermissionManipulateLibrary(request.isPermissionManipulateLibrary());
|
||||
bookLoreUserEntity.setPermissions(permissions);
|
||||
|
||||
bookLoreUserEntity.setBookPreferences(buildDefaultBookPreferences());
|
||||
|
||||
if (request.getSelectedLibraries() != null && !request.getSelectedLibraries().isEmpty()) {
|
||||
List<LibraryEntity> libraries = libraryRepository.findAllById(request.getSelectedLibraries());
|
||||
bookLoreUserEntity.setLibraries(new ArrayList<>(libraries));
|
||||
}
|
||||
|
||||
userRepository.save(bookLoreUserEntity);
|
||||
|
||||
String token = jwtUtils.generateToken(bookLoreUserEntity);
|
||||
|
||||
Map<String, String> response = new HashMap<>();
|
||||
response.put("token", token);
|
||||
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(response);
|
||||
}
|
||||
|
||||
public ResponseEntity<Map<String, String>> loginUser(UserLoginRequest loginRequest) {
|
||||
@@ -124,9 +120,36 @@ public class UserService {
|
||||
userRepository.delete(user);
|
||||
}
|
||||
|
||||
|
||||
public BookLoreUser getBookLoreUser(Long id) {
|
||||
BookLoreUserEntity user = userRepository.findById(id).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(id));
|
||||
return bookLoreUserMapper.toDto(user);
|
||||
}
|
||||
|
||||
|
||||
public BookPreferences buildDefaultBookPreferences() {
|
||||
return BookPreferences.builder()
|
||||
.perBookSetting(BookPreferences.PerBookSetting.builder()
|
||||
.epub(BookPreferences.GlobalOrIndividual.Individual)
|
||||
.pdf(BookPreferences.GlobalOrIndividual.Individual)
|
||||
.build())
|
||||
.pdfReaderSetting(BookPreferences.PdfReaderSetting.builder()
|
||||
.showSidebar(false)
|
||||
.pageSpread("odd")
|
||||
.pageZoom("page-fit")
|
||||
.build())
|
||||
.epubReaderSetting(BookPreferences.EpubReaderSetting.builder()
|
||||
.theme("white")
|
||||
.font("serif")
|
||||
.fontSize(150)
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
public void updateBookPreferences(long userId, BookPreferences bookPreferences) {
|
||||
BookLoreUserEntity user = userRepository.findById(userId).orElseThrow(() -> ApiError.USER_NOT_FOUND.createException(userId));
|
||||
user.setBookPreferences(bookPreferences);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -140,13 +140,14 @@ CREATE INDEX IF NOT EXISTS idx_author_id ON book_metadata_author_mapping (author
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(255) NOT NULL UNIQUE,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255) UNIQUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(255) NOT NULL UNIQUE,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255) UNIQUE,
|
||||
book_preferences TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_permissions
|
||||
@@ -204,11 +205,9 @@ CREATE TABLE IF NOT EXISTS user_book_progress
|
||||
CONSTRAINT fk_user_book_progress_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE,
|
||||
CONSTRAINT unique_user_book_progress UNIQUE (user_id, book_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_book_progress_user ON user_book_progress (user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_book_progress_book ON user_book_progress (book_id);
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_library_mapping
|
||||
(
|
||||
user_id BIGINT NOT NULL,
|
||||
|
||||
@@ -1,20 +1,3 @@
|
||||
INSERT INTO app_settings (category, name, val)
|
||||
VALUES
|
||||
('epub', 'theme', 'white'),
|
||||
('epub', 'fontSize', '150'),
|
||||
('epub', 'font', 'serif');
|
||||
|
||||
INSERT INTO app_settings (category, name, val)
|
||||
VALUES
|
||||
('pdf', 'spread', 'odd'),
|
||||
('pdf', 'zoom', 'page-fit'),
|
||||
('pdf', 'sidebar', 'true');
|
||||
|
||||
INSERT INTO app_settings (category, name, val)
|
||||
VALUES
|
||||
('reader_setting', 'pdf', 'individual'),
|
||||
('reader_setting', 'epub', 'individual');
|
||||
|
||||
INSERT INTO app_settings (category, name, val)
|
||||
VALUES
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user