Search in sources :

Example 1 with ZhydArticleException

use of com.zyd.blog.framework.exception.ZhydArticleException in project OneBlog by zhangyd-c.

the class BizArticleServiceImpl method publish.

/**
 * 发布文章
 *
 * @param article
 * @param tags
 * @param file
 * @return
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean publish(Article article, Long[] tags, MultipartFile file) {
    if (null == tags || tags.length <= 0) {
        throw new ZhydArticleException("请至少选择一个标签");
    }
    if (null != file) {
        FileUploader uploader = new GlobalFileUploader();
        VirtualFile virtualFile = uploader.upload(file, FileUploadType.COVER_IMAGE.getPath(), true);
        // 保存封面图片
        article.setCoverImage(virtualFile.getFilePath());
    }
    Long articleId = null;
    if ((articleId = article.getId()) != null) {
        this.updateSelective(article);
    } else {
        article.setUserId(SessionUtil.getUser().getId());
        articleId = this.insert(article).getId();
    }
    if (articleId != null) {
        articleTagsService.removeByArticleId(articleId);
        articleTagsService.insertList(tags, articleId);
    }
    return true;
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) ZhydArticleException(com.zyd.blog.framework.exception.ZhydArticleException) FileUploader(com.zyd.blog.file.FileUploader) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ZhydArticleException

use of com.zyd.blog.framework.exception.ZhydArticleException in project OneBlog by zhangyd-c.

the class BizArticleServiceImpl method doPraise.

/**
 * 文章点赞
 *
 * @param id
 */
@Override
@RedisCache(flush = true)
public void doPraise(Long id) {
    String ip = IpUtil.getRealIp(RequestHolder.getRequest());
    String key = ip + "_doPraise_" + id;
    ValueOperations<String, Object> operations = redisTemplate.opsForValue();
    if (redisTemplate.hasKey(key)) {
        throw new ZhydArticleException("一个小时只能点赞一次哈,感谢支持~~");
    }
    User user = SessionUtil.getUser();
    BizArticleLove love = new BizArticleLove();
    if (null != user) {
        love.setUserId(user.getId());
    }
    love.setArticleId(id);
    love.setUserIp(IpUtil.getRealIp(RequestHolder.getRequest()));
    love.setLoveTime(new Date());
    love.setCreateTime(new Date());
    love.setUpdateTime(new Date());
    bizArticleLoveMapper.insert(love);
    operations.set(key, id, 1, TimeUnit.HOURS);
}
Also used : ZhydArticleException(com.zyd.blog.framework.exception.ZhydArticleException) User(com.zyd.blog.business.entity.User) RedisCache(com.zyd.blog.business.annotation.RedisCache)

Aggregations

ZhydArticleException (com.zyd.blog.framework.exception.ZhydArticleException)2 RedisCache (com.zyd.blog.business.annotation.RedisCache)1 User (com.zyd.blog.business.entity.User)1 FileUploader (com.zyd.blog.file.FileUploader)1 VirtualFile (com.zyd.blog.file.entity.VirtualFile)1 GlobalFileUploader (com.zyd.blog.plugin.file.GlobalFileUploader)1 Transactional (org.springframework.transaction.annotation.Transactional)1