From b140a61a9b3b0ce6a449cc0866b77202fcbf1414 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 5 May 2018 10:57:02 +0400 Subject: [PATCH] fix papers crud --- .../java/ru/ulstu/file/FileController.java | 19 +++++++++------ .../file/model/{File.java => FileData.java} | 4 +++- .../ulstu/file/repostory/FileRepository.java | 4 ++-- .../ru/ulstu/file/service/FileService.java | 22 ++++++++++------- .../paper/controller/PaperController.java | 13 +++++++++- src/main/java/ru/ulstu/paper/model/Paper.java | 13 +++++----- .../java/ru/ulstu/paper/model/PaperDto.java | 16 +++++++++---- .../ru/ulstu/paper/service/PaperService.java | 24 +++++++++++++++---- src/main/resources/templates/default.html | 19 +++++++++++++++ 9 files changed, 100 insertions(+), 34 deletions(-) rename src/main/java/ru/ulstu/file/model/{File.java => FileData.java} (89%) diff --git a/src/main/java/ru/ulstu/file/FileController.java b/src/main/java/ru/ulstu/file/FileController.java index 45bcb4c..cb3d91c 100644 --- a/src/main/java/ru/ulstu/file/FileController.java +++ b/src/main/java/ru/ulstu/file/FileController.java @@ -7,11 +7,15 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import ru.ulstu.configuration.Constants; import ru.ulstu.core.model.response.Response; -import ru.ulstu.file.model.File; +import ru.ulstu.file.model.FileData; import ru.ulstu.file.service.FileService; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import static java.nio.charset.StandardCharsets.UTF_8; import static ru.ulstu.paper.controller.PaperController.URL; @RestController @@ -28,17 +32,18 @@ public class FileController { @GetMapping("/download-tmp/{tmp-file-name}") public ResponseEntity getFile(@PathVariable("tmp-file-name") String tmpFileName) throws IOException { HttpHeaders headers = new HttpHeaders(); - headers.add("Content-Disposition", "attachment; filename=" - + fileService.getTmpFileName(tmpFileName)); + headers.add("Content-Disposition", "attachment; filename='" + + URLEncoder.encode(fileService.getTmpFileName(tmpFileName), UTF_8.toString()) + "'"); return new ResponseEntity<>(fileService.getTmpFile(tmpFileName), headers, HttpStatus.OK); } @GetMapping("/download/{file-id}") - public ResponseEntity getFile(@PathVariable("file-id") Integer fileId) { + public ResponseEntity getFile(@PathVariable("file-id") Integer fileId) throws UnsupportedEncodingException { HttpHeaders headers = new HttpHeaders(); - File file = fileService.getFile(fileId); - headers.add("Content-Disposition", "attachment; filename=" + file.getName()); - return new ResponseEntity<>(file.getData(), headers, HttpStatus.OK); + FileData fileData = fileService.getFile(fileId); + headers.add("Content-Disposition", "attachment; filename='" + + URLEncoder.encode(fileData.getName(), UTF_8.toString()) + "'"); + return new ResponseEntity<>(fileData.getData(), headers, HttpStatus.OK); } @PostMapping("/uploadTmpFile") diff --git a/src/main/java/ru/ulstu/file/model/File.java b/src/main/java/ru/ulstu/file/model/FileData.java similarity index 89% rename from src/main/java/ru/ulstu/file/model/File.java rename to src/main/java/ru/ulstu/file/model/FileData.java index 91a59a2..e5b3277 100644 --- a/src/main/java/ru/ulstu/file/model/File.java +++ b/src/main/java/ru/ulstu/file/model/FileData.java @@ -4,10 +4,12 @@ import ru.ulstu.core.model.BaseEntity; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.Table; import java.util.Date; @Entity -public class File extends BaseEntity { +@Table(name = "file") +public class FileData extends BaseEntity { private String name; private long size; diff --git a/src/main/java/ru/ulstu/file/repostory/FileRepository.java b/src/main/java/ru/ulstu/file/repostory/FileRepository.java index 985ef65..7399239 100644 --- a/src/main/java/ru/ulstu/file/repostory/FileRepository.java +++ b/src/main/java/ru/ulstu/file/repostory/FileRepository.java @@ -1,7 +1,7 @@ package ru.ulstu.file.repostory; import org.springframework.data.jpa.repository.JpaRepository; -import ru.ulstu.file.model.File; +import ru.ulstu.file.model.FileData; -public interface FileRepository extends JpaRepository { +public interface FileRepository extends JpaRepository { } diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 5772c90..2f1fb6d 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -2,7 +2,7 @@ package ru.ulstu.file.service; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import ru.ulstu.file.model.File; +import ru.ulstu.file.model.FileData; import ru.ulstu.file.repostory.FileRepository; import java.io.IOException; @@ -26,12 +26,12 @@ public class FileService { this.fileRepository = fileRepository; } - public File createFileFromTmp(String tmpFileName) throws IOException { - File file = new File(); - file.setData(getTmpFile(tmpFileName)); - file.setName(getTmpFileName(tmpFileName)); - file.setCreateDate(new Date()); - return fileRepository.save(file); + public FileData createFileFromTmp(String tmpFileName) throws IOException { + FileData fileData = new FileData(); + fileData.setData(getTmpFile(tmpFileName)); + fileData.setName(getTmpFileName(tmpFileName)); + fileData.setCreateDate(new Date()); + return fileRepository.save(fileData); } public String uploadToTmpDir(MultipartFile multipartFile) throws IOException { @@ -59,7 +59,7 @@ public class FileService { return Files.readAllBytes(getTmpFilePath(tmpFileName)); } - public File getFile(Integer fileId) { + public FileData getFile(Integer fileId) { return fileRepository.findOne(fileId); } @@ -68,10 +68,14 @@ public class FileService { } private Path getTmpFilePath(String tmpFileName) { - return Paths.get(tmpDir + tmpFileName); + return Paths.get(tmpDir, tmpFileName); } private Path getTmpFileMetaPath(String tmpFileName) { return Paths.get(getTmpFilePath(tmpFileName) + ".meta"); } + + public void deleteFile(FileData fileData) { + fileRepository.delete(fileData); + } } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index dfe38df..9c8d0a9 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -23,7 +23,7 @@ public class PaperController { this.paperService = paperService; } - @GetMapping("/papers") + @GetMapping public Response> getPapers() { return new Response<>(paperService.findAll()); } @@ -32,4 +32,15 @@ public class PaperController { public Response createPaper(@RequestBody PaperDto paperDto) throws IOException { return new Response(paperService.create(paperDto)); } + + @PutMapping + public Response updatePaper(@RequestBody PaperDto paperDto) throws IOException { + return new Response(paperService.update(paperDto)); + } + + @DeleteMapping + public Response delete(Integer paperId) throws IOException { + paperService.delete(paperId); + return new Response(true); + } } diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index fd8b700..35d8da7 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -2,7 +2,7 @@ package ru.ulstu.paper.model; import org.hibernate.validator.constraints.NotBlank; import ru.ulstu.core.model.BaseEntity; -import ru.ulstu.file.model.File; +import ru.ulstu.file.model.FileData; import javax.persistence.*; import java.util.Date; @@ -40,7 +40,8 @@ public class Paper extends BaseEntity { private Boolean locked; @ManyToOne - private File file; + @JoinColumn(name = "file_id") + private FileData fileData; public PaperStatus getStatus() { return status; @@ -82,12 +83,12 @@ public class Paper extends BaseEntity { this.locked = locked; } - public File getFile() { - return file; + public FileData getFileData() { + return fileData; } - public void setFile(File file) { - this.file = file; + public void setFileData(FileData fileData) { + this.fileData = fileData; } public String getTitle() { diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index 82c3a78..c9929e6 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Date; public class PaperDto { + private final Integer id; private final String title; private final Paper.PaperStatus status; private final Date createDate; @@ -18,13 +19,15 @@ public class PaperDto { private final Date fileCreateDate; @JsonCreator - public PaperDto(@JsonProperty("title") String title, + public PaperDto(@JsonProperty("id") Integer id, + @JsonProperty("title") String title, @JsonProperty("status") Paper.PaperStatus status, @JsonProperty("createDate") Date createDate, @JsonProperty("updateDate") Date updateDate, @JsonProperty("comment") String comment, @JsonProperty("locked") Boolean locked, @JsonProperty("tmpFileName") String tmpFileName) { + this.id = id; this.title = title; this.status = status; this.createDate = createDate; @@ -38,6 +41,7 @@ public class PaperDto { } public PaperDto(Paper paper) { + this.id = paper.getId(); this.title = paper.getTitle(); this.status = paper.getStatus(); this.createDate = paper.getCreateDate(); @@ -45,9 +49,13 @@ public class PaperDto { this.comment = paper.getComment(); this.locked = paper.getLocked(); this.tmpFileName = null; - this.fileId = paper.getFile() == null ? null : paper.getFile().getId(); - this.fileName = paper.getFile() == null ? null : paper.getFile().getName(); - this.fileCreateDate = paper.getFile() == null ? null : paper.getFile().getCreateDate(); + this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId(); + this.fileName = paper.getFileData() == null ? null : paper.getFileData().getName(); + this.fileCreateDate = paper.getFileData() == null ? null : paper.getFileData().getCreateDate(); + } + + public Integer getId() { + return id; } public String getTitle() { diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 7a48212..430e66c 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -1,15 +1,13 @@ package ru.ulstu.paper.service; import org.springframework.stereotype.Service; -import ru.ulstu.file.model.File; +import org.springframework.transaction.annotation.Transactional; import ru.ulstu.file.service.FileService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.repository.PaperRepository; -import javax.transaction.Transactional; import java.io.IOException; -import java.util.Arrays; import java.util.List; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -43,8 +41,26 @@ public class PaperService { paper.setTitle(paperDto.getTitle()); paper.setUpdateDate(paperDto.getUpdateDate()); if (paperDto.getTmpFileName() != null) { - paper.setFile(fileService.createFileFromTmp(paperDto.getTmpFileName())); + paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); } return paper; } + + @Transactional + public Integer update(PaperDto paperDto) throws IOException { + Paper paper = paperRepository.findOne(paperDto.getId()); + if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { + fileService.deleteFile(paper.getFileData()); + } + return paperRepository.save(copyFromDto(paper, paperDto)).getId(); + } + + @Transactional + public void delete(Integer paperId) throws IOException { + Paper paper = paperRepository.findOne(paperId); + if (paper.getFileData() != null) { + fileService.deleteFile(paper.getFileData()); + } + paperRepository.delete(paper); + } } diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 826623b..16a2d24 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -57,5 +57,24 @@
+ + \ No newline at end of file