RestLinkController.java 3.4 KB
Newer Older
Y
yadong.zhang 已提交
1 2 3
package com.zyd.blog.controller;

import com.github.pagehelper.PageInfo;
智布道's avatar
智布道 已提交
4
import com.zyd.blog.business.annotation.BussinessLog;
Y
yadong.zhang 已提交
5 6 7 8 9 10 11
import com.zyd.blog.business.entity.Link;
import com.zyd.blog.business.enums.LinkSourceEnum;
import com.zyd.blog.business.enums.ResponseStatus;
import com.zyd.blog.business.enums.TemplateKeyEnum;
import com.zyd.blog.business.service.MailService;
import com.zyd.blog.business.service.SysLinkService;
import com.zyd.blog.business.vo.LinkConditionVO;
智布道's avatar
智布道 已提交
12
import com.zyd.blog.framework.exception.ZhydLinkException;
Y
yadong.zhang 已提交
13 14
import com.zyd.blog.framework.object.PageResult;
import com.zyd.blog.framework.object.ResponseVO;
智布道's avatar
智布道 已提交
15
import com.zyd.blog.util.RegexUtils;
Y
yadong.zhang 已提交
16
import com.zyd.blog.util.ResultUtil;
17 18
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
Y
yadong.zhang 已提交
19 20 21 22 23 24 25 26 27 28 29
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 友情链接
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
30
 * @website https://docs.zhyd.me
Y
yadong.zhang 已提交
31 32 33 34 35 36 37 38 39 40 41
 * @date 2018/4/24 14:37
 * @since 1.0
 */
@RestController
@RequestMapping("/link")
public class RestLinkController {
    @Autowired
    private SysLinkService linkService;
    @Autowired
    private MailService mailService;

42
    @RequiresPermissions("links")
Y
yadong.zhang 已提交
43 44 45 46 47 48
    @PostMapping("/list")
    public PageResult list(LinkConditionVO vo) {
        PageInfo<Link> pageInfo = linkService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

49
    @RequiresPermissions("link:add")
Y
yadong.zhang 已提交
50
    @PostMapping(value = "/add")
智布道's avatar
智布道 已提交
51
    @BussinessLog("添加友情链接")
Y
yadong.zhang 已提交
52 53
    public ResponseVO add(Link link) {
        link.setSource(LinkSourceEnum.ADMIN);
智布道's avatar
智布道 已提交
54 55 56
        if (!RegexUtils.isUrl(link.getUrl())) {
            throw new ZhydLinkException("链接地址无效!");
        }
Y
yadong.zhang 已提交
57 58 59 60 61
        linkService.insert(link);
        mailService.send(link, TemplateKeyEnum.TM_LINKS);
        return ResultUtil.success("成功");
    }

62
    @RequiresPermissions(value = {"link:batchDelete", "link:delete"}, logical = Logical.OR)
Y
yadong.zhang 已提交
63
    @PostMapping(value = "/remove")
智布道's avatar
智布道 已提交
64
    @BussinessLog("删除友情链接")
Y
yadong.zhang 已提交
65 66 67 68 69 70 71 72 73 74
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            linkService.removeByPrimaryKey(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个友情链接");
    }

Y
yadong.zhang 已提交
75
    @RequiresPermissions("link:get")
Y
yadong.zhang 已提交
76
    @PostMapping("/get/{id}")
智布道's avatar
智布道 已提交
77
    @BussinessLog("获取友情链接详情")
Y
yadong.zhang 已提交
78 79 80 81
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.linkService.getByPrimaryKey(id));
    }

82
    @RequiresPermissions("link:edit")
Y
yadong.zhang 已提交
83
    @PostMapping("/edit")
智布道's avatar
智布道 已提交
84
    @BussinessLog("编辑友情链接")
Y
yadong.zhang 已提交
85
    public ResponseVO edit(Link link) {
智布道's avatar
智布道 已提交
86 87 88
        if (!RegexUtils.isUrl(link.getUrl())) {
            throw new ZhydLinkException("链接地址无效!");
        }
Y
yadong.zhang 已提交
89 90 91 92 93 94 95 96 97 98
        try {
            linkService.updateSelective(link);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("友情链接修改失败!");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }

}