From febe2358027ecd62e37ce0da927eb08ca77c1ead Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Sat, 1 Aug 2020 02:48:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=94=B6=E8=97=8F=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/db_table.md | 10 +++ .../note/Controller/CollectController.java | 78 +++++++++++++++++++ .../bupt/note/Controller/PaperController.java | 22 +++++- .../java/com/bupt/note/Model/Collect.java | 49 ++++++++++++ src/main/java/com/bupt/note/Model/Paper.java | 30 +++++++ .../note/Repository/CollectRepository.java | 17 ++++ .../bupt/note/Repository/PaperRepository.java | 1 + .../java/com/bupt/note/dto/CollectForm.java | 5 ++ .../java/com/bupt/note/dto/QueryPaper.java | 9 +++ .../java/com/bupt/note/dto/UploadPaper.java | 1 + 10 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/bupt/note/Controller/CollectController.java create mode 100644 src/main/java/com/bupt/note/Model/Collect.java create mode 100644 src/main/java/com/bupt/note/Repository/CollectRepository.java create mode 100644 src/main/java/com/bupt/note/dto/CollectForm.java diff --git a/doc/db_table.md b/doc/db_table.md index 7d233e9..fe56fce 100644 --- a/doc/db_table.md +++ b/doc/db_table.md @@ -40,6 +40,7 @@ | tag | varchar | not null | 论文标签 | | file_id | bigint | not null | 正文文件id | | user_name | varchar | not null | 上传用户 | +| create_time | Long | not null | 上传时间戳 | # 论文评价 sys_paper_rating | 列名 | 数据类型 | 约束条件 | 含义 | @@ -60,3 +61,12 @@ | note_content | varchar | not null | 笔记内容 | | paper_id | bigint | not null | 论文id | | user_name | varchar | not null | 笔记用户 | + +# 收藏 sys_collect + +| 列名 | 数据类型 | 约束条件 | 含义 | +| ---- | -------- | -------- | ---- | +| id | bigint | primary key; identity | 收藏id | +| paper_id | bigint | not null | 收藏论文id | +| user_name | varchar | not null | 收藏用户 | +| create_time | Long | not null | 收藏时间戳 | \ No newline at end of file diff --git a/src/main/java/com/bupt/note/Controller/CollectController.java b/src/main/java/com/bupt/note/Controller/CollectController.java new file mode 100644 index 0000000..4663ccb --- /dev/null +++ b/src/main/java/com/bupt/note/Controller/CollectController.java @@ -0,0 +1,78 @@ +package com.bupt.note.Controller; + +import com.bupt.note.Model.Collect; +import com.bupt.note.Model.Paper; +import com.bupt.note.Repository.CollectRepository; +import com.bupt.note.Repository.PaperRepository; +import com.bupt.note.ResponseData.ResponseData; +import com.bupt.note.ResponseData.ResponseDataUtil; +import com.bupt.note.dto.Page; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping(value = "/v1/api/collect") +public class CollectController { + + @Autowired + private CollectRepository collectRepository; + + @Autowired + private PaperRepository paperRepository; + + private Logger logger= LoggerFactory.getLogger(CollectController.class); + + @PostMapping("{id}") + public ResponseData collect(@PathVariable("id") Long paperId,@CookieValue("user") String username){ + if(collectRepository.existsByPaperIdAndUserName(paperId,username)){ + if(collectRepository.deleteByPaperIdAndUserName(paperId, username)>0){ + return ResponseDataUtil.buildSuccess("read.tip.cancel_collect_ok"); + }else{ + logger.error("取消收藏失败"); + return ResponseDataUtil.buildError("read.tip.cancel_collect_fail"); + } + }else{ + Collect collect=new Collect(); + collect.setPaperId(paperId); + collect.setUserName(username); + collect.setCreateTime(System.currentTimeMillis()); + collectRepository.save(collect); + if(collectRepository.existsById(collect.getId())){ + return ResponseDataUtil.buildSuccess("read.tip.collect_ok"); + }else{ + logger.error("收藏失败"); + return ResponseDataUtil.buildError("read.tip.collect_fail"); + } + } + } + + @GetMapping + public ResponseData list(@CookieValue("user") String username){ + try { + List collects=collectRepository.findByUserName(username); + Map map=new HashMap<>(); + collects.forEach(collect -> { + map.put(collect.getPaperId(),collect); + }); + List paperList= paperRepository.findAllById(map.keySet()); + paperList.forEach(paper -> { + paper.setHasCollect(true); + paper.setCollectTime(map.get(paper.getId()).getCreateTime()); + }); + Page paperPage=new Page<>(); + paperPage.setData(paperList); + paperPage.setTotal((long) paperList.size()); + return ResponseDataUtil.buildSuccess(paperPage); + } catch (Exception e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + return ResponseDataUtil.buildError(); + } + } +} diff --git a/src/main/java/com/bupt/note/Controller/PaperController.java b/src/main/java/com/bupt/note/Controller/PaperController.java index 200a418..835bacb 100644 --- a/src/main/java/com/bupt/note/Controller/PaperController.java +++ b/src/main/java/com/bupt/note/Controller/PaperController.java @@ -2,17 +2,23 @@ package com.bupt.note.Controller; import com.bupt.note.Model.Paper; import com.bupt.note.Model.Rating; +import com.bupt.note.Repository.CollectRepository; import com.bupt.note.Repository.PaperRepository; import com.bupt.note.Repository.RatingRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; -import com.bupt.note.dto.*; +import com.bupt.note.dto.Page; +import com.bupt.note.dto.QueryPaper; +import com.bupt.note.dto.RatingForm; +import com.bupt.note.dto.UploadPaper; import org.apache.commons.lang3.StringUtils; +import org.hibernate.query.criteria.internal.OrderImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.persistence.criteria.Order; import javax.persistence.criteria.Predicate; import java.util.ArrayList; import java.util.List; @@ -32,6 +38,9 @@ public class PaperController { @Autowired private RatingRepository ratingRepository; + @Autowired + private CollectRepository collectRepository; + @PostMapping("upload") public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(),uploadPaper.getType(), @@ -52,7 +61,7 @@ public class PaperController { } @GetMapping("list") - public ResponseData list(QueryPaper queryPaper){ + public ResponseData list(QueryPaper queryPaper,@CookieValue("user") String username){ try { Page list=new Page<>(); @@ -78,7 +87,14 @@ public class PaperController { }else if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()!=null){ predicates.add(criteriaBuilder.between(root.get("year"),queryPaper.getStartYear(),queryPaper.getEndYear())); } - return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); + if(queryPaper.getOwn()){ + predicates.add(criteriaBuilder.equal(root.get("userName"),username)); + } + Order order=new OrderImpl(root.get("createTime"),false); + return criteriaQuery.where(predicates.toArray(new Predicate[0])).orderBy(order).getRestriction(); + }); + papers.forEach(paper -> { + paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(),username)); }); list.setData(papers); return ResponseDataUtil.buildSuccess(list); diff --git a/src/main/java/com/bupt/note/Model/Collect.java b/src/main/java/com/bupt/note/Model/Collect.java new file mode 100644 index 0000000..5b2f542 --- /dev/null +++ b/src/main/java/com/bupt/note/Model/Collect.java @@ -0,0 +1,49 @@ +package com.bupt.note.Model; + +import javax.persistence.*; + +@Entity +@Table(name = "sys_collect") +public class Collect { + private Long id; + private Long paperId; + private String userName; + private Long createTime; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Column(nullable = false) + public Long getPaperId() { + return paperId; + } + + public void setPaperId(Long paperId) { + this.paperId = paperId; + } + + @Column(nullable = false) + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @Column(nullable = false) + public Long getCreateTime() { + return createTime; + } + + public void setCreateTime(Long createTime) { + this.createTime = createTime; + } +} diff --git a/src/main/java/com/bupt/note/Model/Paper.java b/src/main/java/com/bupt/note/Model/Paper.java index 6e1765d..a5511ef 100644 --- a/src/main/java/com/bupt/note/Model/Paper.java +++ b/src/main/java/com/bupt/note/Model/Paper.java @@ -20,6 +20,9 @@ public class Paper { private String tag; private Long fileId; private String userName; + private Long createTime; + private boolean hasCollect; + private Long collectTime; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -120,5 +123,32 @@ public class Paper { public void setUserName(String userName) { this.userName = userName; } + + @Column(nullable = false) + public Long getCreateTime() { + return createTime; + } + + public void setCreateTime(Long createTime) { + this.createTime = createTime; + } + + @Transient + public boolean isHasCollect() { + return hasCollect; + } + + public void setHasCollect(boolean hasCollect) { + this.hasCollect = hasCollect; + } + + @Transient + public Long getCollectTime() { + return collectTime; + } + + public void setCollectTime(Long collectTime) { + this.collectTime = collectTime; + } } diff --git a/src/main/java/com/bupt/note/Repository/CollectRepository.java b/src/main/java/com/bupt/note/Repository/CollectRepository.java new file mode 100644 index 0000000..a76de46 --- /dev/null +++ b/src/main/java/com/bupt/note/Repository/CollectRepository.java @@ -0,0 +1,17 @@ +package com.bupt.note.Repository; + +import com.bupt.note.Model.Collect; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; + +import javax.transaction.Transactional; +import java.util.List; + +public interface CollectRepository extends JpaRepository { + boolean existsByPaperIdAndUserName(Long paperId, String userName); + @Modifying + @Transactional + Integer deleteByPaperIdAndUserName(Long paperId, String userName); + + List findByUserName(String userName); +} diff --git a/src/main/java/com/bupt/note/Repository/PaperRepository.java b/src/main/java/com/bupt/note/Repository/PaperRepository.java index e89b6a3..58add77 100644 --- a/src/main/java/com/bupt/note/Repository/PaperRepository.java +++ b/src/main/java/com/bupt/note/Repository/PaperRepository.java @@ -5,4 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface PaperRepository extends JpaRepository, JpaSpecificationExecutor { + } diff --git a/src/main/java/com/bupt/note/dto/CollectForm.java b/src/main/java/com/bupt/note/dto/CollectForm.java new file mode 100644 index 0000000..8102bae --- /dev/null +++ b/src/main/java/com/bupt/note/dto/CollectForm.java @@ -0,0 +1,5 @@ +package com.bupt.note.dto; + +public class CollectForm { + +} diff --git a/src/main/java/com/bupt/note/dto/QueryPaper.java b/src/main/java/com/bupt/note/dto/QueryPaper.java index ff5e26d..866a62f 100644 --- a/src/main/java/com/bupt/note/dto/QueryPaper.java +++ b/src/main/java/com/bupt/note/dto/QueryPaper.java @@ -11,6 +11,7 @@ public class QueryPaper { private String tag; private Integer startYear; private Integer endYear; + private Boolean own=false; public String getTitle() { return title; @@ -67,4 +68,12 @@ public class QueryPaper { public void setEndYear(Integer endYear) { this.endYear = endYear; } + + public Boolean getOwn() { + return own; + } + + public void setOwn(Boolean own) { + this.own = own; + } } diff --git a/src/main/java/com/bupt/note/dto/UploadPaper.java b/src/main/java/com/bupt/note/dto/UploadPaper.java index 56d2e0e..b478ea9 100644 --- a/src/main/java/com/bupt/note/dto/UploadPaper.java +++ b/src/main/java/com/bupt/note/dto/UploadPaper.java @@ -96,6 +96,7 @@ public class UploadPaper { paper.setSummary(this.summary); paper.setTag(this.tag); paper.setFileId(this.fileId); + paper.setCreateTime(System.currentTimeMillis()); return paper; } }