#21 -- Save values and fix front for files upload
This commit is contained in:
parent
dab6c030e5
commit
f5c89b0897
@ -2,6 +2,7 @@ package ru.ulstu.file.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Lob;
|
||||
import jakarta.persistence.Table;
|
||||
import ru.ulstu.model.BaseEntity;
|
||||
|
||||
@ -17,11 +18,9 @@ public class FileData extends BaseEntity {
|
||||
@Column(name = "create_date")
|
||||
private Date createDate;
|
||||
|
||||
@Lob
|
||||
private byte[] data;
|
||||
|
||||
@Column(name = "is_latex_attach")
|
||||
private Boolean isLatexAttach;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@ -53,12 +52,4 @@ public class FileData extends BaseEntity {
|
||||
public void setData(byte[] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Boolean isLatexAttach() {
|
||||
return isLatexAttach;
|
||||
}
|
||||
|
||||
public void setLatexAttach(Boolean latexAttach) {
|
||||
isLatexAttach = latexAttach;
|
||||
}
|
||||
}
|
||||
|
@ -1,39 +1,37 @@
|
||||
package ru.ulstu.report.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import ru.ulstu.aspirant.service.AspirantService;
|
||||
import ru.ulstu.indicator.service.IndicatorService;
|
||||
import ru.ulstu.report.model.dto.ReportValueDto;
|
||||
import ru.ulstu.report.service.ReportPeriodService;
|
||||
import ru.ulstu.report.service.ReportService;
|
||||
import ru.ulstu.report.service.ReportValueService;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("report-value")
|
||||
public class ReportValueController {
|
||||
private final IndicatorService indicatorService;
|
||||
private final AspirantService aspirantService;
|
||||
private final ReportService reportService;
|
||||
private final ReportPeriodService reportPeriodService;
|
||||
private final ReportValueService reportValueService;
|
||||
|
||||
public ReportValueController(IndicatorService indicatorService,
|
||||
AspirantService aspirantService,
|
||||
ReportService reportService,
|
||||
ReportPeriodService reportPeriodService) {
|
||||
this.indicatorService = indicatorService;
|
||||
this.aspirantService = aspirantService;
|
||||
this.reportService = reportService;
|
||||
this.reportPeriodService = reportPeriodService;
|
||||
public ReportValueController(ReportValueService reportValueService) {
|
||||
this.reportValueService = reportValueService;
|
||||
}
|
||||
|
||||
@GetMapping("edit-report-value/{reportId}/{indicatorId}")
|
||||
public String getReportPeriods(@PathVariable("reportId") Integer reportId,
|
||||
@PathVariable("indicatorId") Integer indicatorId,
|
||||
Model model) {
|
||||
model.addAttribute("reportValue", new ReportValueDto(reportId, indicatorService.getIndicatorById(indicatorId)));
|
||||
public String getReportValue(@PathVariable("reportId") Integer reportId,
|
||||
@PathVariable("indicatorId") Integer indicatorId,
|
||||
Model model) {
|
||||
model.addAttribute("reportValue", reportValueService.getByIndicatorId(reportId, indicatorId));
|
||||
return "report/editReportValue";
|
||||
}
|
||||
|
||||
@PostMapping("save-report-value")
|
||||
public String saveReportValue(@Valid ReportValueDto reportValueDto, Model model) throws IOException {
|
||||
reportValueService.saveReportValue(reportValueDto);
|
||||
return "redirect:/report/edit-report/" + reportValueDto.getReportId();
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,11 @@ package ru.ulstu.report.model.dto;
|
||||
|
||||
import ru.ulstu.file.model.FileDataDto;
|
||||
import ru.ulstu.indicator.model.Indicator;
|
||||
import ru.ulstu.report.model.ReportValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ReportValueDto {
|
||||
private Integer id = 0;
|
||||
@ -25,6 +27,14 @@ public class ReportValueDto {
|
||||
this.indicator = indicator;
|
||||
}
|
||||
|
||||
public ReportValueDto(ReportValue reportValue, Integer reportId) {
|
||||
this.id = reportValue.getId();
|
||||
this.reportId = reportId;
|
||||
this.indicator = reportValue.getIndicator();
|
||||
this.files = reportValue.getFiles().stream().map(FileDataDto::new).collect(Collectors.toList());
|
||||
this.indicatorValue = reportValue.getIndicatorValue();
|
||||
}
|
||||
|
||||
public Indicator getIndicator() {
|
||||
return indicator;
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package ru.ulstu.report.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import ru.ulstu.report.model.ReportValue;
|
||||
|
||||
public interface ReportValueRepository extends JpaRepository<ReportValue, Integer> {
|
||||
@Query("SELECT rv FROM Report r JOIN r.values rv WHERE r.id = :reportId AND rv.indicator.id = :indicatorId")
|
||||
ReportValue findByReportIdAndIndicatorId(@Param("reportId") Integer reportId, @Param("indicatorId") Integer indicatorId);
|
||||
}
|
@ -5,6 +5,7 @@ import ru.ulstu.aspirant.model.Aspirant;
|
||||
import ru.ulstu.aspirant.service.AspirantService;
|
||||
import ru.ulstu.report.model.Report;
|
||||
import ru.ulstu.report.model.ReportPeriod;
|
||||
import ru.ulstu.report.model.ReportValue;
|
||||
import ru.ulstu.report.model.dto.ReportDto;
|
||||
import ru.ulstu.report.repository.ReportRepository;
|
||||
import ru.ulstu.user.UserService;
|
||||
@ -61,4 +62,10 @@ public class ReportService {
|
||||
public void deleteReport(Integer id) {
|
||||
reportRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public void addReportValue(Integer reportId, ReportValue reportValue) {
|
||||
Report report = getReportById(reportId);
|
||||
report.getValues().add(reportValue);
|
||||
reportRepository.save(report);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,64 @@
|
||||
package ru.ulstu.report.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.file.service.FileService;
|
||||
import ru.ulstu.indicator.service.IndicatorService;
|
||||
import ru.ulstu.report.model.ReportValue;
|
||||
import ru.ulstu.report.model.dto.ReportValueDto;
|
||||
import ru.ulstu.report.repository.ReportValueRepository;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Service
|
||||
public class ReportValueService {
|
||||
private final ReportValueRepository reportValueRepository;
|
||||
private final IndicatorService indicatorService;
|
||||
private final FileService fileService;
|
||||
private final ReportService reportService;
|
||||
|
||||
public ReportValueService(ReportValueRepository reportValueRepository,
|
||||
IndicatorService indicatorService,
|
||||
FileService fileService,
|
||||
ReportService reportService) {
|
||||
this.reportValueRepository = reportValueRepository;
|
||||
this.indicatorService = indicatorService;
|
||||
this.fileService = fileService;
|
||||
this.reportService = reportService;
|
||||
}
|
||||
|
||||
public ReportValue saveReportValue(ReportValueDto reportValueDto) throws IOException {
|
||||
ReportValue reportValue;
|
||||
if (reportValueDto.getId() == null || reportValueDto.getId() == 0) {
|
||||
reportValue = new ReportValue();
|
||||
} else {
|
||||
reportValue = getReportValueById(reportValueDto.getId());
|
||||
}
|
||||
reportValue.setIndicatorValue(reportValueDto.getIndicatorValue());
|
||||
reportValue.setIndicator(indicatorService.getIndicatorById(reportValueDto.getIndicator().getId()));
|
||||
reportValue.setFiles(fileService.saveOrCreate(reportValueDto.getFiles().stream()
|
||||
.filter(f -> !f.isDeleted())
|
||||
.toList()));
|
||||
reportValue = reportValueRepository.save(reportValue);
|
||||
reportService.addReportValue(reportValueDto.getReportId(), reportValue);
|
||||
|
||||
return reportValue;
|
||||
}
|
||||
|
||||
public ReportValue getReportValueById(Integer reportValueId) {
|
||||
return reportValueRepository
|
||||
.findById(reportValueId)
|
||||
.orElseThrow(() -> new RuntimeException("Report value not found by id"));
|
||||
}
|
||||
|
||||
public void deleteReportValue(Integer id) {
|
||||
reportValueRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public ReportValueDto getByIndicatorId(Integer reportId, Integer indicatorId) {
|
||||
ReportValue reportValue = reportValueRepository.findByReportIdAndIndicatorId(reportId, indicatorId);
|
||||
if (reportValue == null) {
|
||||
return new ReportValueDto(reportId, indicatorService.getIndicatorById(indicatorId));
|
||||
}
|
||||
return new ReportValueDto(reportValue, reportId);
|
||||
}
|
||||
}
|
@ -2,21 +2,15 @@
|
||||
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
|
||||
layout:decorate="~{default}">
|
||||
<div class="container" layout:fragment="content">
|
||||
<h3 th:text="${'Редактирование показателя '+ reportValue.indicator.name}"></h3>
|
||||
<form action="#" th:action="@{/report/save-report-value}"
|
||||
th:object="${reportValue}"
|
||||
method="post">
|
||||
<h3>Редактирование показателя</h3>
|
||||
<h3 th:text="${reportValue.indicator.name}"></h3>
|
||||
|
||||
<form action="#" th:action="@{/report-value/save-report-value}"
|
||||
th:object="${reportValue}" method="post">
|
||||
<input type="hidden" th:field="*{id}">
|
||||
<input type="hidden" th:field="*{reportId}">
|
||||
<input type="hidden" th:field="*{indicator.id}">
|
||||
|
||||
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
|
||||
<button name="delete"
|
||||
type="submit"
|
||||
class="btn btn-outline-dark"
|
||||
onclick="return confirm('Удалить запись?')">
|
||||
Удалить
|
||||
</button>
|
||||
<a href="${'/report/edit-report/'+ reportValue.reportId}" class="btn btn-outline-dark">Отмена</a>
|
||||
<div class="form-group files-list" id="files-list">
|
||||
<label class="form-label">Загрузка подтверждающих документов</label>
|
||||
<div th:each="file, rowStat : ${reportValue.files}">
|
||||
@ -37,8 +31,13 @@
|
||||
</a>
|
||||
</div>
|
||||
<div class="col-10">
|
||||
<a th:onclick="${file.id==null} ? 'downloadFile('+${file.tmpFileName}+',null,\''+${file.name}+'\')':
|
||||
'downloadFile(null,'+${file.id}+',\''+${file.name}+'\')' "
|
||||
<a th:if="${file.id != null}"
|
||||
th:onclick="downloadFile(null, [[${file.id}]], [[${file.name}]])"
|
||||
href="javascript:void(0)"
|
||||
th:text="*{files[__${rowStat.index}__].name}">
|
||||
</a>
|
||||
<a th:if="${file.id == null}"
|
||||
th:onclick="downloadFile([[${file.tmpFileName}]],null,[[${file.name}]])"
|
||||
href="javascript:void(0)"
|
||||
th:text="*{files[__${rowStat.index}__].name}">
|
||||
</a>
|
||||
@ -52,7 +51,18 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
|
||||
<button name="delete"
|
||||
type="submit"
|
||||
class="btn btn-outline-dark"
|
||||
onclick="return confirm('Удалить запись?')">
|
||||
Удалить
|
||||
</button>
|
||||
<a href="${'/report/edit-report/' + reportValue.reportId}" class="btn btn-outline-dark">Отмена</a>
|
||||
</form>
|
||||
|
||||
|
||||
<script type="text/javascript" src="/js/file-loader.js"></script>
|
||||
<script>
|
||||
/*<![CDATA[*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user