增加收藏接口

pdf
pan 4 years ago
parent 22dc74a257
commit febe235802
  1. 10
      doc/db_table.md
  2. 78
      src/main/java/com/bupt/note/Controller/CollectController.java
  3. 22
      src/main/java/com/bupt/note/Controller/PaperController.java
  4. 49
      src/main/java/com/bupt/note/Model/Collect.java
  5. 30
      src/main/java/com/bupt/note/Model/Paper.java
  6. 17
      src/main/java/com/bupt/note/Repository/CollectRepository.java
  7. 1
      src/main/java/com/bupt/note/Repository/PaperRepository.java
  8. 5
      src/main/java/com/bupt/note/dto/CollectForm.java
  9. 9
      src/main/java/com/bupt/note/dto/QueryPaper.java
  10. 1
      src/main/java/com/bupt/note/dto/UploadPaper.java

@ -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 | 收藏时间戳 |

@ -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<Collect> collects=collectRepository.findByUserName(username);
Map<Long,Collect> map=new HashMap<>();
collects.forEach(collect -> {
map.put(collect.getPaperId(),collect);
});
List<Paper> paperList= paperRepository.findAllById(map.keySet());
paperList.forEach(paper -> {
paper.setHasCollect(true);
paper.setCollectTime(map.get(paper.getId()).getCreateTime());
});
Page<Paper> 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();
}
}
}

@ -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<Paper> 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);

@ -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;
}
}

@ -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;
}
}

@ -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<Collect, Long> {
boolean existsByPaperIdAndUserName(Long paperId, String userName);
@Modifying
@Transactional
Integer deleteByPaperIdAndUserName(Long paperId, String userName);
List<Collect> findByUserName(String userName);
}

@ -5,4 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface PaperRepository extends JpaRepository<Paper, Long>, JpaSpecificationExecutor<Paper> {
}

@ -0,0 +1,5 @@
package com.bupt.note.dto;
public class CollectForm {
}

@ -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;
}
}

@ -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;
}
}

Loading…
Cancel
Save