阳光沙滩博客系统-管理中心友情连接模块

# 添加友情连接

接口

@PreAuthorize("@permission.admin()")
@PostMapping
public ResponseResult addFriendsLink(@RequestBody FriendLink friendLink) {
    return friendLinkService.addFriendLink(friendLink);
}
1
2
3
4
5

添加友情链接实现

/**
     * 添加友情连接
     *
     * @param friendLink
     * @return
     */
    @Override
    public ResponseResult addFriendLink(FriendLink friendLink) {
        //判断数据
        String url = friendLink.getUrl();
        if (TextUtils.isEmpty(url)) {
            return ResponseResult.FAILED("链接Url不可以为空.");
        }
        String logo = friendLink.getLogo();
        if (TextUtils.isEmpty(logo)) {
            return ResponseResult.FAILED("logo不可以为空.");
        }
        String name = friendLink.getName();
        if (TextUtils.isEmpty(name)) {
            return ResponseResult.FAILED("对方网站名不可以为空.");
        }
        //补全数据
        friendLink.setId(idWorker.nextId() + "");
        friendLink.setUpdateTime(new Date());
        friendLink.setCreateTime(new Date());
        //保存数据
        friendLinkDao.save(friendLink);
        //返回结果
        return ResponseResult.FAILED("添加成功.");
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 删除友情连接

接口

@PreAuthorize("@permission.admin()")
@DeleteMapping("/{friendLinkId}")
public ResponseResult deleteFriendLink(@PathVariable("friendLinkId") String friendLinkId) {
    return friendLinkService.deleteFriendLink(friendLinkId);
}
1
2
3
4
5

删除接口实现

@Override
public ResponseResult deleteFriendLink(String friendLinkId) {
    int result = friendLinkDao.deleteAllById(friendLinkId);
    if (result == 0) {
        return ResponseResult.FAILED("删除失败.");
    }
    return ResponseResult.SUCCESS("删除成功.");
}
1
2
3
4
5
6
7
8

# 更新友情链接

接口

@PreAuthorize("@permission.admin()")
@PutMapping("/{friendLinkId}")
public ResponseResult updateFriendLink(@PathVariable("friendLinkId") String friendLinkId,
                                       @RequestBody FriendLink friendLink) {
    return friendLinkService.updateFriendLink(friendLinkId, friendLink);
}
1
2
3
4
5
6

更新接口实现

/**
     * 更新内容有什么:
     * logo
     * 对方网站的名称
     * url
     * order
     *
     * @param friendLinkId
     * @param friendLink
     * @return
     */
@Override
public ResponseResult updateFriendLink(String friendLinkId, FriendLink friendLink) {
    FriendLink friendLinkFromDb = friendLinkDao.findOneById(friendLinkId);
    if (friendLinkFromDb == null) {
        return ResponseResult.FAILED("更新失败.");
    }
    String logo = friendLink.getLogo();
    if (!TextUtils.isEmpty(logo)) {
        friendLinkFromDb.setLogo(logo);
    }
    String name = friendLink.getName();
    if (!TextUtils.isEmpty(name)) {
        friendLinkFromDb.setName(name);
    }
    String url = friendLink.getUrl();
    if (!TextUtils.isEmpty(url)) {
        friendLinkFromDb.setUrl(url);
    }
    friendLinkFromDb.setOrder(friendLink.getOrder());
    friendLinkFromDb.setUpdateTime(new Date());
    //保存数据
    friendLinkDao.save(friendLinkFromDb);
    return ResponseResult.SUCCESS("更新成功.");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

# 获取友情链接列表

接口

@PreAuthorize("@permission.admin()")
@GetMapping("/list/{page}/{size}")
public ResponseResult listFriendLinks(@PathVariable("page") int page, @PathVariable("size") int size) {
    return friendLinkService.listFriendLinks(page, size);
}
1
2
3
4
5

实现

@Override
public ResponseResult listFriendLinks(int page, int size) {
    if (page < Constants.Page.DEFAULT_PAGE) {
        page = Constants.Page.DEFAULT_PAGE;
    }
    if (size < Constants.Page.DEFAULT_SIZE) {
        size = Constants.Page.DEFAULT_SIZE;
    }
    //创建条件
    Sort sort = new Sort(Sort.Direction.DESC, "createTime", "order");
    Pageable pageable = PageRequest.of(page - 1, size, sort);
    Page<FriendLink> all = friendLinkDao.findAll(pageable);
    return ResponseResult.SUCCESS("获取列表成功.").setData(all);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 获取友情链接

接口

@PreAuthorize("@permission.admin()")
@GetMapping("/{friendLinkId}")
public ResponseResult getFriendLink(@PathVariable("friendLinkId") String friendLinkId) {
    return friendLinkService.getFriendLink(friendLinkId);
}
1
2
3
4
5

获取友情链接实现

@Override
public ResponseResult getFriendLink(String friendLinkId) {
    FriendLink friendLink = friendLinkDao.findOneById(friendLinkId);
    if (friendLink == null) {
        return ResponseResult.FAILED("友情链接不存");
    }
    return ResponseResult.SUCCESS("获取成功").setData(friendLink);
}
1
2
3
4
5
6
7
8
上次更新: 2022/03/28, 23:04:38