|
| 1 | +package com.crimeLink.analyzer.controller; |
| 2 | + |
| 3 | +import com.crimeLink.analyzer.service.CriminalService; |
| 4 | +import com.crimeLink.analyzer.util.LogSanitizer; |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.*; |
| 9 | +import org.springframework.web.multipart.MultipartFile; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +/** |
| 16 | + * REST Controller for Criminal record management (CRUD). |
| 17 | + * Operates directly against the database via JPA — coordinates with Python ML for embeddings. |
| 18 | + * |
| 19 | + * Endpoints: |
| 20 | + * - POST /api/criminals - Create new criminal (with photo upload + embedding) |
| 21 | + * - GET /api/criminals - List all criminals |
| 22 | + * - GET /api/criminals/{id} - Get criminal details |
| 23 | + * - PUT /api/criminals/{id} - Update criminal profile (with optional photo) |
| 24 | + * - DELETE /api/criminals/{id} - Delete criminal record |
| 25 | + */ |
| 26 | +@RestController |
| 27 | +@RequestMapping("/api/criminals") |
| 28 | +@RequiredArgsConstructor |
| 29 | +@Slf4j |
| 30 | +public class CriminalController { |
| 31 | + |
| 32 | + private final CriminalService criminalService; |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a new criminal record with optional photo. |
| 36 | + */ |
| 37 | + @PostMapping |
| 38 | + public ResponseEntity<?> createCriminal( |
| 39 | + @RequestParam("name") String name, |
| 40 | + @RequestParam("nic") String nic, |
| 41 | + @RequestParam(value = "photo", required = false) MultipartFile photo, |
| 42 | + @RequestParam(value = "risk_level", required = false) String riskLevel, |
| 43 | + @RequestParam(value = "crime_history", required = false) String crimeHistory, |
| 44 | + @RequestParam(value = "address", required = false) String address, |
| 45 | + @RequestParam(value = "contact_number", required = false) String contactNumber, |
| 46 | + @RequestParam(value = "secondary_contact", required = false) String secondaryContact, |
| 47 | + @RequestParam(value = "date_of_birth", required = false) String dateOfBirth, |
| 48 | + @RequestParam(value = "gender", required = false) String gender, |
| 49 | + @RequestParam(value = "alias", required = false) String alias, |
| 50 | + @RequestParam(value = "status", required = false) String status) { |
| 51 | + |
| 52 | + try { |
| 53 | + log.info("Criminal registration requested: name={}, nic={}", |
| 54 | + LogSanitizer.sanitize(name), LogSanitizer.sanitize(nic)); |
| 55 | + |
| 56 | + if (name == null || name.trim().isEmpty()) { |
| 57 | + return ResponseEntity.badRequest().body(Map.of("error", "Name is required")); |
| 58 | + } |
| 59 | + if (nic == null || nic.trim().isEmpty()) { |
| 60 | + return ResponseEntity.badRequest().body(Map.of("error", "NIC is required")); |
| 61 | + } |
| 62 | + |
| 63 | + Map<String, Object> result = criminalService.createCriminal( |
| 64 | + name.trim(), nic.trim(), riskLevel, crimeHistory, |
| 65 | + address, contactNumber, secondaryContact, |
| 66 | + dateOfBirth, gender, alias, status, photo); |
| 67 | + |
| 68 | + return ResponseEntity.ok(result); |
| 69 | + |
| 70 | + } catch (Exception e) { |
| 71 | + log.error("Criminal registration failed: {}", e.getMessage()); |
| 72 | + return ResponseEntity.internalServerError() |
| 73 | + .body(Map.of("error", "Registration failed: " + e.getMessage())); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get all criminals (summary list). |
| 79 | + */ |
| 80 | + @GetMapping |
| 81 | + public ResponseEntity<?> getAllCriminals() { |
| 82 | + try { |
| 83 | + List<Map<String, Object>> criminals = criminalService.getAllCriminals(); |
| 84 | + return ResponseEntity.ok(criminals); |
| 85 | + } catch (Exception e) { |
| 86 | + log.error("Failed to fetch criminals: {}", e.getMessage()); |
| 87 | + return ResponseEntity.internalServerError() |
| 88 | + .body(Map.of("error", "Failed to fetch criminals: " + e.getMessage())); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get full details for a specific criminal. |
| 94 | + */ |
| 95 | + @GetMapping("/{criminalId}") |
| 96 | + public ResponseEntity<?> getCriminalDetails(@PathVariable String criminalId) { |
| 97 | + try { |
| 98 | + Optional<Map<String, Object>> result = criminalService.getCriminalDetails(criminalId); |
| 99 | + if (result.isEmpty()) { |
| 100 | + return ResponseEntity.status(404) |
| 101 | + .body(Map.of("error", "Criminal not found: " + LogSanitizer.sanitize(criminalId))); |
| 102 | + } |
| 103 | + return ResponseEntity.ok(result.get()); |
| 104 | + } catch (Exception e) { |
| 105 | + log.error("Failed to fetch criminal {}: {}", LogSanitizer.sanitize(criminalId), e.getMessage()); |
| 106 | + return ResponseEntity.internalServerError() |
| 107 | + .body(Map.of("error", "Failed to fetch criminal details: " + e.getMessage())); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Update an existing criminal's profile data (with optional photo change). |
| 113 | + */ |
| 114 | + @PutMapping("/{criminalId}") |
| 115 | + public ResponseEntity<?> updateCriminal( |
| 116 | + @PathVariable String criminalId, |
| 117 | + @RequestParam(value = "name", required = false) String name, |
| 118 | + @RequestParam(value = "nic", required = false) String nic, |
| 119 | + @RequestParam(value = "photo", required = false) MultipartFile photo, |
| 120 | + @RequestParam(value = "risk_level", required = false) String riskLevel, |
| 121 | + @RequestParam(value = "crime_history", required = false) String crimeHistory, |
| 122 | + @RequestParam(value = "address", required = false) String address, |
| 123 | + @RequestParam(value = "contact_number", required = false) String contactNumber, |
| 124 | + @RequestParam(value = "secondary_contact", required = false) String secondaryContact, |
| 125 | + @RequestParam(value = "date_of_birth", required = false) String dateOfBirth, |
| 126 | + @RequestParam(value = "gender", required = false) String gender, |
| 127 | + @RequestParam(value = "alias", required = false) String alias, |
| 128 | + @RequestParam(value = "status", required = false) String status) { |
| 129 | + |
| 130 | + try { |
| 131 | + log.info("Criminal update requested for ID: {}", LogSanitizer.sanitize(criminalId)); |
| 132 | + |
| 133 | + Optional<Map<String, Object>> result = criminalService.updateCriminal( |
| 134 | + criminalId, name, nic, riskLevel, crimeHistory, |
| 135 | + address, contactNumber, secondaryContact, |
| 136 | + dateOfBirth, gender, alias, status, photo); |
| 137 | + |
| 138 | + if (result.isEmpty()) { |
| 139 | + return ResponseEntity.status(404) |
| 140 | + .body(Map.of("error", "Criminal not found: " + LogSanitizer.sanitize(criminalId))); |
| 141 | + } |
| 142 | + |
| 143 | + Map<String, Object> response = new java.util.LinkedHashMap<>(); |
| 144 | + response.put("message", "Criminal updated successfully"); |
| 145 | + response.put("criminal", result.get()); |
| 146 | + return ResponseEntity.ok(response); |
| 147 | + |
| 148 | + } catch (Exception e) { |
| 149 | + log.error("Criminal update failed for {}: {}", LogSanitizer.sanitize(criminalId), e.getMessage()); |
| 150 | + return ResponseEntity.internalServerError() |
| 151 | + .body(Map.of("error", "Update failed: " + e.getMessage())); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Delete a criminal record (cascades to suspect_photos, cleans up storage). |
| 157 | + */ |
| 158 | + @DeleteMapping("/{criminalId}") |
| 159 | + public ResponseEntity<?> deleteCriminal(@PathVariable String criminalId) { |
| 160 | + try { |
| 161 | + log.info("Criminal deletion requested for ID: {}", LogSanitizer.sanitize(criminalId)); |
| 162 | + |
| 163 | + boolean deleted = criminalService.deleteCriminal(criminalId); |
| 164 | + if (!deleted) { |
| 165 | + return ResponseEntity.status(404) |
| 166 | + .body(Map.of("error", "Criminal not found: " + LogSanitizer.sanitize(criminalId))); |
| 167 | + } |
| 168 | + |
| 169 | + return ResponseEntity.ok(Map.of("message", "Criminal deleted successfully", "criminal_id", criminalId)); |
| 170 | + |
| 171 | + } catch (Exception e) { |
| 172 | + log.error("Criminal deletion failed for {}: {}", LogSanitizer.sanitize(criminalId), e.getMessage()); |
| 173 | + return ResponseEntity.internalServerError() |
| 174 | + .body(Map.of("error", "Deletion failed: " + e.getMessage())); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments