-
Notifications
You must be signed in to change notification settings - Fork 0
Implement bullet management with controller, service, and DTOs #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9e20fa2
35d1bd3
1cb5ba5
65a29b1
a0201cc
47a9983
01e8260
68409e3
e0182ff
fe36bed
ab36153
8363579
00dc00e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| 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 | ||
| @CrossOrigin(origins = "*") | ||
| 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,7 @@ | ||
| package com.crimeLink.analyzer.controller; | ||
|
|
||
|
|
||
| import com.crimeLink.analyzer.entity.User; | ||
| import com.crimeLink.analyzer.service.UserService; | ||
| import com.crimeLink.analyzer.service.WeaponIssueService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
@@ -19,11 +16,13 @@ public UserController(UserService service) { | |
| this.service = service; | ||
| } | ||
|
|
||
| @GetMapping("/field-officers") | ||
| public List<User> getFieldOfficers() { | ||
| return service.getFieldOfficers(); | ||
| } | ||
|
Comment on lines
18
to
21
|
||
|
|
||
| @GetMapping("/all-officers") | ||
| public List<User> getAllOfficers() { | ||
| return service.getAllOfficers(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.crimeLink.analyzer.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class BulletAddDTO { | ||
| private String bulletType; | ||
| private Integer numberOfMagazines; | ||
| private String remarks; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.crimeLink.analyzer.dto; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Data | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class BulletResponseDTO { | ||
| private Integer bulletId; | ||
| private String bulletType; | ||
| private Integer numberOfMagazines; | ||
| private String remarks; | ||
| private String registerDate; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.crimeLink.analyzer.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class BulletUpdateDTO { | ||
| private String bulletType; | ||
| private Integer numberOfMagazines; | ||
| private String remarks; | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.crimeLink.analyzer.entity; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| @Table(name = "bullets") | ||
| @Data | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class Bullet { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "bullet_id") | ||
| private Integer bulletId; | ||
|
|
||
| @Column(name = "bullet_type", nullable = false) | ||
| private String bulletType; | ||
|
|
||
| @Column(name = "number_of_magazines", nullable = false) | ||
| private Integer numberOfMagazines; | ||
|
|
||
| @Column(name = "register_date", updatable = false) | ||
| private LocalDateTime registerDate; | ||
|
|
||
| @Column(name = "updated_date") | ||
| private LocalDateTime updatedDate; | ||
|
|
||
| @Column(name = "remarks") | ||
| private String remarks; | ||
|
|
||
| @PrePersist | ||
| void onCreate() { | ||
| registerDate = LocalDateTime.now(); | ||
| updatedDate = LocalDateTime.now(); | ||
| } | ||
|
|
||
| @PreUpdate | ||
| void onUpdate() { | ||
| updatedDate = LocalDateTime.now(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.crimeLink.analyzer.repository; | ||
|
|
||
| import com.crimeLink.analyzer.entity.Bullet; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| @Repository | ||
| public interface BulletRepository extends JpaRepository<Bullet, Integer> { | ||
|
|
||
| Optional<Bullet> findByBulletType(String bulletType); | ||
|
|
||
| boolean existsByBulletTypeIgnoreCase(String bulletType); | ||
|
Comment on lines
+10
to
+14
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.crimeLink.analyzer.service; | ||
|
|
||
| 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 java.util.List; | ||
|
|
||
| public interface BulletService { | ||
| Bullet addBullet(BulletAddDTO dto); | ||
| Bullet updateBullet(Integer bulletId, BulletUpdateDTO dto); | ||
| List<Bullet> getAllBullets(); | ||
| List<BulletResponseDTO> getAllBulletsWithDetails(); | ||
| Bullet getBulletById(Integer bulletId); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception handler calls
e.printStackTrace()and also returnse.getMessage()to clients. This can leak implementation details and makes logs inconsistent with the rest of the app; prefer structured logging (e.g., SLF4J logger) and return a generic error message while logging the full exception server-side.