Improve ImageController error handling
This commit is contained in:
@@ -7,6 +7,7 @@ import de.htwsaar.webshop.service.ValidatorService;
|
|||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -34,14 +35,22 @@ public class ImageController {
|
|||||||
public ResponseEntity<List<ImageModel>> getAll(HttpServletRequest request,
|
public ResponseEntity<List<ImageModel>> getAll(HttpServletRequest request,
|
||||||
@RequestParam(value = PARAM_ARTICLE_ID) Long articleId) {
|
@RequestParam(value = PARAM_ARTICLE_ID) Long articleId) {
|
||||||
logRequest(request);
|
logRequest(request);
|
||||||
return ResponseEntity.ok(imageService.from(imageService.getImagesByArticleId(articleId)));
|
List<Image> images = imageService.getImagesByArticleId(articleId);
|
||||||
|
if(images.isEmpty()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(imageService.from(images));
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "application/json")
|
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "application/json")
|
||||||
public ResponseEntity<ImageModel> getFirst(HttpServletRequest request,
|
public ResponseEntity<ImageModel> getFirst(HttpServletRequest request,
|
||||||
@RequestParam(value = PARAM_ARTICLE_ID) Long articleId) {
|
@RequestParam(value = PARAM_ARTICLE_ID) Long articleId) {
|
||||||
logRequest(request);
|
logRequest(request);
|
||||||
return ResponseEntity.ok(imageService.from(imageService.getImageByArticleId(articleId)));
|
Image image = imageService.getImageByArticleId(articleId);
|
||||||
|
if(image == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
|
}
|
||||||
|
return ResponseEntity.ok(imageService.from(image));
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json")
|
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json")
|
||||||
|
|||||||
Reference in New Issue
Block a user