fix papers crud

This commit is contained in:
Anton Romanov 2018-05-05 10:57:02 +04:00
parent 7d286c0d76
commit b140a61a9b
9 changed files with 100 additions and 34 deletions

View File

@ -7,11 +7,15 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.configuration.Constants; import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response; 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 ru.ulstu.file.service.FileService;
import java.io.IOException; 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; import static ru.ulstu.paper.controller.PaperController.URL;
@RestController @RestController
@ -28,17 +32,18 @@ public class FileController {
@GetMapping("/download-tmp/{tmp-file-name}") @GetMapping("/download-tmp/{tmp-file-name}")
public ResponseEntity<byte[]> getFile(@PathVariable("tmp-file-name") String tmpFileName) throws IOException { public ResponseEntity<byte[]> getFile(@PathVariable("tmp-file-name") String tmpFileName) throws IOException {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=" headers.add("Content-Disposition", "attachment; filename='"
+ fileService.getTmpFileName(tmpFileName)); + URLEncoder.encode(fileService.getTmpFileName(tmpFileName), UTF_8.toString()) + "'");
return new ResponseEntity<>(fileService.getTmpFile(tmpFileName), headers, HttpStatus.OK); return new ResponseEntity<>(fileService.getTmpFile(tmpFileName), headers, HttpStatus.OK);
} }
@GetMapping("/download/{file-id}") @GetMapping("/download/{file-id}")
public ResponseEntity<byte[]> getFile(@PathVariable("file-id") Integer fileId) { public ResponseEntity<byte[]> getFile(@PathVariable("file-id") Integer fileId) throws UnsupportedEncodingException {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
File file = fileService.getFile(fileId); FileData fileData = fileService.getFile(fileId);
headers.add("Content-Disposition", "attachment; filename=" + file.getName()); headers.add("Content-Disposition", "attachment; filename='" +
return new ResponseEntity<>(file.getData(), headers, HttpStatus.OK); URLEncoder.encode(fileData.getName(), UTF_8.toString()) + "'");
return new ResponseEntity<>(fileData.getData(), headers, HttpStatus.OK);
} }
@PostMapping("/uploadTmpFile") @PostMapping("/uploadTmpFile")

View File

@ -4,10 +4,12 @@ import ru.ulstu.core.model.BaseEntity;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date; import java.util.Date;
@Entity @Entity
public class File extends BaseEntity { @Table(name = "file")
public class FileData extends BaseEntity {
private String name; private String name;
private long size; private long size;

View File

@ -1,7 +1,7 @@
package ru.ulstu.file.repostory; package ru.ulstu.file.repostory;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.file.model.File; import ru.ulstu.file.model.FileData;
public interface FileRepository extends JpaRepository<File, Integer> { public interface FileRepository extends JpaRepository<FileData, Integer> {
} }

View File

@ -2,7 +2,7 @@ package ru.ulstu.file.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; 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 ru.ulstu.file.repostory.FileRepository;
import java.io.IOException; import java.io.IOException;
@ -26,12 +26,12 @@ public class FileService {
this.fileRepository = fileRepository; this.fileRepository = fileRepository;
} }
public File createFileFromTmp(String tmpFileName) throws IOException { public FileData createFileFromTmp(String tmpFileName) throws IOException {
File file = new File(); FileData fileData = new FileData();
file.setData(getTmpFile(tmpFileName)); fileData.setData(getTmpFile(tmpFileName));
file.setName(getTmpFileName(tmpFileName)); fileData.setName(getTmpFileName(tmpFileName));
file.setCreateDate(new Date()); fileData.setCreateDate(new Date());
return fileRepository.save(file); return fileRepository.save(fileData);
} }
public String uploadToTmpDir(MultipartFile multipartFile) throws IOException { public String uploadToTmpDir(MultipartFile multipartFile) throws IOException {
@ -59,7 +59,7 @@ public class FileService {
return Files.readAllBytes(getTmpFilePath(tmpFileName)); return Files.readAllBytes(getTmpFilePath(tmpFileName));
} }
public File getFile(Integer fileId) { public FileData getFile(Integer fileId) {
return fileRepository.findOne(fileId); return fileRepository.findOne(fileId);
} }
@ -68,10 +68,14 @@ public class FileService {
} }
private Path getTmpFilePath(String tmpFileName) { private Path getTmpFilePath(String tmpFileName) {
return Paths.get(tmpDir + tmpFileName); return Paths.get(tmpDir, tmpFileName);
} }
private Path getTmpFileMetaPath(String tmpFileName) { private Path getTmpFileMetaPath(String tmpFileName) {
return Paths.get(getTmpFilePath(tmpFileName) + ".meta"); return Paths.get(getTmpFilePath(tmpFileName) + ".meta");
} }
public void deleteFile(FileData fileData) {
fileRepository.delete(fileData);
}
} }

View File

@ -23,7 +23,7 @@ public class PaperController {
this.paperService = paperService; this.paperService = paperService;
} }
@GetMapping("/papers") @GetMapping
public Response<List<PaperDto>> getPapers() { public Response<List<PaperDto>> getPapers() {
return new Response<>(paperService.findAll()); return new Response<>(paperService.findAll());
} }
@ -32,4 +32,15 @@ public class PaperController {
public Response createPaper(@RequestBody PaperDto paperDto) throws IOException { public Response createPaper(@RequestBody PaperDto paperDto) throws IOException {
return new Response(paperService.create(paperDto)); 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);
}
} }

View File

@ -2,7 +2,7 @@ package ru.ulstu.paper.model;
import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.file.model.File; import ru.ulstu.file.model.FileData;
import javax.persistence.*; import javax.persistence.*;
import java.util.Date; import java.util.Date;
@ -40,7 +40,8 @@ public class Paper extends BaseEntity {
private Boolean locked; private Boolean locked;
@ManyToOne @ManyToOne
private File file; @JoinColumn(name = "file_id")
private FileData fileData;
public PaperStatus getStatus() { public PaperStatus getStatus() {
return status; return status;
@ -82,12 +83,12 @@ public class Paper extends BaseEntity {
this.locked = locked; this.locked = locked;
} }
public File getFile() { public FileData getFileData() {
return file; return fileData;
} }
public void setFile(File file) { public void setFileData(FileData fileData) {
this.file = file; this.fileData = fileData;
} }
public String getTitle() { public String getTitle() {

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date; import java.util.Date;
public class PaperDto { public class PaperDto {
private final Integer id;
private final String title; private final String title;
private final Paper.PaperStatus status; private final Paper.PaperStatus status;
private final Date createDate; private final Date createDate;
@ -18,13 +19,15 @@ public class PaperDto {
private final Date fileCreateDate; private final Date fileCreateDate;
@JsonCreator @JsonCreator
public PaperDto(@JsonProperty("title") String title, public PaperDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title,
@JsonProperty("status") Paper.PaperStatus status, @JsonProperty("status") Paper.PaperStatus status,
@JsonProperty("createDate") Date createDate, @JsonProperty("createDate") Date createDate,
@JsonProperty("updateDate") Date updateDate, @JsonProperty("updateDate") Date updateDate,
@JsonProperty("comment") String comment, @JsonProperty("comment") String comment,
@JsonProperty("locked") Boolean locked, @JsonProperty("locked") Boolean locked,
@JsonProperty("tmpFileName") String tmpFileName) { @JsonProperty("tmpFileName") String tmpFileName) {
this.id = id;
this.title = title; this.title = title;
this.status = status; this.status = status;
this.createDate = createDate; this.createDate = createDate;
@ -38,6 +41,7 @@ public class PaperDto {
} }
public PaperDto(Paper paper) { public PaperDto(Paper paper) {
this.id = paper.getId();
this.title = paper.getTitle(); this.title = paper.getTitle();
this.status = paper.getStatus(); this.status = paper.getStatus();
this.createDate = paper.getCreateDate(); this.createDate = paper.getCreateDate();
@ -45,9 +49,13 @@ public class PaperDto {
this.comment = paper.getComment(); this.comment = paper.getComment();
this.locked = paper.getLocked(); this.locked = paper.getLocked();
this.tmpFileName = null; this.tmpFileName = null;
this.fileId = paper.getFile() == null ? null : paper.getFile().getId(); this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId();
this.fileName = paper.getFile() == null ? null : paper.getFile().getName(); this.fileName = paper.getFileData() == null ? null : paper.getFileData().getName();
this.fileCreateDate = paper.getFile() == null ? null : paper.getFile().getCreateDate(); this.fileCreateDate = paper.getFileData() == null ? null : paper.getFileData().getCreateDate();
}
public Integer getId() {
return id;
} }
public String getTitle() { public String getTitle() {

View File

@ -1,15 +1,13 @@
package ru.ulstu.paper.service; package ru.ulstu.paper.service;
import org.springframework.stereotype.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.file.service.FileService;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.repository.PaperRepository; import ru.ulstu.paper.repository.PaperRepository;
import javax.transaction.Transactional;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.List; import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
@ -43,8 +41,26 @@ public class PaperService {
paper.setTitle(paperDto.getTitle()); paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(paperDto.getUpdateDate()); paper.setUpdateDate(paperDto.getUpdateDate());
if (paperDto.getTmpFileName() != null) { if (paperDto.getTmpFileName() != null) {
paper.setFile(fileService.createFileFromTmp(paperDto.getTmpFileName())); paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
} }
return paper; 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);
}
} }

View File

@ -57,5 +57,24 @@
</nav> </nav>
<div layout:fragment="content"> <div layout:fragment="content">
</div> </div>
<footer>
<div class="container">
<div class="row">
<div class="col-md-4">
<span class="copyright">Copyright © НИО17 2018</span>
</div>
<div class="col-md-4">
</div>
<div class="col-md-4">
<ul class="list-inline quicklinks">
<li class="list-inline-item">
<a href="/swagger-ui.html">Разработчикам</a>
</li>
</ul>
</div>
</div>
</div>
</footer>
</body> </body>
</html> </html>