Skip to content

Instantly share code, notes, and snippets.

@h-hub
Created July 15, 2019 15:50
Show Gist options
  • Save h-hub/1182e038400a191fe8f36db3d52a93b3 to your computer and use it in GitHub Desktop.
Save h-hub/1182e038400a191fe8f36db3d52a93b3 to your computer and use it in GitHub Desktop.
@Controller
public class BlogController {
private static final String BLOG_IMAGES = "blogImages";
private static final String TOMCAT_HOME_PROPERTY = "catalina.home";
private static final String TOMCAT_HOME_PATH = System.getProperty(TOMCAT_HOME_PROPERTY);
private static final String BLOG_IMAGES_PATH = TOMCAT_HOME_PATH + File.separator + BLOG_IMAGES;
private static final File BLOG_IMAGES_DIR = new File(BLOG_IMAGES_PATH);
private static final String BLOG_IMAGES_DIR_ABSOLUTE_PATH = BLOG_IMAGES_DIR.getAbsolutePath() + File.separator;
@Autowired
private BlogPostService blogPostService;
@Autowired
BlogMapper blogMapper;
@GetMapping("/add_blog_post")
public String showBlogPostForm(Model model) throws Exception {
BlogDto blogDto = new BlogDto();
model.addAttribute("blogDto", blogDto);
return "blog/add_blog_post";
}
@PostMapping("/add_blog_post")
public String addBlogPost(@ModelAttribute @Valid BlogDto blogDto, BindingResult result, Model m) throws Exception {
if (blogDto.getStatus() == BlogStatus.READY) {
m.addAttribute("globalError", "Unable to save");
m.addAttribute("blogDto", blogDto);
return "blog/add_blog_post";
}
if (result.hasErrors()) {
if (result.getGlobalError() != null) {
m.addAttribute("globalError", result.getGlobalError().getDefaultMessage());
}
m.addAttribute("error", "Please check the form for errors");
m.addAttribute("blogDto", blogDto);
return "blog/add_blog_post";
}
MultipartFile file = blogDto.getImage();
String imagePath = "";
if (!file.isEmpty()) {
createBlogImagesDirIfNeeded();
imagePath = blogPostService.createImage(
(new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date())) + file.getOriginalFilename(), file,
BLOG_IMAGES_DIR_ABSOLUTE_PATH);
}
BlogDto savedBlogDto = blogPostService.createBlogPost(blogDto, imagePath);
if (blogDto.isPreview()) {
return "redirect:preview_blog?blogId=" + savedBlogDto.getId();
} else if (blogDto.isSave()) {
m.addAttribute("message", "Draft was saved successfully");
m.addAttribute("blogDto", savedBlogDto);
} else {
m.addAttribute("message", "Blog post is ready to be published after review.");
m.addAttribute("blogDto", savedBlogDto);
}
return "blog/add_blog_post";
}
@GetMapping("/image/{imageName}")
public @ResponseBody byte[] getImage(@PathVariable("imageName") String imageName) throws IOException {
createBlogImagesDirIfNeeded();
File serverFile = new File(BLOG_IMAGES_DIR_ABSOLUTE_PATH + imageName);
File file = ResourceUtils.getFile("classpath:static/image-not-found.png");
try {
return Files.readAllBytes(serverFile.toPath());
} catch (IOException e) {
return Files.readAllBytes(file.toPath());
}
}
private void createBlogImagesDirIfNeeded() {
if (!BLOG_IMAGES_DIR.exists()) {
BLOG_IMAGES_DIR.mkdirs();
}
}
@GetMapping("/preview_blog")
public String previewBlog(@RequestParam("blogId") Integer blogId, Model m) throws Exception {
BlogDto blogDto = blogPostService.findByIdtoShow(blogId);
m.addAttribute("blogDto", blogDto);
return "blog/view_blog_post";
}
@GetMapping("/edit_blog")
public String editBlog(@RequestParam("blogId") Integer blogId, Model m) throws Exception {
BlogDto blogDto = blogPostService.findByIdtoShow(blogId);
m.addAttribute("blogDto", blogDto);
return "blog/add_blog_post";
}
@GetMapping("/blog_post_list")
public String showPostList(Model model) {
List<BlogDto> blogPostList = blogPostService.getAll();
model.addAttribute("blogPostList", blogPostList);
return "blog/blog_post_list";
}
@PostMapping("/change_blog_post_status")
public String changeBlogStatus(@RequestParam("blogPostId") Integer blogPostId,
@RequestParam("approve") Boolean approve, @RequestParam("pubToDate") String date) throws ParseException {
blogPostService.changeStatus(blogPostId, approve, date);
return "redirect:blog_post_list";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment