@Override public BlogTO createBlog(BlogTO blogTO) { CustomerTO customer = customerService.getCustomer(blogTO.getCustomer().getId()); blogTO.setCustomer(customer); BlogDO savedDO = blogRepository.save(blogConverter.convertTOIntoEntity(blogTO, new BlogDO())); return blogConverter.convertEntityIntoTO(savedDO, blogTO); }
@Override public BlogTO getBlog(Integer id) { return Optional.ofNullable(blogRepository.findOne(id)) .map(blog -> this.setVoteCounter(blog)) .map(blog -> blogConverter.convertEntityIntoTO(blog, new BlogTO())) .orElseThrow(() -> new IllegalArgumentException("There's no blog with the ID" + id)); }
@Override public List<BlogTO> getAll() { List<BlogDO> blogs = (List<BlogDO>) blogRepository.findAll(); return blogs .parallelStream() .map(blog -> this.setVoteCounter(blog)) .map(blog -> blogConverter.convertEntityIntoTO(blog, new BlogTO())) .collect(Collectors.toList()); }
@Override public BlogTO updateBlog(BlogTO blogTO) { BlogDO blogDO = blogRepository.findOne(blogTO.getId()); blogConverter.convertTOIntoEntity(blogTO, blogDO); return blogConverter.convertEntityIntoTO(blogRepository.save(blogDO), blogTO); }