#13 file list on paper page

This commit is contained in:
Семенова Мария 2019-03-18 16:02:55 +04:00
parent 0764d961fa
commit cb394edf90
5 changed files with 45 additions and 13 deletions

View File

@ -9,12 +9,13 @@ public class FileDataDto {
private Integer id; private Integer id;
private String name; private String name;
private long size; private long size;
private Date createDate; private Date createDate;
private byte[] data; private byte[] data;
private String fileName; private String fileName;
private String tmpFileName; private String tmpFileName;
private boolean deleteFlag;
public FileDataDto() { public FileDataDto() {
} }
@ -99,4 +100,12 @@ public class FileDataDto {
this.tmpFileName = tmpFileName; this.tmpFileName = tmpFileName;
} }
public boolean isDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(boolean deleteFlag) {
this.deleteFlag = deleteFlag;
}
} }

View File

@ -110,4 +110,10 @@ public class FileService {
fileData.setSize(fileDataDto.getSize()); fileData.setSize(fileDataDto.getSize());
return fileData; return fileData;
} }
@Transactional
public void delete(Integer fileId) throws IOException {
FileData file = fileRepository.findOne(fileId);
fileRepository.delete(file);
}
} }

View File

@ -5,6 +5,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.model.FileDataDto;
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;
@ -80,7 +81,7 @@ public class PaperService {
paper.setTitle(paperDto.getTitle()); paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(new Date()); paper.setUpdateDate(new Date());
paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines()));
paper.setFiles(fileService.saveOrCreate(paperDto.getFiles())); paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream().filter(f -> f.isDeleteFlag() == false).collect(Collectors.toList())));
paper.getAuthors().clear(); paper.getAuthors().clear();
if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) {
paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds)));
@ -93,6 +94,11 @@ public class PaperService {
Paper paper = paperRepository.findOne(paperDto.getId()); Paper paper = paperRepository.findOne(paperDto.getId());
Paper.PaperStatus oldStatus = paper.getStatus(); Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors()); Set<User> oldAuthors = new HashSet<>(paper.getAuthors());
for (FileDataDto file : paperDto.getFiles().stream().filter(f -> f.isDeleteFlag() == true && f.getId() != null).collect(Collectors.toList())) {
fileService.delete(file.getId());
}
paperRepository.save(copyFromDto(paper, paperDto)); paperRepository.save(copyFromDto(paper, paperDto));
paper.getAuthors().forEach(author -> { paper.getAuthors().forEach(author -> {

View File

@ -8,16 +8,5 @@
<column name="paper_id" type="integer"/> <column name="paper_id" type="integer"/>
</addColumn> </addColumn>
<createTable tableName="paper_files">
<column name="paper_id" type="integer"/>
<column name="file_id" type="integer"/>
</createTable>
<addForeignKeyConstraint baseTableName="paper_files" baseColumnNames="paper_id"
constraintName="fk_paper_paper_files" referencedTableName="paper"
referencedColumnNames="id"/>
<addForeignKeyConstraint baseTableName="paper_files" baseColumnNames="file_id"
constraintName="fk_file_paper_files" referencedTableName="file"
referencedColumnNames="id"/>
</changeSet> </changeSet>
</databaseChangeLog> </databaseChangeLog>

View File

@ -91,6 +91,28 @@
<p th:if="${#fields.hasErrors('authorIds')}" th:errors="*{authorIds}" <p th:if="${#fields.hasErrors('authorIds')}" th:errors="*{authorIds}"
class="alert alert-danger">Incorrect title</p> class="alert alert-danger">Incorrect title</p>
</div> </div>
<div class="form-group">
<label>Файлы:</label>
<th:block th:each="file, rowStat : *{files}">
<div class="row" th:id="|files${rowStat.index}|"
th:style="${file.deleteFlag} ? 'display: none;' :''">
<input type="hidden" th:field="*{files[__${rowStat.index}__].id}"/>
<input type="hidden" th:field="*{files[__${rowStat.index}__].deleteFlag}"/>
<input type="hidden" th:field="*{files[__${rowStat.index}__].name}"/>
<div class="col-2">
<a class="btn btn-danger float-right"
th:onclick="|$('#files${rowStat.index}\\.deleteFlag').val('true'); $('#files${rowStat.index}').hide(); |">
<span aria-hidden="true"><i class="fa fa-times"/></span>
</a>
</div>
<div class="col-10">
<div th:text="*{files[__${rowStat.index}__].name}"></div>
</div>
</div>
</th:block>
</div>
<div class="form-check"> <div class="form-check">
<input type="checkbox" class="form-check-input" id="locked" <input type="checkbox" class="form-check-input" id="locked"
th:field="*{locked}"/> th:field="*{locked}"/>