From dc68352473afc9bd70807212bbe2efc8098a9bda Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Fri, 31 Jul 2020 23:15:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=AC=94=E8=AE=B0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bupt/note/Controller/NoteController.java | 24 ++++++++++++++++--- .../bupt/note/Controller/PaperController.java | 12 +++++----- src/main/java/com/bupt/note/Model/Note.java | 16 +++++++++---- ...erRepository.java => PaperRepository.java} | 2 +- 4 files changed, 40 insertions(+), 14 deletions(-) rename src/main/java/com/bupt/note/Repository/{PapaerRepository.java => PaperRepository.java} (66%) diff --git a/src/main/java/com/bupt/note/Controller/NoteController.java b/src/main/java/com/bupt/note/Controller/NoteController.java index 9d07072..c3b4813 100644 --- a/src/main/java/com/bupt/note/Controller/NoteController.java +++ b/src/main/java/com/bupt/note/Controller/NoteController.java @@ -1,9 +1,11 @@ package com.bupt.note.Controller; import com.bupt.note.Model.Note; +import com.bupt.note.Model.Paper; import com.bupt.note.Model.PaperNote; import com.bupt.note.Repository.NoteRepository; import com.bupt.note.Repository.PaperNoteRepository; +import com.bupt.note.Repository.PaperRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.dto.DeleteNote; @@ -23,6 +25,7 @@ import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.List; +import java.util.Optional; @RestController @RequestMapping(value = "/v1/api/notes") @@ -34,6 +37,9 @@ public class NoteController { @Autowired private PaperNoteRepository paperNoteRepository; + @Autowired + private PaperRepository paperRepository; + @Autowired private FileService fileService; @@ -107,8 +113,8 @@ public class NoteController { * @param username * @return */ - @GetMapping("list/{id}") - public ResponseData list(@PathVariable("id") Long paperId,@CookieValue("user") String username) { + @GetMapping({"list/{id}","list"}) + public ResponseData list(@PathVariable(value = "id",required = false) Long paperId,NoteForm noteForm,@CookieValue("user") String username) { try { Page list = new Page<>(); list.setTotal(noteRepository.count()); @@ -117,9 +123,21 @@ public class NoteController { if (StringUtils.isNotEmpty(username)) { predicates.add(criteriaBuilder.equal(root.get("userName"), username)); } - predicates.add(criteriaBuilder.equal(root.get("paperId"),paperId)); + if(StringUtils.isNotEmpty(noteForm.getNoteTitle())){ + predicates.add(criteriaBuilder.like(root.get("noteTitle"),"%"+noteForm.getNoteTitle()+"%")); + } + if(StringUtils.isNotEmpty(noteForm.getNoteContent())){ + predicates.add(criteriaBuilder.like(root.get("noteContent"),"%"+noteForm.getNoteContent()+"%")); + } + if(paperId!=null) { + predicates.add(criteriaBuilder.equal(root.get("paperId"), paperId)); + } return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); }); + noteList.forEach(note -> { + Optional paper=paperRepository.findById(note.getPaperId()); + paper.ifPresent(note::setPaper); + }); list.setData(noteList); return ResponseDataUtil.buildSuccess(list); } catch (Exception e) { diff --git a/src/main/java/com/bupt/note/Controller/PaperController.java b/src/main/java/com/bupt/note/Controller/PaperController.java index ce16eac..200a418 100644 --- a/src/main/java/com/bupt/note/Controller/PaperController.java +++ b/src/main/java/com/bupt/note/Controller/PaperController.java @@ -2,7 +2,7 @@ package com.bupt.note.Controller; import com.bupt.note.Model.Paper; import com.bupt.note.Model.Rating; -import com.bupt.note.Repository.PapaerRepository; +import com.bupt.note.Repository.PaperRepository; import com.bupt.note.Repository.RatingRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; @@ -27,7 +27,7 @@ public class PaperController { private Logger logger= LoggerFactory.getLogger(PaperController.class); @Autowired - private PapaerRepository papaerRepository; + private PaperRepository paperRepository; @Autowired private RatingRepository ratingRepository; @@ -38,8 +38,8 @@ public class PaperController { uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){ Paper paper = uploadPaper.toPaper(); paper.setUserName(username); - papaerRepository.save(paper); - if(papaerRepository.existsById(paper.getId())){ + paperRepository.save(paper); + if(paperRepository.existsById(paper.getId())){ return ResponseDataUtil.buildSuccess(); }else{ logger.error("论文上传失败"); @@ -56,8 +56,8 @@ public class PaperController { try { Page list=new Page<>(); - list.setTotal(papaerRepository.count()); - List papers = papaerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { + list.setTotal(paperRepository.count()); + List papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { List predicates = new ArrayList<>(); if(StringUtils.isNotEmpty(queryPaper.getTitle())){ predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%")); diff --git a/src/main/java/com/bupt/note/Model/Note.java b/src/main/java/com/bupt/note/Model/Note.java index 7a570ae..8e9a2a7 100644 --- a/src/main/java/com/bupt/note/Model/Note.java +++ b/src/main/java/com/bupt/note/Model/Note.java @@ -1,9 +1,6 @@ package com.bupt.note.Model; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; +import javax.persistence.*; import java.io.Serializable; @Entity @@ -16,6 +13,8 @@ public class Note implements Serializable { private String userName; private String originalText; + private Paper paper; + @Id public String getNoteId() { return noteId; @@ -69,5 +68,14 @@ public class Note implements Serializable { public void setOriginalText(String originalText) { this.originalText = originalText; } + + @Transient + public Paper getPaper() { + return paper; + } + + public void setPaper(Paper paper) { + this.paper = paper; + } } diff --git a/src/main/java/com/bupt/note/Repository/PapaerRepository.java b/src/main/java/com/bupt/note/Repository/PaperRepository.java similarity index 66% rename from src/main/java/com/bupt/note/Repository/PapaerRepository.java rename to src/main/java/com/bupt/note/Repository/PaperRepository.java index dc7ac91..e89b6a3 100644 --- a/src/main/java/com/bupt/note/Repository/PapaerRepository.java +++ b/src/main/java/com/bupt/note/Repository/PaperRepository.java @@ -4,5 +4,5 @@ import com.bupt.note.Model.Paper; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; -public interface PapaerRepository extends JpaRepository, JpaSpecificationExecutor { +public interface PaperRepository extends JpaRepository, JpaSpecificationExecutor { }