From 0239b0afd479e8f59ba4b704dd369c04fdb36ff9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 3 Mar 2023 10:44:26 +0400 Subject: [PATCH] #91 -- Add save operation --- .../TimeSeriesMarkupController.java | 14 ++++++++++---- .../ulstu/extractor/markup/model/Markup.java | 8 ++++++++ .../extractor/markup/model/MarkupForm.java | 4 ++++ .../markup/repository/MarkupRepository.java | 7 +++++++ .../markup/service/MarkupService.java | 19 +++++++++++++++++++ src/main/resources/templates/markup.html | 3 ++- 6 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/markup/repository/MarkupRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java 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 c816987..9eeab36 100644 --- a/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java +++ b/src/main/java/ru/ulstu/extractor/markup/controller/TimeSeriesMarkupController.java @@ -3,10 +3,12 @@ package ru.ulstu.extractor.markup.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.RequestBody; +import ru.ulstu.extractor.core.Route; import ru.ulstu.extractor.markup.model.MarkupForm; import ru.ulstu.extractor.markup.model.MarkupRow; +import ru.ulstu.extractor.markup.service.MarkupService; import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.service.TimeSeriesService; import springfox.documentation.annotations.ApiIgnore; @@ -18,9 +20,12 @@ import java.util.stream.Collectors; @ApiIgnore public class TimeSeriesMarkupController { private final TimeSeriesService timeSeriesService; + private final MarkupService markupService; - public TimeSeriesMarkupController(TimeSeriesService timeSeriesService) { + public TimeSeriesMarkupController(TimeSeriesService timeSeriesService, + MarkupService markupService) { this.timeSeriesService = timeSeriesService; + this.markupService = markupService; } @GetMapping("time-series-markup") @@ -30,8 +35,9 @@ public class TimeSeriesMarkupController { return "markup"; } - @PostMapping("addMarkups") - public String addMarkups(@RequestBody MarkupForm markupForm) { + @PostMapping(Route.ADD_MARKUP) + public String addMarkups(@ModelAttribute MarkupForm markupForm) { + 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 84c6d80..0117bc8 100644 --- a/src/main/java/ru/ulstu/extractor/markup/model/Markup.java +++ b/src/main/java/ru/ulstu/extractor/markup/model/Markup.java @@ -12,6 +12,14 @@ public class Markup extends BaseEntity { private TimeSeries timeSeries; private String markup; + public Markup() { + } + + public Markup(MarkupRow markupRow) { + this.timeSeries = markupRow.getTimeSeries(); + this.markup = markupRow.getMarkup(); + } + public TimeSeries getTimeSeries() { return timeSeries; } 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 987103a..e197b6b 100644 --- a/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java +++ b/src/main/java/ru/ulstu/extractor/markup/model/MarkupForm.java @@ -12,4 +12,8 @@ public class MarkupForm { public List getMarkupRows() { return markupRows; } + + public void setMarkupRows(List markupRows) { + this.markupRows = markupRows; + } } diff --git a/src/main/java/ru/ulstu/extractor/markup/repository/MarkupRepository.java b/src/main/java/ru/ulstu/extractor/markup/repository/MarkupRepository.java new file mode 100644 index 0000000..0d149f1 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/markup/repository/MarkupRepository.java @@ -0,0 +1,7 @@ +package ru.ulstu.extractor.markup.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.markup.model.Markup; + +public interface MarkupRepository extends JpaRepository { +} diff --git a/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java new file mode 100644 index 0000000..266240c --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/markup/service/MarkupService.java @@ -0,0 +1,19 @@ +package ru.ulstu.extractor.markup.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.markup.model.Markup; +import ru.ulstu.extractor.markup.model.MarkupForm; +import ru.ulstu.extractor.markup.repository.MarkupRepository; + +@Service +public class MarkupService { + private final MarkupRepository markupRepository; + + public MarkupService(MarkupRepository markupRepository) { + this.markupRepository = markupRepository; + } + + public void saveMarkup(MarkupForm markupForm) { + markupForm.getMarkupRows().forEach(markupRow -> markupRepository.save(new Markup(markupRow))); + } +} diff --git a/src/main/resources/templates/markup.html b/src/main/resources/templates/markup.html index 3579887..6417ba2 100644 --- a/src/main/resources/templates/markup.html +++ b/src/main/resources/templates/markup.html @@ -7,7 +7,7 @@
-
+ @@ -21,6 +21,7 @@
+