更新笔记查询接口

pdf
pan 4 years ago
parent c7bf1b2d44
commit dc68352473
  1. 24
      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;
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<Note> 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> paper=paperRepository.findById(note.getPaperId());
paper.ifPresent(note::setPaper);
});
list.setData(noteList);
return ResponseDataUtil.buildSuccess(list);
} catch (Exception e) {

@ -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<Paper> list=new Page<>();
list.setTotal(papaerRepository.count());
List<Paper> papers = papaerRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
list.setTotal(paperRepository.count());
List<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
if(StringUtils.isNotEmpty(queryPaper.getTitle())){
predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%"));

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

@ -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<Paper, Long>, JpaSpecificationExecutor<Paper> {
public interface PaperRepository extends JpaRepository<Paper, Long>, JpaSpecificationExecutor<Paper> {
}
Loading…
Cancel
Save