feat(markers): get by id
This commit is contained in:
@@ -115,4 +115,11 @@ public class GlobalExceptionHandler {
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(response);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MarkerNotFound.class)
|
||||
public ResponseEntity<APIResponse<Void>> handleMarkerNotFound(MarkerNotFound ex) {
|
||||
logger.error("MarkerNotFound: {}", ex.getMessage());
|
||||
APIResponse<Void> response = new APIResponse<>(ERROR, ex.getMessage(), null);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,15 @@ public class MarkerController {
|
||||
);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<APIResponse<MarkerResponse>> getMarkerById(@PathVariable Long id) {
|
||||
Marker marker = markerService.getMarkerById(id);
|
||||
|
||||
return ResponseEntity.ok(
|
||||
new APIResponse<>(SUCCESS, "Marker retrieved successfully", convertToDTO(marker))
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("")
|
||||
public ResponseEntity<APIResponse<MarkerResponse>> createMarker(@Valid @RequestBody MarkerCreateRequest request) {
|
||||
Marker marker = markerService.create(request.message());
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.ddf.vodsystem.exceptions;
|
||||
|
||||
public class MarkerNotFound extends RuntimeException {
|
||||
public MarkerNotFound(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.ddf.vodsystem.services;
|
||||
import com.ddf.vodsystem.entities.Marker;
|
||||
import com.ddf.vodsystem.entities.Stream;
|
||||
import com.ddf.vodsystem.entities.User;
|
||||
import com.ddf.vodsystem.exceptions.MarkerNotFound;
|
||||
import com.ddf.vodsystem.exceptions.NotAuthenticated;
|
||||
import com.ddf.vodsystem.exceptions.NotStreaming;
|
||||
import com.ddf.vodsystem.repositories.MarkerRepository;
|
||||
@@ -25,6 +26,21 @@ public class MarkerService {
|
||||
this.markerRepository = markerRepository;
|
||||
}
|
||||
|
||||
public Marker getMarkerById(Long id) {
|
||||
Optional<Marker> marker = markerRepository.findById(id);
|
||||
|
||||
if (marker.isEmpty()) {
|
||||
throw new MarkerNotFound("Marker does not exist");
|
||||
}
|
||||
|
||||
Optional<User> user = userService.getLoggedInUser();
|
||||
if (user.isEmpty() || !user.get().equals(marker.get().getUser())) {
|
||||
throw new NotAuthenticated("User not authenticated for this marker");
|
||||
}
|
||||
|
||||
return marker.get();
|
||||
}
|
||||
|
||||
public List<Marker> getUserMarkers() {
|
||||
Optional<User> user = userService.getLoggedInUser();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user