Feat: [后台]:完成批量删除,新增书签接口]

This commit is contained in:
fanxb 2019-07-13 17:54:03 +08:00
parent da10b9daa4
commit 2e9466ad6b
6 changed files with 137 additions and 13 deletions

View File

@ -1,6 +1,8 @@
package com.fanxb.bookmark.business.bookmark.controller; package com.fanxb.bookmark.business.bookmark.controller;
import com.fanxb.bookmark.business.bookmark.entity.BatchDeleteBody;
import com.fanxb.bookmark.business.bookmark.service.BookmarkService; import com.fanxb.bookmark.business.bookmark.service.BookmarkService;
import com.fanxb.bookmark.common.entity.Bookmark;
import com.fanxb.bookmark.common.entity.Result; import com.fanxb.bookmark.common.entity.Result;
import com.fanxb.bookmark.common.util.UserContextHolder; import com.fanxb.bookmark.common.util.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -49,4 +51,32 @@ public class BookmarkController {
return Result.success(null); return Result.success(null);
} }
/**
* Description: 新增一条书签
*
* @param bookmark 书签信息
* @return com.fanxb.bookmark.common.entity.Result
* @author fanxb
* @date 2019/7/12 17:28
*/
@PutMapping("")
public Result addOne(@RequestBody Bookmark bookmark) {
bookmark = bookmarkService.addOne(bookmark);
return Result.success(bookmark);
}
/**
* Description: 批量删除
*
* @param body 批量删除表单
* @return com.fanxb.bookmark.common.entity.Result
* @author fanxb
* @date 2019/7/12 17:28
*/
@PostMapping("/batchDelete")
public Result batchDelete(@RequestBody BatchDeleteBody body) {
bookmarkService.batchDelete(UserContextHolder.get().getUserId(), body.getFolderIdList(), body.getBookmarkIdList());
return Result.success(null);
}
} }

View File

@ -58,4 +58,25 @@ public interface BookmarkDao {
* @date 2019/7/9 18:55 * @date 2019/7/9 18:55
*/ */
List<Bookmark> getListByUserId(int userId); List<Bookmark> getListByUserId(int userId);
/**
* Description: 删除某用户某个书签文件下所有数据
*
* @param userId 用户id
* @param folderId 文件夹id
* @return void
* @author fanxb
* @date 2019/7/12 14:13
*/
void deleteUserFolder(@Param("userId") int userId, @Param("folderId") int folderId);
/**
* Description: 删除用户书签
*
* @param userId 用户id
* @param bookmarkIds 书签id
* @author fanxb
* @date 2019/7/12 17:24
*/
void deleteUserBookmark(@Param("userId") int userId, @Param("bookmarkIds") List<Integer> bookmarkIds);
} }

View File

@ -0,0 +1,18 @@
package com.fanxb.bookmark.business.bookmark.entity;
import lombok.Data;
import java.util.List;
/**
* Description:
*
* @author fanxb
* @date 2019/7/12 18:34
*/
@Data
public class BatchDeleteBody{
private List<Integer> folderIdList;
private List<Integer> bookmarkIdList;
}

View File

@ -2,6 +2,7 @@ package com.fanxb.bookmark.business.bookmark.service;
import com.fanxb.bookmark.business.bookmark.dao.BookmarkDao; import com.fanxb.bookmark.business.bookmark.dao.BookmarkDao;
import com.fanxb.bookmark.common.entity.Bookmark; import com.fanxb.bookmark.common.entity.Bookmark;
import com.fanxb.bookmark.common.util.UserContextHolder;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -73,27 +74,61 @@ public class BookmarkService {
* @author fanxb * @author fanxb
* @date 2019/7/9 18:45 * @date 2019/7/9 18:45
*/ */
public List<Bookmark> getOneBookmarkTree(int userId) { public Map<String, List<Bookmark>> getOneBookmarkTree(int userId) {
List<Bookmark> list = bookmarkDao.getListByUserId(userId); List<Bookmark> list = bookmarkDao.getListByUserId(userId);
Map<String, List<Bookmark>> map = new HashMap<>(50); Map<String, List<Bookmark>> map = new HashMap<>(50);
list.forEach(item -> { list.forEach(item -> {
map.computeIfAbsent(item.getPath(), k -> new ArrayList<>()); map.computeIfAbsent(item.getPath(), k -> new ArrayList<>());
map.get(item.getPath()).add(item); map.get(item.getPath()).add(item);
}); });
List<Bookmark> res = map.get(""); return map;
res.forEach(item -> insertToBookmarkTree(item, map)); // if (map.size() == 0) {
return res; // return new ArrayList<>();
// } else {
// List<Bookmark> res = map.get("");
// res.forEach(item -> insertToBookmarkTree(item, map));
// return res;
// }
} }
private void insertToBookmarkTree(Bookmark node, Map<String, List<Bookmark>> map) { /**
String path = node.getPath(); * Description: 批量删除书签
String key = path + (path.length() == 0 ? "" : ".") + node.getBookmarkId().toString(); *
if (map.containsKey(key)) { * @param userId 用户id
node.setChildren(map.get(key)); * @param folderIdList 书签文件夹id list
node.getChildren().forEach(item -> insertToBookmarkTree(item, map)); * @param bookmarkIdList 书签id list
* @author fanxb
* @date 2019/7/12 14:09
*/
@Transactional(rollbackFor = Exception.class)
public void batchDelete(int userId, List<Integer> folderIdList, List<Integer> bookmarkIdList) {
for (Integer item : folderIdList) {
bookmarkDao.deleteUserFolder(userId, item);
bookmarkIdList.add(item);
} }
bookmarkDao.deleteUserBookmark(userId, bookmarkIdList);
} }
/**
* Description: 详情
*
* @param bookmark 插入一条记录
* @return com.fanxb.bookmark.common.entity.Bookmark
* @author fanxb
* @date 2019/7/12 17:18
*/
public Bookmark addOne(Bookmark bookmark) {
int userId = UserContextHolder.get().getUserId();
Integer sort = bookmarkDao.selectMaxSort(userId, bookmark.getPath());
bookmark.setSort(sort == null ? 1 : sort + 1);
bookmark.setUserId(userId);
bookmark.setCreateTime(System.currentTimeMillis());
bookmark.setAddTime(bookmark.getCreateTime());
bookmarkDao.insertOne(bookmark);
return bookmark;
}
/** /**
* Description: 处理html节点解析出文件夹和书签 * Description: 处理html节点解析出文件夹和书签
* *
@ -124,7 +159,7 @@ public class BookmarkService {
sortBase = 0; sortBase = 0;
} }
} }
String childPath = path.length() == 0 ? node.getBookmarkId().toString() : path + "." + node.getBookmarkId(); String childPath = path + "." + node.getBookmarkId();
Elements children = ele.child(1).children(); Elements children = ele.child(1).children();
for (int i = 0, size = children.size(); i < size; i++) { for (int i = 0, size = children.size(); i < size; i++) {
dealBookmark(userId, children.get(i), childPath, sortBase + i + 1); dealBookmark(userId, children.get(i), childPath, sortBase + i + 1);
@ -151,4 +186,5 @@ public class BookmarkService {
return true; return true;
} }
} }
} }

View File

@ -42,5 +42,24 @@
order by path, sort order by path, sort
</select> </select>
<delete id="deleteUserFolder">
DELETE
FROM
bookmark
WHERE
userId = #{userId}
and path LIKE (SELECT a.path
FROM (SELECT CONCAT(path, '.', '${folderId}', '%') AS path
FROM bookmark
WHERE bookmarkId = #{folderId}) a);
</delete>
<delete id="deleteUserBookmark">
delete from bookmark where userId = #{userId} and bookmarkId in
<foreach collection="bookmarkIds" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
</delete>
</mapper> </mapper>

View File

@ -24,8 +24,8 @@ public class Bookmark {
private Integer userId; private Integer userId;
private String path; private String path;
private String name; private String name;
private String url; private String url="";
private String icon; private String icon="";
private Integer sort; private Integer sort;
private Long addTime; private Long addTime;
private Long createTime; private Long createTime;