-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulletController.java
More file actions
96 lines (86 loc) · 3.62 KB
/
Copy pathBulletController.java
File metadata and controls
96 lines (86 loc) · 3.62 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
package com.crimeLink.analyzer.controller;
import com.crimeLink.analyzer.dto.BulletAddDTO;
import com.crimeLink.analyzer.dto.BulletResponseDTO;
import com.crimeLink.analyzer.dto.BulletUpdateDTO;
import com.crimeLink.analyzer.entity.Bullet;
import com.crimeLink.analyzer.service.BulletService;
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/bullet")
@RequiredArgsConstructor
public class BulletController {
private final BulletService bulletService;
@PostMapping("/add-bullet")
public ResponseEntity<?> addBullet(@RequestBody BulletAddDTO dto) {
try {
Bullet bullet = bulletService.addBullet(dto);
return ResponseEntity.status(HttpStatus.CREATED).body(bullet);
} 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("/bullet-update/{bulletId}")
public ResponseEntity<?> updateBullet(
@PathVariable Integer bulletId,
@RequestBody BulletUpdateDTO dto) {
try {
Bullet bullet = bulletService.updateBullet(bulletId, dto);
return ResponseEntity.ok(bullet);
} 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<?> getAllBullets() {
try {
List<Bullet> bullets = bulletService.getAllBullets();
return ResponseEntity.ok(bullets);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(createErrorResponse("Failed to fetch bullets"));
}
}
@GetMapping("/all-with-details")
public ResponseEntity<?> getAllBulletsWithDetails() {
try {
List<BulletResponseDTO> bullets = bulletService.getAllBulletsWithDetails();
return ResponseEntity.ok(bullets);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(createErrorResponse("Failed to fetch bullets with details: " + e.getMessage()));
}
}
@GetMapping("/{bulletId}")
public ResponseEntity<?> getBulletById(@PathVariable Integer bulletId) {
try {
Bullet bullet = bulletService.getBulletById(bulletId);
return ResponseEntity.ok(bullet);
} 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 bullet"));
}
}
private Map<String, String> createErrorResponse(String message) {
Map<String, String> response = new HashMap<>();
response.put("error", message);
return response;
}
}