Enforce ResponseEntity returns on controllers

This commit is contained in:
Alex Gryczewski 2023-09-12 12:09:33 -04:00
parent 8e89720f91
commit da118b40d3
2 changed files with 23 additions and 13 deletions

View File

@ -19,8 +19,10 @@ public class GalleryController {
private GalleryRepository galleryRepository; private GalleryRepository galleryRepository;
@GetMapping() @GetMapping()
public List<Gallery> getAllGalleries() { public ResponseEntity<List<Gallery>> getAllGalleries() {
return galleryRepository.findAll(); List<Gallery> galleries = galleryRepository.findAll();
return ResponseEntity.ok(galleries);
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -31,8 +33,10 @@ public class GalleryController {
} }
@GetMapping("/server/{id}") @GetMapping("/server/{id}")
public List<Gallery> getGalleriesByServer(@PathVariable(value = "id") String serverId) { public ResponseEntity<List<Gallery>> getGalleriesByServer(@PathVariable(value = "id") String serverId) {
return galleryRepository.findAllByServer(serverId); List<Gallery> galleries = galleryRepository.findAllByServer(serverId);
return ResponseEntity.ok(galleries);
} }
@GetMapping("/server/{serverId}/channel/{channelId}") @GetMapping("/server/{serverId}/channel/{channelId}")
@ -43,8 +47,10 @@ public class GalleryController {
} }
@PostMapping() @PostMapping()
public Gallery createGallery(@Valid @RequestBody Gallery gallery) { public ResponseEntity<Gallery> createGallery(@Valid @RequestBody Gallery gallery) {
return galleryRepository.save(gallery); Gallery createdGallery = galleryRepository.save(gallery);
return ResponseEntity.ok(createdGallery);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
@ -63,7 +69,7 @@ public class GalleryController {
return ResponseEntity.ok(updatedGallery); return ResponseEntity.ok(updatedGallery);
} }
@DeleteMapping() @DeleteMapping("/{id}")
public ResponseEntity<Map<String, Boolean>> deleteGallery(@PathVariable(value = "id") Long galleryId) throws ResourceNotFoundException { public ResponseEntity<Map<String, Boolean>> deleteGallery(@PathVariable(value = "id") Long galleryId) throws ResourceNotFoundException {
Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId)); Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId));

View File

@ -20,8 +20,10 @@ public class OutfitController {
private OutfitRepository outfitRepository; private OutfitRepository outfitRepository;
@GetMapping() @GetMapping()
public List<Outfit> getAllOutfits() { public ResponseEntity<List<Outfit>> getAllOutfits() {
return outfitRepository.findAll(); List<Outfit> outfits = outfitRepository.findAll();
return ResponseEntity.ok(outfits);
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -39,8 +41,10 @@ public class OutfitController {
} }
@PostMapping() @PostMapping()
public Outfit createOutfit(@Valid @RequestBody Outfit outfit) { public ResponseEntity<Outfit> createOutfit(@Valid @RequestBody Outfit newOutfit) {
return outfitRepository.save(outfit); Outfit outfit = outfitRepository.save(newOutfit);
return ResponseEntity.ok(outfit);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
@ -66,12 +70,12 @@ public class OutfitController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Map<String, Boolean> deleteOutfit(@PathVariable(value = "id") String outfitId) throws ResourceNotFoundException { public ResponseEntity<Map<String, Boolean>> deleteOutfit(@PathVariable(value = "id") String outfitId) throws ResourceNotFoundException {
Outfit outfit = outfitRepository.findById(outfitId).orElseThrow(() -> new ResourceNotFoundException("Outfit not found for id :: " + outfitId)); Outfit outfit = outfitRepository.findById(outfitId).orElseThrow(() -> new ResourceNotFoundException("Outfit not found for id :: " + outfitId));
outfitRepository.delete(outfit); outfitRepository.delete(outfit);
Map<String, Boolean> response = new HashMap<>(); Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE); response.put("deleted", Boolean.TRUE);
return response; return ResponseEntity.ok(response);
} }
} }