更新笔记查询接口

pdf
pan 4 years ago
parent c7bf1b2d44
commit dc68352473
  1. 22
      src/main/java/com/bupt/note/Controller/NoteController.java
  2. 12
      src/main/java/com/bupt/note/Controller/PaperController.java
  3. 16
      src/main/java/com/bupt/note/Model/Note.java
  4. 2
      src/main/java/com/bupt/note/Repository/PaperRepository.java

@ -1,9 +1,11 @@
package com.bupt.note.Controller; package com.bupt.note.Controller;
import com.bupt.note.Model.Note; import com.bupt.note.Model.Note;
import com.bupt.note.Model.Paper;
import com.bupt.note.Model.PaperNote; import com.bupt.note.Model.PaperNote;
import com.bupt.note.Repository.NoteRepository; import com.bupt.note.Repository.NoteRepository;
import com.bupt.note.Repository.PaperNoteRepository; import com.bupt.note.Repository.PaperNoteRepository;
import com.bupt.note.Repository.PaperRepository;
import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.dto.DeleteNote; import com.bupt.note.dto.DeleteNote;
@ -23,6 +25,7 @@ import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
@RestController @RestController
@RequestMapping(value = "/v1/api/notes") @RequestMapping(value = "/v1/api/notes")
@ -34,6 +37,9 @@ public class NoteController {
@Autowired @Autowired
private PaperNoteRepository paperNoteRepository; private PaperNoteRepository paperNoteRepository;
@Autowired
private PaperRepository paperRepository;
@Autowired @Autowired
private FileService fileService; private FileService fileService;
@ -107,8 +113,8 @@ public class NoteController {
* @param username * @param username
* @return * @return
*/ */
@GetMapping("list/{id}") @GetMapping({"list/{id}","list"})
public ResponseData list(@PathVariable("id") Long paperId,@CookieValue("user") String username) { public ResponseData list(@PathVariable(value = "id",required = false) Long paperId,NoteForm noteForm,@CookieValue("user") String username) {
try { try {
Page<Note> list = new Page<>(); Page<Note> list = new Page<>();
list.setTotal(noteRepository.count()); list.setTotal(noteRepository.count());
@ -117,9 +123,21 @@ public class NoteController {
if (StringUtils.isNotEmpty(username)) { if (StringUtils.isNotEmpty(username)) {
predicates.add(criteriaBuilder.equal(root.get("userName"), username)); predicates.add(criteriaBuilder.equal(root.get("userName"), username));
} }
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)); predicates.add(criteriaBuilder.equal(root.get("paperId"), paperId));
}
return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction();
}); });
noteList.forEach(note -> {
Optional<Paper> paper=paperRepository.findById(note.getPaperId());
paper.ifPresent(note::setPaper);
});
list.setData(noteList); list.setData(noteList);
return ResponseDataUtil.buildSuccess(list); return ResponseDataUtil.buildSuccess(list);
} catch (Exception e) { } catch (Exception e) {

@ -2,7 +2,7 @@ package com.bupt.note.Controller;
import com.bupt.note.Model.Paper; import com.bupt.note.Model.Paper;
import com.bupt.note.Model.Rating; 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.Repository.RatingRepository;
import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.ResponseData.ResponseDataUtil;
@ -27,7 +27,7 @@ public class PaperController {
private Logger logger= LoggerFactory.getLogger(PaperController.class); private Logger logger= LoggerFactory.getLogger(PaperController.class);
@Autowired @Autowired
private PapaerRepository papaerRepository; private PaperRepository paperRepository;
@Autowired @Autowired
private RatingRepository ratingRepository; private RatingRepository ratingRepository;
@ -38,8 +38,8 @@ public class PaperController {
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){ uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){
Paper paper = uploadPaper.toPaper(); Paper paper = uploadPaper.toPaper();
paper.setUserName(username); paper.setUserName(username);
papaerRepository.save(paper); paperRepository.save(paper);
if(papaerRepository.existsById(paper.getId())){ if(paperRepository.existsById(paper.getId())){
return ResponseDataUtil.buildSuccess(); return ResponseDataUtil.buildSuccess();
}else{ }else{
logger.error("论文上传失败"); logger.error("论文上传失败");
@ -56,8 +56,8 @@ public class PaperController {
try { try {
Page<Paper> list=new Page<>(); Page<Paper> list=new Page<>();
list.setTotal(papaerRepository.count()); list.setTotal(paperRepository.count());
List<Paper> papers = papaerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { List<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>(); List<Predicate> predicates = new ArrayList<>();
if(StringUtils.isNotEmpty(queryPaper.getTitle())){ if(StringUtils.isNotEmpty(queryPaper.getTitle())){
predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%")); predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%"));

@ -1,9 +1,6 @@
package com.bupt.note.Model; package com.bupt.note.Model;
import javax.persistence.Column; import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
@Entity @Entity
@ -16,6 +13,8 @@ public class Note implements Serializable {
private String userName; private String userName;
private String originalText; private String originalText;
private Paper paper;
@Id @Id
public String getNoteId() { public String getNoteId() {
return noteId; return noteId;
@ -69,5 +68,14 @@ public class Note implements Serializable {
public void setOriginalText(String originalText) { public void setOriginalText(String originalText) {
this.originalText = originalText; this.originalText = originalText;
} }
@Transient
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
} }

@ -4,5 +4,5 @@ import com.bupt.note.Model.Paper;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface PapaerRepository extends JpaRepository<Paper, Long>, JpaSpecificationExecutor<Paper> { public interface PaperRepository extends JpaRepository<Paper, Long>, JpaSpecificationExecutor<Paper> {
} }
Loading…
Cancel
Save