WIP: App Settings
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettings;
|
||||
import com.adityachandel.booklore.service.AppSettingService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
public class AppSettingController {
|
||||
|
||||
private AppSettingService appSettingService;
|
||||
|
||||
@GetMapping("/api/settings")
|
||||
public AppSettings getAppSettings() {
|
||||
return appSettingService.getAppSettings();
|
||||
}
|
||||
|
||||
@PutMapping("/api/settings")
|
||||
public void updateSetting(@RequestParam String category, @RequestParam String key, @RequestParam String newValue) {
|
||||
appSettingService.updateSetting(category, key, newValue);
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.adityachandel.booklore.convertor;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
import jakarta.persistence.Converter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@Converter(autoApply = true)
|
||||
public class MapToStringConverter implements AttributeConverter<Map<String, String>, String> {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
public String convertToDatabaseColumn(Map<String, String> attribute) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(attribute);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("Error converting Map to String", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> convertToEntityAttribute(String dbData) {
|
||||
try {
|
||||
return objectMapper.readValue(dbData, Map.class);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("Error converting String to Map", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.adityachandel.booklore.model.dto.settings;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AppSettings {
|
||||
private EpubSettings epub;
|
||||
private PdfSettings pdf;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class EpubSettings {
|
||||
private String theme;
|
||||
private String fontSize;
|
||||
private String font;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class PdfSettings {
|
||||
private String spread;
|
||||
private String zoom;
|
||||
private boolean sidebar;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.adityachandel.booklore.model.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
|
||||
@Entity
|
||||
@Table(name = "app_settings")
|
||||
@Data
|
||||
public class AppSettingEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "category", nullable = false)
|
||||
private String category;
|
||||
|
||||
@Column(name = "key1", nullable = false)
|
||||
private String key;
|
||||
|
||||
@Column(name = "value", nullable = false, columnDefinition = "TEXT")
|
||||
private String value;
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.adityachandel.booklore.repository;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.AppSettingEntity;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AppSettingsRepository extends JpaRepository<AppSettingEntity, Long> {
|
||||
AppSettingEntity findByCategoryAndKey(String category, String key);;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettings;
|
||||
import com.adityachandel.booklore.model.entity.AppSettingEntity;
|
||||
import com.adityachandel.booklore.repository.AppSettingsRepository;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class AppSettingService {
|
||||
|
||||
private AppSettingsRepository appSettingsRepository;
|
||||
|
||||
public AppSettings getAppSettings() {
|
||||
List<AppSettingEntity> settings = appSettingsRepository.findAll();
|
||||
Map<String, Map<String, String>> settingsMap = settings.stream().collect(Collectors.groupingBy(AppSettingEntity::getCategory, Collectors.toMap(AppSettingEntity::getKey, AppSettingEntity::getValue)));
|
||||
AppSettings appSettings = new AppSettings();
|
||||
if (settingsMap.containsKey("epub")) {
|
||||
Map<String, String> epubSettings = settingsMap.get("epub");
|
||||
appSettings.setEpub(new AppSettings.EpubSettings(epubSettings.get("theme"), epubSettings.get("fontSize"), epubSettings.get("font")));
|
||||
}
|
||||
if (settingsMap.containsKey("pdf")) {
|
||||
Map<String, String> pdfSettings = settingsMap.get("pdf");
|
||||
appSettings.setPdf(new AppSettings.PdfSettings(pdfSettings.get("spread"), pdfSettings.get("zoom"), Boolean.parseBoolean(pdfSettings.get("sidebar"))));
|
||||
}
|
||||
return appSettings;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateSetting(String category, String key, String newValue) {
|
||||
AppSettingEntity setting = appSettingsRepository.findByCategoryAndKey(category, key);
|
||||
if (setting != null) {
|
||||
setting.setValue(newValue);
|
||||
appSettingsRepository.save(setting);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Setting not found for category: " + category + " and key: " + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,4 +102,13 @@ CREATE TABLE IF NOT EXISTS book_shelf_mapping
|
||||
PRIMARY KEY (book_id, shelf_id),
|
||||
CONSTRAINT fk_book_shelf_mapping_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_book_shelf_mapping_shelf FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE app_settings
|
||||
(
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
category VARCHAR(255) NOT NULL,
|
||||
key1 VARCHAR (255) NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
UNIQUE (category, key1)
|
||||
);
|
||||
@@ -1 +1,12 @@
|
||||
INSERT INTO booklore.shelf (name, icon) VALUES ('Favorites', 'heart');
|
||||
INSERT INTO booklore.shelf (name, icon)
|
||||
VALUES ('Favorites', 'heart');
|
||||
|
||||
INSERT INTO app_settings (category, key1, value)
|
||||
VALUES ('epub', 'theme', 'white'),
|
||||
('epub', 'fontSize', '120'),
|
||||
('epub', 'font', 'serif');
|
||||
|
||||
INSERT INTO app_settings (category, key1, value)
|
||||
VALUES ('pdf', 'spread', 'even_spread'),
|
||||
('pdf', 'zoom', 'page_fit'),
|
||||
('pdf', 'sidebar', 'true');
|
||||
Reference in New Issue
Block a user