-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeaponController.java
More file actions
102 lines (91 loc) · 3.9 KB
/
Copy pathWeaponController.java
File metadata and controls
102 lines (91 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.crimeLink.analyzer.controller;
import com.crimeLink.analyzer.dto.WeaponAddDTO;
import com.crimeLink.analyzer.dto.WeaponResponseDTO;
import com.crimeLink.analyzer.dto.WeaponUpdateDTO;
import com.crimeLink.analyzer.entity.Weapon;
import com.crimeLink.analyzer.service.WeaponService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/weapon")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class WeaponController {
private final WeaponService weaponService;
@PostMapping("/add-weapon")
public ResponseEntity<?> addWeapon(@RequestBody WeaponAddDTO dto) {
try {
Weapon weapon = weaponService.addWeapon(dto);
return ResponseEntity.status(HttpStatus.CREATED).body(weapon);
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(createErrorResponse(e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(createErrorResponse("An unexpected error occurred"));
}
}
@PutMapping("/weapon-update/{serialNumber}")
public ResponseEntity<?> updateWeapon(
@PathVariable String serialNumber,
@RequestBody WeaponUpdateDTO dto) {
try {
Weapon weapon = weaponService.updateWeapon(serialNumber, dto);
return ResponseEntity.ok(weapon);
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(createErrorResponse(e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(createErrorResponse("An unexpected error occurred"));
}
}
@GetMapping("/all")
public ResponseEntity<?> getAllWeapons() {
try {
List<Weapon> weapons = weaponService.getAllWeapons();
return ResponseEntity.ok(weapons);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(createErrorResponse("Failed to fetch weapons"));
}
}
@GetMapping("/all-with-details")
public ResponseEntity<?> getAllWeaponsWithDetails() {
try {
List<WeaponResponseDTO> weapons = weaponService.getAllWeaponsWithDetails();
return ResponseEntity.ok(weapons);
} catch (Exception e) {
e.printStackTrace(); // Log the full stack trace
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(createErrorResponse("Failed to fetch weapons with details: " + e.getMessage()));
}
}
@GetMapping("/{serialNumber}")
public ResponseEntity<?> getWeaponBySerial(@PathVariable String serialNumber) {
try {
Weapon weapon = weaponService.getWeaponBySerial(serialNumber);
return ResponseEntity.ok(weapon);
} catch (RuntimeException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(createErrorResponse(e.getMessage()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(createErrorResponse("Failed to fetch weapon"));
}
}
@GetMapping("/officer/{officerId}")
public List<Weapon> getWeaponsIssuedToOfficer(@PathVariable Integer officerId) {
return weaponService.getWeaponsIssuedToOfficer(officerId);
}
private Map<String, String> createErrorResponse(String message) {
Map<String, String> response = new HashMap<>();
response.put("error", message);
return response;
}
}