-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleController.java
More file actions
100 lines (86 loc) · 3.61 KB
/
Copy pathVehicleController.java
File metadata and controls
100 lines (86 loc) · 3.61 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
package com.crimeLink.analyzer.controller;
import com.crimeLink.analyzer.entity.Vehicle;
import com.crimeLink.analyzer.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.util.List;
@RestController
@RequestMapping("/api/vehicles")
public class VehicleController {
@Autowired
private VehicleService vehicleService;
@GetMapping
public ResponseEntity<List<Vehicle>> getVehicles(){
try {
List<Vehicle> vehicles = vehicleService.getAllVehicles();
return ResponseEntity.ok(vehicles);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@PostMapping
public ResponseEntity<Vehicle> addVehicle(@RequestBody Vehicle vehicle){
try {
Vehicle savedVehicle = vehicleService.addVehicle(vehicle);
return ResponseEntity.status(HttpStatus.CREATED).body(savedVehicle);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@GetMapping("/{id}") // ← Path variable: /api/vehicles/5
public ResponseEntity<Vehicle> getVehicleById(@PathVariable Long id) {
try {
Vehicle vehicle = vehicleService.getVehicleById(id);
if (vehicle != null) {
return ResponseEntity.ok(vehicle);
}
return ResponseEntity.notFound().build();
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteVehicle(@PathVariable Long id) {
try {
vehicleService.deleteVehicle(id);
return ResponseEntity.noContent().build();
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@PutMapping("/{id}")
public ResponseEntity<Vehicle> updateVehicle(@PathVariable Long id,@RequestBody Vehicle vehicleDetails){
try{
Vehicle updatedVehicle = vehicleService.updateVehicle(id, vehicleDetails);
if(updatedVehicle != null){
return ResponseEntity.ok(updatedVehicle);
}
return ResponseEntity.notFound().build();
}catch (Exception e){
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
@GetMapping(value = "/report/pdf", produces = MediaType.APPLICATION_PDF_VALUE)
public ResponseEntity<byte[]> getPlateRegistryReportPdf(
@RequestParam(value = "start", required = false) String start,
@RequestParam(value = "end", required = false) String end
) {
byte[] pdfBytes = vehicleService.generatePlateRegistryReportPdf(start, end);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDispositionFormData("attachment", "plate-registry-report.pdf");
return new ResponseEntity<>(pdfBytes, headers, HttpStatus.OK);
}
}