From d7790dff700c3e0a1c1ff96ac03f0f1e51365c63 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 7 Mar 2023 16:59:17 +0400 Subject: [PATCH] #91 -- Save markup --- .../TimeSeriesMarkupController.java | 35 ++++++++++++++++--- .../ulstu/extractor/markup/model/Markup.java | 6 ++-- .../extractor/markup/model/MarkupForm.java | 27 ++++++++++++++ .../markup/service/MarkupService.java | 14 ++++++-- src/main/resources/templates/markup.html | 25 +++++++++++++ 5 files changed, 98 insertions(+), 9 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java b/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java index 0c1600c..0672544 100644 --- a/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java +++ b/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java @@ -5,6 +5,7 @@ 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 ru.ulstu.extractor.branch.service.BranchService; import ru.ulstu.extractor.core.Route; import ru.ulstu.extractor.markup.model.MarkupForm; import ru.ulstu.extractor.markup.model.MarkupRow; @@ -20,24 +21,50 @@ import java.util.stream.Collectors; @ApiIgnore public class TimeSeriesMarkupController { private final TimeSeriesService timeSeriesService; + private final BranchService branchService; private final MarkupService markupService; public TimeSeriesMarkupController(TimeSeriesService timeSeriesService, + BranchService branchService, MarkupService markupService) { this.timeSeriesService = timeSeriesService; + this.branchService = branchService; this.markupService = markupService; } @GetMapping("time-series-markup") public String markupTs(Model model) { - List tss = timeSeriesService.getAllTimeSeries(); - model.addAttribute("markupForm", new MarkupForm(tss.stream().map(MarkupRow::new).limit(200).collect(Collectors.toList()))); + model.addAttribute("branches", branchService.findAll()); + model.addAttribute("markupForm", new MarkupForm()); + return "markup"; + } + + @PostMapping("time-series-markup") + public String filter(Model model, @ModelAttribute MarkupForm markupForm) { + model.addAttribute("branches", branchService.findAll()); + if (markupForm != null && markupForm.getBranchId() != null) { + List tss = timeSeriesService.getByBranch(markupForm.getBranchId()); + MarkupForm markupFormWithTs = new MarkupForm(tss.stream().map(MarkupRow::new).limit(200).collect(Collectors.toList())); + markupFormWithTs.setBranchId(markupForm.getBranchId()); + model.addAttribute("markupForm", markupFormWithTs); + } else { + model.addAttribute("markupForm", new MarkupForm()); + } return "markup"; } @PostMapping(Route.ADD_MARKUP) - public String addMarkups(@ModelAttribute MarkupForm markupForm) { - markupService.saveMarkup(markupForm); + public String addMarkups(Model model, @ModelAttribute MarkupForm markupForm) { + model.addAttribute("branches", branchService.findAll()); + if (markupForm.getBranchId() != null) { + List tss = timeSeriesService.getByBranch(markupForm.getBranchId()); + MarkupForm markupFormWithTs = new MarkupForm(tss.stream().map(MarkupRow::new).limit(200).collect(Collectors.toList())); + markupFormWithTs.setBranchId(markupForm.getBranchId()); + model.addAttribute("markupForm", markupFormWithTs); + if (markupForm.getMarkupRows() != null && !markupForm.getMarkupRows().isEmpty()) { + markupService.saveMarkup(markupForm); + } + } return "markup"; } } diff --git a/src/main/java/ru/ulstu/extractor/markup/model/Markup.java b/src/main/java/ru/ulstu/extractor/markup/model/Markup.java index 0117bc8..f119e41 100644 --- a/src/main/java/ru/ulstu/extractor/markup/model/Markup.java +++ b/src/main/java/ru/ulstu/extractor/markup/model/Markup.java @@ -15,9 +15,9 @@ public class Markup extends BaseEntity { public Markup() { } - public Markup(MarkupRow markupRow) { - this.timeSeries = markupRow.getTimeSeries(); - this.markup = markupRow.getMarkup(); + public Markup(TimeSeries timeSeries, String markup) { + this.timeSeries = timeSeries; + this.markup = markup; } public TimeSeries getTimeSeries() { diff --git a/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java b/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java index b81a110..c07c837 100644 --- a/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java +++ b/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java @@ -5,6 +5,9 @@ import java.util.List; public class MarkupForm { private List markupRows = new ArrayList<>(); + private Integer branchId; + private boolean save; + private boolean filter; public MarkupForm() { } @@ -20,4 +23,28 @@ public class MarkupForm { public void setMarkupRows(List markupRows) { this.markupRows = markupRows; } + + public Integer getBranchId() { + return branchId; + } + + public void setBranchId(Integer branchId) { + this.branchId = branchId; + } + + public boolean isSave() { + return save; + } + + public void setSave(boolean save) { + this.save = save; + } + + public boolean isFilter() { + return filter; + } + + public void setFilter(boolean filter) { + this.filter = filter; + } } diff --git a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java index 266240c..ae3e35f 100644 --- a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java +++ b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java @@ -1,19 +1,29 @@ package ru.ulstu.extractor.markup.service; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import ru.ulstu.extractor.markup.model.Markup; import ru.ulstu.extractor.markup.model.MarkupForm; import ru.ulstu.extractor.markup.repository.MarkupRepository; +import ru.ulstu.extractor.ts.model.TimeSeries; +import ru.ulstu.extractor.ts.service.TimeSeriesService; @Service public class MarkupService { private final MarkupRepository markupRepository; + private final TimeSeriesService timeSeriesService; - public MarkupService(MarkupRepository markupRepository) { + public MarkupService(MarkupRepository markupRepository, + TimeSeriesService timeSeriesService) { this.markupRepository = markupRepository; + this.timeSeriesService = timeSeriesService; } + @Transactional public void saveMarkup(MarkupForm markupForm) { - markupForm.getMarkupRows().forEach(markupRow -> markupRepository.save(new Markup(markupRow))); + markupForm.getMarkupRows().forEach(markupRow -> { + TimeSeries timeSeries = timeSeriesService.getById(markupRow.getTimeSeries().getId()); + markupRepository.save(new Markup(timeSeries, markupRow.getMarkup())); + }); } } diff --git a/src/main/resources/templates/markup.html b/src/main/resources/templates/markup.html index b2b1d4e..c0c98a7 100644 --- a/src/main/resources/templates/markup.html +++ b/src/main/resources/templates/markup.html @@ -8,6 +8,31 @@
+
+
+ Репозиторий-ветка +
+
+ + +
+
+ +
+