From 3791f10b8e978159272a4a405bb3101a87c87ec9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 5 Apr 2025 23:48:36 +0400 Subject: [PATCH] #21 -- Fix save report --- .../controller/AdminReportController.java | 52 ------------------- .../report/controller/ReportController.java | 21 +++++--- .../ru/ulstu/report/model/dto/ReportDto.java | 13 ++++- .../ulstu/report/service/ReportService.java | 27 +++++++++- .../templates/report/editReport.html | 12 ++--- .../templates/report/reportList.html | 6 +-- 6 files changed, 58 insertions(+), 73 deletions(-) delete mode 100644 src/main/java/ru/ulstu/admin/controller/AdminReportController.java diff --git a/src/main/java/ru/ulstu/admin/controller/AdminReportController.java b/src/main/java/ru/ulstu/admin/controller/AdminReportController.java deleted file mode 100644 index 3018ca4..0000000 --- a/src/main/java/ru/ulstu/admin/controller/AdminReportController.java +++ /dev/null @@ -1,52 +0,0 @@ -package ru.ulstu.admin.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.multipart.MultipartFile; -import ru.ulstu.admin.model.ReportForm; -import ru.ulstu.admin.service.AdminReportService; - -import java.util.List; - -@Controller -@RequestMapping("/admin") -public class AdminReportController { - - private final AdminReportService adminReportService; - - public AdminReportController(AdminReportService adminReportService) { - this.adminReportService = adminReportService; - } - - @GetMapping("/report") - public String getReportForm(Model model) { - ReportForm reportForm = new ReportForm(); - - //todo get current user aspirant id - //reportForm.setAspirantId(aspirantId); // Устанавливаем ID аспиранта - model.addAttribute("report", reportForm); - return "admin/aspirantReport"; // Возвращает шаблон report.html - } - - @PostMapping("/saveReport") - public String saveReport(@ModelAttribute ReportForm reportForm, - @RequestParam("file") List files) { - // Сохраняем отчет - adminReportService.saveReport(reportForm); - - // Обработка загруженных файлов - if (!files.isEmpty()) { - for (MultipartFile file : files) { - System.out.println("Файл: " + file.getOriginalFilename()); - // Здесь можно добавить логику сохранения файла на сервере - } - } - - return "redirect:/admin/aspirants"; // Перенаправление на список аспирантов - } -} \ No newline at end of file diff --git a/src/main/java/ru/ulstu/report/controller/ReportController.java b/src/main/java/ru/ulstu/report/controller/ReportController.java index 93ba8c9..d6395fb 100644 --- a/src/main/java/ru/ulstu/report/controller/ReportController.java +++ b/src/main/java/ru/ulstu/report/controller/ReportController.java @@ -41,6 +41,7 @@ public class ReportController { List periods = reportPeriodService.getReportPeriods(); model.addAttribute("reportPeriods", periods); model.addAttribute("reportListForm", new ReportListForm(periods.isEmpty() ? null : periods.getFirst())); + model.addAttribute("reports", reportService.getReports(periods.isEmpty() ? null : periods.getFirst())); model.addAttribute("canCreate", !periods.isEmpty() && reportService.canCreateReport(periods.getFirst())); return "report/reportList"; } @@ -51,22 +52,28 @@ public class ReportController { model.addAttribute("reports", reportService.getReports(reportListForm.getReportPeriod())); List periods = reportPeriodService.getReportPeriods(); model.addAttribute("reportPeriods", periods); - model.addAttribute("canCreate", !periods.isEmpty() && reportService.canCreateReport(periods.getFirst())); + model.addAttribute("canCreate", !periods.isEmpty() && reportService.canCreateReport(reportListForm.getReportPeriod())); return "report/reportList"; } - @GetMapping("editReport/{reportId}/{reportPeriodId}") - public String createReport(@PathVariable("reportId") Integer reportId, - @PathVariable("reportPeriodId") Integer reportPeriodId, + @GetMapping("new-report/{reportPeriodId}") + public String createReport(@PathVariable("reportPeriodId") Integer reportPeriodId, Model model) { List indicators = aspirantService.getCurrentAspirantIndicators(); - model.addAttribute("report", new ReportDto(reportId, reportPeriodId, indicators)); + model.addAttribute("report", new ReportDto(reportPeriodId, indicators)); return "report/editReport"; } - @PostMapping("saveReport") + @GetMapping("edit-report/{reportId}") + public String editReportWithoutReportPeriod(@PathVariable("reportId") Integer reportId, Model model) { + List indicators = aspirantService.getCurrentAspirantIndicators(); + model.addAttribute("report", new ReportDto(reportService.getReportById(reportId), indicators)); + return "report/editReport"; + } + + @PostMapping("save-report") public String saveReport(@ModelAttribute("report") ReportDto reportDto, Model model) { - System.out.println(reportDto); + reportService.saveReport(reportDto); return "redirect:/report/reportList"; } } diff --git a/src/main/java/ru/ulstu/report/model/dto/ReportDto.java b/src/main/java/ru/ulstu/report/model/dto/ReportDto.java index 92041b0..5dfd591 100644 --- a/src/main/java/ru/ulstu/report/model/dto/ReportDto.java +++ b/src/main/java/ru/ulstu/report/model/dto/ReportDto.java @@ -1,6 +1,8 @@ package ru.ulstu.report.model.dto; +import org.springframework.format.annotation.DateTimeFormat; import ru.ulstu.indicator.model.Indicator; +import ru.ulstu.report.model.Report; import java.util.ArrayList; import java.util.Date; @@ -9,18 +11,25 @@ import java.util.List; public class ReportDto { private Integer id; private Integer reportPeriodId; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") private Date createDate = new Date(); private List reportValues = new ArrayList<>(); public ReportDto() { } - public ReportDto(Integer id, Integer reportPeriodId, List indicators) { - this.id = id; + public ReportDto(Integer reportPeriodId, List indicators) { this.reportPeriodId = reportPeriodId; this.reportValues = indicators.stream().map(ReportValueDto::new).toList(); } + public ReportDto(Report report, List indicators) { + this.id = report.getId(); + this.reportPeriodId = report.getReportPeriod().getId(); + this.createDate = report.getCreateDate(); + this.reportValues = indicators.stream().map(ReportValueDto::new).toList(); + } + public List getReportValues() { return reportValues; } diff --git a/src/main/java/ru/ulstu/report/service/ReportService.java b/src/main/java/ru/ulstu/report/service/ReportService.java index 567b86c..1559b25 100644 --- a/src/main/java/ru/ulstu/report/service/ReportService.java +++ b/src/main/java/ru/ulstu/report/service/ReportService.java @@ -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.dto.ReportDto; import ru.ulstu.report.repository.ReportRepository; import ru.ulstu.user.UserService; @@ -15,13 +16,15 @@ public class ReportService { private final ReportRepository reportRepository; private final UserService userService; private final AspirantService aspirantService; + private final ReportPeriodService reportPeriodService; public ReportService(ReportRepository reportRepository, UserService userService, - AspirantService aspirantService) { + AspirantService aspirantService, ReportPeriodService reportPeriodService) { this.reportRepository = reportRepository; this.userService = userService; this.aspirantService = aspirantService; + this.reportPeriodService = reportPeriodService; } public List getReports(ReportPeriod reportPeriod) { @@ -32,4 +35,26 @@ public class ReportService { Aspirant currentAspirant = aspirantService.getAspirantByUser(userService.getCurrentUser()); return reportRepository.findByReportPeriodAndAspirant(reportPeriod, currentAspirant) == null; } + + public Report saveReport(ReportDto reportDto) { + Report report; + if (reportDto.getId() == null || reportDto.getId() == 0) { + report = new Report(); + } else { + report = getReportById(reportDto.getId()); + } + if (reportDto.getReportPeriodId() != null) { + report.setReportPeriod(reportPeriodService.getById(reportDto.getReportPeriodId())); + } + report.setAspirant(aspirantService.getAspirantByUser(userService.getCurrentUser())); + report.setCreateDate(reportDto.getCreateDate()); + //TODO: add status + return reportRepository.save(report); + } + + public Report getReportById(Integer reportId) { + return reportRepository + .findById(reportId) + .orElseThrow(() -> new RuntimeException("Report not found by id")); + } } diff --git a/src/main/resources/templates/report/editReport.html b/src/main/resources/templates/report/editReport.html index 19c3321..cfc7285 100644 --- a/src/main/resources/templates/report/editReport.html +++ b/src/main/resources/templates/report/editReport.html @@ -3,22 +3,18 @@ layout:decorate="~{default}">

-
+
-
-
+

- -
- -
-
diff --git a/src/main/resources/templates/report/reportList.html b/src/main/resources/templates/report/reportList.html index c88ff70..3b44dff 100644 --- a/src/main/resources/templates/report/reportList.html +++ b/src/main/resources/templates/report/reportList.html @@ -29,7 +29,7 @@
- @@ -49,10 +49,10 @@ - + - + Редактировать