diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java index 685d67c40f..d8d7541c76 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java @@ -1,7 +1,6 @@ package com.baeldung.mongodb.services; import java.io.IOException; -import java.util.Optional; import org.bson.BsonBinarySubType; import org.bson.types.Binary; @@ -19,20 +18,13 @@ public class PhotoService { private PhotoRepository photoRepo; public Photo getPhoto(String id) { - Optional result = photoRepo.findById(id); - return result.isPresent() ? result.get() : null; + return photoRepo.findById(id).get(); } - public String addPhoto(String title, MultipartFile file) { - String id = null; - try { - Photo photo = new Photo(title); - photo.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes())); - photo = photoRepo.insert(photo); - id = photo.getId(); - } catch (IOException e) { - return null; - } - return id; + public String addPhoto(String title, MultipartFile file) throws IOException { + Photo photo = new Photo(title); + photo.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes())); + photo = photoRepo.insert(photo); + return photo.getId(); } } diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java index dea6540973..ade1f7c73a 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java @@ -25,31 +25,19 @@ public class VideoService { @Autowired private GridFsOperations operations; - public Video getVideo(String id) { - Video video = null; + public Video getVideo(String id) throws IllegalStateException, IOException { GridFSFile file = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id))); - if (file != null) { - video = new Video(); - video.setTitle(file.getMetadata().get("title").toString()); - try { - video.setStream(operations.getResource(file).getInputStream()); - } catch (IOException e) { - return null; - } - } + Video video = new Video(); + video.setTitle(file.getMetadata().get("title").toString()); + video.setStream(operations.getResource(file).getInputStream()); return video; } - public String addVideo(String title, MultipartFile file) { + public String addVideo(String title, MultipartFile file) throws IOException { DBObject metaData = new BasicDBObject(); metaData.put("type", "video"); metaData.put("title", title); - ObjectId id; - try { - id = gridFsTemplate.store(file.getInputStream(), file.getName(), file.getContentType(), metaData); - } catch (IOException e) { - return null; - } + ObjectId id = gridFsTemplate.store(file.getInputStream(), file.getName(), file.getContentType(), metaData); return id.toString(); } diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java index fffb6ec320..4d5746f0d8 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java @@ -1,5 +1,6 @@ package com.baeldung.mongodb.web; +import java.io.IOException; import java.util.Base64; import org.springframework.beans.factory.annotation.Autowired; @@ -23,13 +24,9 @@ public class PhotoController { @GetMapping("/photos/{id}") public String getPhoto(@PathVariable String id, Model model) { Photo photo = photoService.getPhoto(id); - if (photo != null) { - model.addAttribute("title", photo.getTitle()); - model.addAttribute("image", Base64.getEncoder().encodeToString(photo.getImage().getData())); - return "photos"; - } - model.addAttribute("message", "Photo not found"); - return "index"; + model.addAttribute("title", photo.getTitle()); + model.addAttribute("image", Base64.getEncoder().encodeToString(photo.getImage().getData())); + return "photos"; } @GetMapping("/photos/upload") @@ -39,12 +36,8 @@ public class PhotoController { } @PostMapping("/photos/add") - public String addPhoto(@RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model) { + public String addPhoto(@RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model) throws IOException { String id = photoService.addPhoto(title, image); - if (id == null) { - model.addAttribute("message", "Error Occurred"); - return "index"; - } return "redirect:/photos/" + id; } } diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java index e5fda0cc64..313ce9e650 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java @@ -24,29 +24,17 @@ public class VideoController { private VideoService videoService; @GetMapping("/videos/{id}") - public String getVideo(@PathVariable String id, Model model) { + public String getVideo(@PathVariable String id, Model model) throws IllegalStateException, IOException { Video video = videoService.getVideo(id); - if (video != null) { - model.addAttribute("title", video.getTitle()); - model.addAttribute("url", "/videos/stream/" + id); - return "videos"; - } - model.addAttribute("message", "Video not found"); - return "index"; + model.addAttribute("title", video.getTitle()); + model.addAttribute("url", "/videos/stream/" + id); + return "videos"; } @GetMapping("/videos/stream/{id}") - public void streamVideo(@PathVariable String id, HttpServletResponse response) { + public void streamVideo(@PathVariable String id, HttpServletResponse response) throws IllegalStateException, IOException { Video video = videoService.getVideo(id); - if (video != null) { - try { - FileCopyUtils.copy(video.getStream(), response.getOutputStream()); - } catch (IOException e) { - response.setStatus(500); - } - } else { - response.setStatus(404); - } + FileCopyUtils.copy(video.getStream(), response.getOutputStream()); } @GetMapping("/videos/upload") @@ -56,12 +44,8 @@ public class VideoController { } @PostMapping("/videos/add") - public String addVideo(@RequestParam("title") String title, @RequestParam("file") MultipartFile file, Model model) { + public String addVideo(@RequestParam("title") String title, @RequestParam("file") MultipartFile file, Model model) throws IOException { String id = videoService.addVideo(title, file); - if (id == null) { - model.addAttribute("message", "Error Occurred"); - return "index"; - } return "redirect:/videos/" + id; } }