Admin backend - #40
Merged
Merged
Conversation
…ngs management and admin controls.
There was a problem hiding this comment.
Pull request overview
Adds admin-focused backend capabilities to the CrimeLink Analyzer service, introducing database backup/restore functionality and persisted system settings management exposed via /api/admin/**.
Changes:
- Added
BackupService+BackupMetadatapersistence and new admin endpoints to create/restore/list backups. - Added
SystemSettingsService+SystemSettingpersistence and admin endpoints to get/update system settings with validation. - Added backup directory configuration and ignored local
backupsoutput in.gitignore.
Reviewed changes
Copilot reviewed 9 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/main/resources/application.properties | Adds configurable backup.directory property. |
| src/main/java/com/crimeLink/analyzer/service/SystemSettingsService.java | Implements defaulted settings retrieval and validated updates persisted to DB. |
| src/main/java/com/crimeLink/analyzer/service/BackupService.java | Implements JDBC-based backup creation and SQL-script restore from filesystem. |
| src/main/java/com/crimeLink/analyzer/repository/SystemSettingRepository.java | JPA repository for SystemSetting. |
| src/main/java/com/crimeLink/analyzer/repository/BackupMetadataRepository.java | JPA repository for BackupMetadata and ordered listing. |
| src/main/java/com/crimeLink/analyzer/entity/SystemSetting.java | Adds system_settings entity with updatedAt lifecycle hook. |
| src/main/java/com/crimeLink/analyzer/entity/BackupMetadata.java | Adds backup_metadata entity for tracking backup runs. |
| src/main/java/com/crimeLink/analyzer/controller/AdminController.java | Adds admin-only endpoints for backup/restore/backups list and settings get/update. |
| src/main/java/com/crimeLink/analyzer/config/SecurityConfig.java | Restricts backup/restore/settings routes to Admin role before general /api/admin/** rule. |
| .gitignore | Ignores backups directory. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+113
to
+117
| String sql = Files.readString(backupFile); | ||
|
|
||
| // Split by semicolons and execute each statement | ||
| String[] statements = sql.split(";"); | ||
| int executed = 0; |
Comment on lines
+64
to
+66
| try (BufferedWriter writer = new BufferedWriter(new FileWriter(backupFile.toFile()))) { | ||
| writer.write("-- CrimeLink Analyzer Database Backup\n"); | ||
| writer.write("-- Created: " + LocalDateTime.now() + "\n"); |
Comment on lines
+125
to
+137
| try { | ||
| jdbcTemplate.execute(trimmed); | ||
| executed++; | ||
| } catch (Exception stmtEx) { | ||
| // Log and continue — some statements may fail on duplicates etc. | ||
| log.warn("Skipping failed statement: {}... Error: {}", | ||
| sanitizeForLog(trimmed.substring(0, Math.min(80, trimmed.length()))), | ||
| sanitizeForLog(stmtEx.getMessage())); | ||
| } | ||
| } | ||
|
|
||
| log.info("Database restored from {} by {} ({} statements executed)", | ||
| sanitizeForLog(filename), sanitizeForLog(userEmail), executed); |
Comment on lines
+65
to
+72
| // Validate numeric value | ||
| int numericValue; | ||
| try { | ||
| numericValue = Integer.parseInt(value); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException( | ||
| "Setting '" + key + "' must be a valid integer, got: " + value); | ||
| } |
Comment on lines
+54
to
+90
| public Map<String, String> updateSettings(Map<String, String> incoming) { | ||
| for (Map.Entry<String, String> entry : incoming.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
|
|
||
| // Ignore unknown keys | ||
| if (!DEFAULTS.containsKey(key)) { | ||
| log.warn("Ignoring unknown setting key: {}", sanitizeForLog(key)); | ||
| continue; | ||
| } | ||
|
|
||
| // Validate numeric value | ||
| int numericValue; | ||
| try { | ||
| numericValue = Integer.parseInt(value); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException( | ||
| "Setting '" + key + "' must be a valid integer, got: " + value); | ||
| } | ||
|
|
||
| int[] range = VALIDATION_RULES.get(key); | ||
| if (range != null && (numericValue < range[0] || numericValue > range[1])) { | ||
| throw new IllegalArgumentException( | ||
| "Setting '" + key + "' must be between " + range[0] + " and " + range[1] + | ||
| ", got: " + numericValue); | ||
| } | ||
|
|
||
| // Upsert | ||
| SystemSetting setting = settingRepo.findBySettingKey(key) | ||
| .orElseGet(() -> { | ||
| SystemSetting s = new SystemSetting(); | ||
| s.setSettingKey(key); | ||
| return s; | ||
| }); | ||
| setting.setSettingValue(value); | ||
| settingRepo.save(setting); | ||
| } |
Comment on lines
+112
to
+131
| try { | ||
| String sql = Files.readString(backupFile); | ||
|
|
||
| // Split by semicolons and execute each statement | ||
| String[] statements = sql.split(";"); | ||
| int executed = 0; | ||
|
|
||
| for (String stmt : statements) { | ||
| String trimmed = stmt.trim(); | ||
| // Skip empty lines and comments | ||
| if (trimmed.isEmpty() || trimmed.startsWith("--")) { | ||
| continue; | ||
| } | ||
| try { | ||
| jdbcTemplate.execute(trimmed); | ||
| executed++; | ||
| } catch (Exception stmtEx) { | ||
| // Log and continue — some statements may fail on duplicates etc. | ||
| log.warn("Skipping failed statement: {}... Error: {}", | ||
| sanitizeForLog(trimmed.substring(0, Math.min(80, trimmed.length()))), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.