From dd4fd1f080a1184f7657638e3ad9adef57917bcc Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Fri, 31 Jul 2020 18:50:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BA=E6=96=87=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=E5=A2=9E=E5=88=A0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/db_table.md | 23 ++- .../bupt/note/Controller/FileController.java | 82 ++++---- .../bupt/note/Controller/NoteController.java | 183 ++++++++++++++---- .../bupt/note/Controller/PaperController.java | 5 + .../bupt/note/Controller/UserController.java | 56 +----- src/main/java/com/bupt/note/Model/Note.java | 83 ++++---- .../java/com/bupt/note/Model/PaperNote.java | 69 +++++++ .../bupt/note/Repository/NoteRepository.java | 12 +- .../note/Repository/PaperNoteRepository.java | 9 + .../java/com/bupt/note/dto/DeleteNote.java | 31 +++ src/main/java/com/bupt/note/dto/NoteForm.java | 72 +++++++ .../{QueryRating.java => QueryContent.java} | 11 +- .../java/com/bupt/note/dto/QueryNote.java | 13 ++ .../com/bupt/note/service/FileService.java | 31 +++ src/test/java/com/bupt/note/MyTest.java | 10 + .../com/bupt/note/NoteApplicationTests.java | 3 + 16 files changed, 492 insertions(+), 201 deletions(-) create mode 100644 src/main/java/com/bupt/note/Model/PaperNote.java create mode 100644 src/main/java/com/bupt/note/Repository/PaperNoteRepository.java create mode 100644 src/main/java/com/bupt/note/dto/DeleteNote.java create mode 100644 src/main/java/com/bupt/note/dto/NoteForm.java rename src/main/java/com/bupt/note/dto/{QueryRating.java => QueryContent.java} (51%) create mode 100644 src/main/java/com/bupt/note/dto/QueryNote.java create mode 100644 src/main/java/com/bupt/note/service/FileService.java create mode 100644 src/test/java/com/bupt/note/MyTest.java diff --git a/doc/db_table.md b/doc/db_table.md index 8d034bc..7d233e9 100644 --- a/doc/db_table.md +++ b/doc/db_table.md @@ -14,6 +14,17 @@ | file_path | varchar | not null | 文件路径 | | url_path | varchar | not null | 访问路径 | +# 论文笔记表 sys_paper_note + +| 列名 | 数据类型 | 约束条件 | 含义 | +| ---- | -------- | -------- | ---- | +| id | bigint | primary key; identity | 文件id | +| paper_id | bigint | not null | 论文id | +| file_id | bigint | not null | 原文件id | +| file_path | varchar | not null | 文件路径 | +| url_path | varchar | not null | 访问路径 | +| user_name | varchar | not null | 修改用户 | + # 论文 sys_paper | 列名 | 数据类型 | 约束条件 | 含义 | @@ -40,12 +51,12 @@ | paper_id | bigint | not null | 论文id | | user_name | varchar | not null | 评价用户 | -# 笔记 Note +# 笔记 sys_note | 列名 | 数据类型 | 约束条件 | 含义 | | ---- | -------- | -------- | ---- | -| Nid | int | primary key; identity | 编号 | -| Pid | int | foreign key | 文章编号 | -| Uid | int | foreign key | 用户编号 | -| Ntype | int | | 笔记分类 | -| Ncontent | varchar | | 笔记内容 | +| note_id | varchar | primary key | 笔记id | +| note_title | varchar | not null | 笔记标题 | +| note_content | varchar | not null | 笔记内容 | +| paper_id | bigint | not null | 论文id | +| user_name | varchar | not null | 笔记用户 | diff --git a/src/main/java/com/bupt/note/Controller/FileController.java b/src/main/java/com/bupt/note/Controller/FileController.java index 4dc2475..0b4b15a 100644 --- a/src/main/java/com/bupt/note/Controller/FileController.java +++ b/src/main/java/com/bupt/note/Controller/FileController.java @@ -1,24 +1,22 @@ package com.bupt.note.Controller; +import com.bupt.note.Model.PaperNote; import com.bupt.note.Repository.FileRepository; +import com.bupt.note.Repository.PaperNoteRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.dto.FileForm; +import com.bupt.note.dto.QueryContent; +import com.bupt.note.service.FileService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; -import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; -import java.util.Optional; -import java.util.UUID; /** * 论文文本管理接口 @@ -27,34 +25,31 @@ import java.util.UUID; @RequestMapping(value = "/v1/api/file") public class FileController { - @Value("${spring.resources.static-locations}") - private String txtPath; private static Logger logger = LoggerFactory.getLogger(FileController.class); @Autowired private FileRepository fileRepository; + @Autowired + private PaperNoteRepository paperNoteRepository; + + @Autowired + private FileService fileService; + //上传论文文本 @PostMapping("upload") - public ResponseData upload(FileForm fileForm){ - MultipartFile file=fileForm.getFile(); - if(file!=null&& MediaType.TEXT_PLAIN_VALUE.equals(file.getContentType())){ + public ResponseData upload(FileForm fileForm) { + MultipartFile file = fileForm.getFile(); + if (file != null && MediaType.TEXT_PLAIN_VALUE.equals(file.getContentType())) { - File txtFile; - File txtDir; try { - txtDir=new File(URLDecoder.decode(ResourceUtils.getURL("classpath:").getPath(), StandardCharsets.UTF_8) + txtPath.replace("classpath:/", "")); - - if(!txtDir.exists()&&txtDir.mkdirs()){ - logger.info("成功初始化上传论文目录:"+txtDir.getAbsolutePath()); - } - txtFile=new File(txtDir, UUID.randomUUID()+".txt"); - logger.debug("论文保存到"+txtFile.getAbsolutePath()); + File txtFile = fileService.newFile(); + logger.debug("论文保存到" + txtFile.getAbsolutePath()); file.transferTo(txtFile); - com.bupt.note.Model.File f=new com.bupt.note.Model.File(); + com.bupt.note.Model.File f = new com.bupt.note.Model.File(); f.setFilePath(txtFile.getAbsolutePath()); - f.setUrlPath("/txt/"+txtFile.getName()); + f.setUrlPath("/txt/" + txtFile.getName()); fileRepository.save(f); return ResponseDataUtil.buildSuccess(f.getId()); } catch (IOException e) { @@ -62,42 +57,47 @@ public class FileController { e.printStackTrace(); return ResponseDataUtil.buildError(); } - }else{ + } else { + logger.error("上传论文表单校验失败"); return ResponseDataUtil.buildError(); } } - @GetMapping("find/{id}") - public ResponseData get(@PathVariable Long id){ - if(fileRepository.existsById(id)){ - com.bupt.note.Model.File file= fileRepository.getOne(id); + @GetMapping("find") + public ResponseData get(QueryContent queryContent,@CookieValue("user") String username) { + if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(),username)) { + PaperNote paperNote=paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(),username); + return ResponseDataUtil.buildSuccess(paperNote.getUrlPath()); + }else if(fileRepository.existsById(queryContent.getFileId())){ + com.bupt.note.Model.File file=fileRepository.getOne(queryContent.getFileId()); return ResponseDataUtil.buildSuccess(file.getUrlPath()); - }else{ + } else{ + logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId())); return ResponseDataUtil.buildError(); } } + /** * 删除论文文本文件 + * * @param id 文件id * @return */ @DeleteMapping("remove/{id}") - public ResponseData remove(@PathVariable Long id){ - if(fileRepository.existsById(id)){ - Optional f=fileRepository.findById(id); - if(f.isPresent()){ - File txtFile=new File(f.get().getFilePath()); - if(txtFile.exists()&&txtFile.delete()) { - fileRepository.deleteById(id); - return ResponseDataUtil.buildSuccess(); - }else{ - return ResponseDataUtil.buildError(); - } - }else { + public ResponseData remove(@PathVariable Long id) { + if (fileRepository.existsById(id)) { + com.bupt.note.Model.File txtFile = fileRepository.getOne(id); + File f = new File(txtFile.getFilePath()); + if (f.exists() && f.delete()) { + fileRepository.deleteById(id); + return ResponseDataUtil.buildSuccess(); + } else { + logger.error(String.format("删除论文id=%d失败",id)); return ResponseDataUtil.buildError(); } - }else{ + } else { + logger.error(String.format("论文id=%d不存在",id)); return ResponseDataUtil.buildError(); } } diff --git a/src/main/java/com/bupt/note/Controller/NoteController.java b/src/main/java/com/bupt/note/Controller/NoteController.java index 6608c3e..a74cc18 100644 --- a/src/main/java/com/bupt/note/Controller/NoteController.java +++ b/src/main/java/com/bupt/note/Controller/NoteController.java @@ -1,69 +1,166 @@ package com.bupt.note.Controller; import com.bupt.note.Model.Note; +import com.bupt.note.Model.PaperNote; import com.bupt.note.Repository.NoteRepository; +import com.bupt.note.Repository.PaperNoteRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; -import com.bupt.note.ResponseData.ResultEnums; +import com.bupt.note.dto.DeleteNote; +import com.bupt.note.dto.NoteForm; +import com.bupt.note.dto.Page; +import com.bupt.note.service.FileService; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.*; +import javax.persistence.criteria.Predicate; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; @RestController @RequestMapping(value = "/v1/api/notes") public class NoteController { @Autowired - NoteRepository noteRepository; + private NoteRepository noteRepository; - @RequestMapping(value="/{docId}", method=RequestMethod.POST, produces = "application/json") - public ResponseData postNote(@PathVariable long docId, @RequestBody Note note) { - // 处理"/notes/[doc_id]"的POST请求,用来创建Note - Note newNote; - note.setDocId(docId); - try { - newNote = noteRepository.save(note); - } catch (Exception e) { - return ResponseDataUtil.buildError(ResultEnums.ErrNoteSaveFailed); - } - return ResponseDataUtil.buildSuccess(newNote); - } + @Autowired + private PaperNoteRepository paperNoteRepository; - @RequestMapping(value="/{docId}/{noteId}", method=RequestMethod.DELETE, produces = "application/json") - public ResponseData deleteNote(@PathVariable long docId, @PathVariable long noteId) { - // 处理"/notes/[doc_id]/[note_id]"的DELETE请求,用来删除Note - Optional optionalNote = noteRepository.findById(noteId); - if (!optionalNote.isPresent()) { - return ResponseDataUtil.buildError(ResultEnums.ErrNoteDeleteFailed); + @Autowired + private FileService fileService; + + private Logger logger = LoggerFactory.getLogger(NoteController.class); + + /** + * 添加笔记 + * + * @param noteForm + * @param username + * @return + */ + @PostMapping("add") + public ResponseData addNote(@RequestBody NoteForm noteForm, @CookieValue("user") String username) { + if (noteForm.getPaperId() != null && StringUtils.isNoneBlank(noteForm.getNoteId(), + noteForm.getNoteTitle(), noteForm.getNoteContent(), noteForm.getContent())) { + Note note = noteForm.toNote(); + note.setUserName(username); + noteRepository.save(note); + if (noteRepository.existsById(note.getNoteId())) { + Writer wr = null; + try { + PaperNote paperNote; + File f; + if (paperNoteRepository.existsByPaperIdAndUserName(noteForm.getPaperId(), username)) { + paperNote = paperNoteRepository.findByPaperIdAndUserName(note.getPaperId(), username); + f = new File(paperNote.getFilePath()); + } else { + f = fileService.newFile(); + paperNote = new PaperNote(); + paperNote.setFileId(noteForm.getFileId()); + paperNote.setPaperId(noteForm.getPaperId()); + paperNote.setFilePath(f.getAbsolutePath()); + paperNote.setUrlPath("/txt/" + f.getName()); + paperNote.setUserName(username); + paperNoteRepository.save(paperNote); + } + wr = new FileWriter(f); + wr.write(noteForm.getContent()); + return ResponseDataUtil.buildSuccess(note); + } catch (IOException e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + } finally { + if (wr != null) { + try { + wr.close(); + } catch (IOException e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + } + } + } + logger.error(String.format("更新论文id=%d失败", noteForm.getPaperId())); + return ResponseDataUtil.buildError(); + + } else { + logger.error("保存笔记失败"); + return ResponseDataUtil.buildError(); + } + } else { + logger.error("表单校验失败"); + return ResponseDataUtil.buildError(); } - Note note = optionalNote.get(); - noteRepository.delete(note); - return ResponseDataUtil.buildSuccess("success"); } - @RequestMapping(value="/{docId}/{noteId}", method=RequestMethod.PATCH, produces = "application/json") - public ResponseData updateNote(@PathVariable long docId, @PathVariable long noteId, - @RequestParam String type, @RequestParam String text) { - // 处理"/notes/[doc_id]/[note_id]"的PATCH请求,用来更新Note - Optional optionalNote = noteRepository.findById(noteId); - if (!optionalNote.isPresent()) { - return ResponseDataUtil.buildError(ResultEnums.ErrNoteIdNotExist); + /** + * 查找笔记 + * + * @param username + * @return + */ + @GetMapping("list/{id}") + public ResponseData list(@PathVariable("id") Long paperId,@CookieValue("user") String username) { + try { + Page list = new Page<>(); + list.setTotal(noteRepository.count()); + List noteList = noteRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { + List predicates = new ArrayList<>(); + if (StringUtils.isNotEmpty(username)) { + predicates.add(criteriaBuilder.equal(root.get("userName"), username)); + } + predicates.add(criteriaBuilder.equal(root.get("paperId"),paperId)); + return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); + }); + list.setData(noteList); + return ResponseDataUtil.buildSuccess(list); + } catch (Exception e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + return ResponseDataUtil.buildError(); } - Note note = optionalNote.get(); - note.setText(text); - note.setType(type); - noteRepository.save(note); - return ResponseDataUtil.buildSuccess("success"); } - @RequestMapping(value="/{docId}", method=RequestMethod.GET, produces = "application/json") - public ResponseData getNote(@PathVariable long docId) { - // 处理"/[doc_id]"的GET请求,用来获取某个doc的笔记列表 - List noteList = noteRepository.findByDocId(docId); - if (noteList.isEmpty()) { - return ResponseDataUtil.buildError(ResultEnums.ErrDocIdNotExist); + /** + * 删除笔记 + * @return + */ + @DeleteMapping("remove") + public ResponseData remove(@RequestBody DeleteNote deleteNote,@CookieValue("user") String username){ + if(deleteNote.getNoteId()!=null&&deleteNote.getPaperId()!=null&&StringUtils.isNotEmpty(deleteNote.getContent())&& + paperNoteRepository.existsByPaperIdAndUserName(deleteNote.getPaperId(),username)){ + noteRepository.deleteById(deleteNote.getNoteId()); + PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(deleteNote.getPaperId(), username); + File f = new File(paperNote.getFilePath()); + Writer wr = null; + try { + wr = new FileWriter(f); + wr.write(deleteNote.getContent()); + return ResponseDataUtil.buildSuccess(); + } catch (IOException e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + } finally { + if (wr != null) { + try { + wr.close(); + } catch (IOException e) { + e.printStackTrace(); + logger.error(String.valueOf(e)); + } + } + } + logger.error("删除笔记失败"); + }else{ + logger.error("删除笔记表单校验失败"); } - return ResponseDataUtil.buildSuccess(noteList); + 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 ca2e201..ce16eac 100644 --- a/src/main/java/com/bupt/note/Controller/PaperController.java +++ b/src/main/java/com/bupt/note/Controller/PaperController.java @@ -42,9 +42,11 @@ public class PaperController { if(papaerRepository.existsById(paper.getId())){ return ResponseDataUtil.buildSuccess(); }else{ + logger.error("论文上传失败"); return ResponseDataUtil.buildError(); } }else{ + logger.error("论文表单校验失败"); return ResponseDataUtil.buildError(); } } @@ -97,9 +99,11 @@ public class PaperController { if(ratingRepository.existsById(rating.getId())){ return ResponseDataUtil.buildSuccess(rating); }else{ + logger.warn("论文保存失败"); return ResponseDataUtil.buildSuccess(); } }else{ + logger.error("论文表单校验失败"); return ResponseDataUtil.buildError(); } } @@ -110,6 +114,7 @@ public class PaperController { Rating rating=ratingRepository.findByPaperIdAndUserName(paperId,userName); return ResponseDataUtil.buildSuccess(rating); }else{ + logger.error("论文评价表单校验失败"); return ResponseDataUtil.buildError(); } } diff --git a/src/main/java/com/bupt/note/Controller/UserController.java b/src/main/java/com/bupt/note/Controller/UserController.java index e683b08..33e9350 100644 --- a/src/main/java/com/bupt/note/Controller/UserController.java +++ b/src/main/java/com/bupt/note/Controller/UserController.java @@ -7,10 +7,10 @@ import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.ResponseData.ResultEnums; import com.bupt.note.dto.UserForm; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -import java.util.List; -import java.util.Optional; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; /** * 用户接口 @@ -52,52 +52,4 @@ public class UserController { return ResponseDataUtil.buildSuccess(ResultEnums.SUCCESS); } } - - @RequestMapping(method=RequestMethod.GET) - public ResponseData> getUserList() { - // 处理"/users/"的GET请求,用来获取用户列表 - List userList = userRepository.findAll(); - return ResponseDataUtil.buildSuccess(userList); - } - - @RequestMapping(value="/{id}", method=RequestMethod.GET) - public ResponseData getUser(@PathVariable Long id) { - // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息 - Optional optionalUser = userRepository.findById(id); - if (!optionalUser.isPresent()) { - return ResponseDataUtil.buildError(ResultEnums.ErrUserIdNotExist); - } - - User u = optionalUser.get(); - return ResponseDataUtil.buildSuccess(u); - } - - @RequestMapping(value="/{id}", method=RequestMethod.PUT) - public ResponseData putUser(@PathVariable Long id, @ModelAttribute User user) { - // 处理"/users/{id}"的PUT请求,用来更新User信息 - Optional optionalUser = userRepository.findById(id); - if (!optionalUser.isPresent()) { - return ResponseDataUtil.buildError(ResultEnums.ErrUserIdNotExist); - } - - User u = optionalUser.get(); - u.setUserName(user.getUserName()); - u.setPassword(user.getPassword()); - userRepository.save(u); - return ResponseDataUtil.buildSuccess("success"); - } - - @RequestMapping(value="/{id}", method=RequestMethod.DELETE) - public ResponseData deleteUser(@PathVariable Long id) { - // 处理"/users/{id}"的DELETE请求,用来删除User - Optional optionalUser = userRepository.findById(id); - if (!optionalUser.isPresent()) { - - return ResponseDataUtil.buildError(ResultEnums.ErrUserIdNotExist); - } - - User u = optionalUser.get(); - userRepository.delete(u); - return ResponseDataUtil.buildSuccess("success"); - } } diff --git a/src/main/java/com/bupt/note/Model/Note.java b/src/main/java/com/bupt/note/Model/Note.java index 09f47f0..16d8454 100644 --- a/src/main/java/com/bupt/note/Model/Note.java +++ b/src/main/java/com/bupt/note/Model/Note.java @@ -1,78 +1,63 @@ package com.bupt.note.Model; -import com.fasterxml.jackson.annotation.JsonProperty; - -import javax.persistence.*; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; import java.io.Serializable; @Entity @Table(name = "sys_note") public class Note implements Serializable { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long Id; - @Column(nullable = false) - @JsonProperty(value = "docId") - private Long docId; - @Column(nullable = false) - @JsonProperty(value = "userId") - private Long userId; - @Column(nullable = false) - private String type; - @Column(nullable = false) - private String text; - - public Note() { - super(); - } + private String noteId; + private String noteTitle; + private String noteContent; + private Long paperId; + private String userName; - public Note(long docId, long userId, String type, String text) { - super(); - this.docId = docId; - this.userId = userId; - this.type = type; - this.text = text; - } - - @GeneratedValue(strategy = GenerationType.IDENTITY) - public Long getId() { - return Id; + @Id + public String getNoteId() { + return noteId; } - public void setId(Long id) { - Id = id; + public void setNoteId(String noteId) { + this.noteId = noteId; } - public Long getDocId() { - return docId; + @Column(nullable = false) + public String getNoteTitle() { + return noteTitle; } - public void setDocId(Long docId) { - this.docId = docId; + public void setNoteTitle(String noteTitle) { + this.noteTitle = noteTitle; } - public Long getUserId() { - return userId; + @Column(nullable = false) + public String getNoteContent() { + return noteContent; } - public void setUserId(Long userId) { - this.userId = userId; + public void setNoteContent(String noteContent) { + this.noteContent = noteContent; } - public String getType() { - return type; + @Column(nullable = false) + public Long getPaperId() { + return paperId; } - public void setType(String type) { - this.type = type; + public void setPaperId(Long paperId) { + this.paperId = paperId; } - public String getText() { - return text; + @Column(nullable = false) + public String getUserName() { + return userName; } - public void setText(String text) { - this.text = text; + public void setUserName(String userName) { + this.userName = userName; } } diff --git a/src/main/java/com/bupt/note/Model/PaperNote.java b/src/main/java/com/bupt/note/Model/PaperNote.java new file mode 100644 index 0000000..899d3c9 --- /dev/null +++ b/src/main/java/com/bupt/note/Model/PaperNote.java @@ -0,0 +1,69 @@ +package com.bupt.note.Model; + +import javax.persistence.*; + +@Entity +@Table(name = "sys_paper_note") +public class PaperNote { + private Long id; + private Long fileId; + private Long paperId; + private String filePath; + private String urlPath; + private String userName; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @Column(nullable = false) + public Long getFileId() { + return fileId; + } + + public void setFileId(Long fileId) { + this.fileId = fileId; + } + + @Column(nullable = false) + public String getFilePath() { + return filePath; + } + + public void setFilePath(String filePath) { + this.filePath = filePath; + } + + @Column(nullable = false) + public String getUrlPath() { + return urlPath; + } + + public void setUrlPath(String urlPath) { + this.urlPath = urlPath; + } + + @Column(nullable = false) + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @Column(nullable = false) + public Long getPaperId() { + return paperId; + } + + public void setPaperId(Long paperId) { + this.paperId = paperId; + } +} diff --git a/src/main/java/com/bupt/note/Repository/NoteRepository.java b/src/main/java/com/bupt/note/Repository/NoteRepository.java index 3724951..562110d 100644 --- a/src/main/java/com/bupt/note/Repository/NoteRepository.java +++ b/src/main/java/com/bupt/note/Repository/NoteRepository.java @@ -1,15 +1,9 @@ package com.bupt.note.Repository; import com.bupt.note.Model.Note; -import org.springframework.data.repository.CrudRepository; -import java.util.List; -import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; -public interface NoteRepository extends CrudRepository { - @Override - Optional findById(Long id); +public interface NoteRepository extends JpaRepository, JpaSpecificationExecutor { - List findByDocId(Long id); - int countByDocId(Long id); - void deleteById(Long id); } diff --git a/src/main/java/com/bupt/note/Repository/PaperNoteRepository.java b/src/main/java/com/bupt/note/Repository/PaperNoteRepository.java new file mode 100644 index 0000000..661c0b6 --- /dev/null +++ b/src/main/java/com/bupt/note/Repository/PaperNoteRepository.java @@ -0,0 +1,9 @@ +package com.bupt.note.Repository; + +import com.bupt.note.Model.PaperNote; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PaperNoteRepository extends JpaRepository { + boolean existsByPaperIdAndUserName(Long paperId, String userName); + PaperNote findByPaperIdAndUserName(Long paperId, String userName); +} diff --git a/src/main/java/com/bupt/note/dto/DeleteNote.java b/src/main/java/com/bupt/note/dto/DeleteNote.java new file mode 100644 index 0000000..4d0a2e4 --- /dev/null +++ b/src/main/java/com/bupt/note/dto/DeleteNote.java @@ -0,0 +1,31 @@ +package com.bupt.note.dto; + +public class DeleteNote { + private String noteId; + private Long paperId; + private String content; + + public String getNoteId() { + return noteId; + } + + public void setNoteId(String noteId) { + this.noteId = noteId; + } + + public Long getPaperId() { + return paperId; + } + + public void setPaperId(Long paperId) { + this.paperId = paperId; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/src/main/java/com/bupt/note/dto/NoteForm.java b/src/main/java/com/bupt/note/dto/NoteForm.java new file mode 100644 index 0000000..50c3c26 --- /dev/null +++ b/src/main/java/com/bupt/note/dto/NoteForm.java @@ -0,0 +1,72 @@ +package com.bupt.note.dto; + +import com.bupt.note.Model.Note; + +/** + * 笔记表单 + */ +public class NoteForm { + private String noteId; + private String noteTitle; + private String noteContent; + private Long paperId; + private Long fileId; + private String content; + + public String getNoteId() { + return noteId; + } + + public void setNoteId(String noteId) { + this.noteId = noteId; + } + + public String getNoteTitle() { + return noteTitle; + } + + public void setNoteTitle(String noteTitle) { + this.noteTitle = noteTitle; + } + + public String getNoteContent() { + return noteContent; + } + + public void setNoteContent(String noteContent) { + this.noteContent = noteContent; + } + + public Long getPaperId() { + return paperId; + } + + public void setPaperId(Long paperId) { + this.paperId = paperId; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public Long getFileId() { + return fileId; + } + + public void setFileId(Long fileId) { + this.fileId = fileId; + } + + public Note toNote(){ + Note note=new Note(); + note.setPaperId(this.paperId); + note.setNoteContent(this.noteContent); + note.setNoteTitle(this.noteTitle); + note.setNoteId(this.noteId); + return note; + } +} diff --git a/src/main/java/com/bupt/note/dto/QueryRating.java b/src/main/java/com/bupt/note/dto/QueryContent.java similarity index 51% rename from src/main/java/com/bupt/note/dto/QueryRating.java rename to src/main/java/com/bupt/note/dto/QueryContent.java index b53ec56..2414f25 100644 --- a/src/main/java/com/bupt/note/dto/QueryRating.java +++ b/src/main/java/com/bupt/note/dto/QueryContent.java @@ -1,7 +1,8 @@ package com.bupt.note.dto; -public class QueryRating { +public class QueryContent { private Long paperId; + private Long fileId; public Long getPaperId() { return paperId; @@ -10,4 +11,12 @@ public class QueryRating { public void setPaperId(Long paperId) { this.paperId = paperId; } + + public Long getFileId() { + return fileId; + } + + public void setFileId(Long fileId) { + this.fileId = fileId; + } } diff --git a/src/main/java/com/bupt/note/dto/QueryNote.java b/src/main/java/com/bupt/note/dto/QueryNote.java new file mode 100644 index 0000000..6bd902f --- /dev/null +++ b/src/main/java/com/bupt/note/dto/QueryNote.java @@ -0,0 +1,13 @@ +package com.bupt.note.dto; + +public class QueryNote { + private Long currentPage; + + public Long getCurrentPage() { + return currentPage; + } + + public void setCurrentPage(Long currentPage) { + this.currentPage = currentPage; + } +} diff --git a/src/main/java/com/bupt/note/service/FileService.java b/src/main/java/com/bupt/note/service/FileService.java new file mode 100644 index 0000000..8790da0 --- /dev/null +++ b/src/main/java/com/bupt/note/service/FileService.java @@ -0,0 +1,31 @@ +package com.bupt.note.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.util.ResourceUtils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.UUID; + +@Service +public class FileService { + + @Value("${spring.resources.static-locations}") + private String txtPath; + + private Logger logger = LoggerFactory.getLogger(FileService.class); + + public File newFile() throws FileNotFoundException { + File txtDir = new File(URLDecoder.decode(ResourceUtils.getURL("classpath:").getPath(), StandardCharsets.UTF_8) + txtPath.replace("classpath:/", "")); + if (!txtDir.exists() && txtDir.mkdirs()) { + logger.info("成功初始化上传论文目录:" + txtDir.getAbsolutePath()); + } + return new File(txtDir, UUID.randomUUID() + ".txt"); + + } +} diff --git a/src/test/java/com/bupt/note/MyTest.java b/src/test/java/com/bupt/note/MyTest.java new file mode 100644 index 0000000..099d3a6 --- /dev/null +++ b/src/test/java/com/bupt/note/MyTest.java @@ -0,0 +1,10 @@ +package com.bupt.note; + +import org.junit.jupiter.api.Test; + +public class MyTest { + @Test + public void test1(){ + System.out.println(String.format("%d=%d",1,1L)); + } +} diff --git a/src/test/java/com/bupt/note/NoteApplicationTests.java b/src/test/java/com/bupt/note/NoteApplicationTests.java index e44da8d..f9941c0 100644 --- a/src/test/java/com/bupt/note/NoteApplicationTests.java +++ b/src/test/java/com/bupt/note/NoteApplicationTests.java @@ -33,4 +33,7 @@ class NoteApplicationTests { .contentType(MediaType.APPLICATION_JSON); mvc.perform(builder).andDo(MockMvcResultHandlers.print()); } + + + }