From 37fcc9bdcda2143f2cf728ac0ab581abc87bf64e Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 14 Mar 2025 14:52:12 +0400 Subject: [PATCH] #25 -- Fix rules dto --- .../project/service/ProjectRulesService.java | 6 +++++ .../rule/controller/FuzzyRuleController.java | 24 +++++++++---------- .../controller/FuzzyRuleRestController.java | 22 ++++++++--------- .../FuzzyRuleDto.java} | 11 +++++---- .../fc/rule/service/FuzzyRuleService.java | 10 +++++--- src/main/resources/templates/rule/edit.html | 2 +- 6 files changed, 42 insertions(+), 33 deletions(-) rename src/main/java/ru/ulstu/fc/rule/model/{FuzzyRuleForm.java => dto/FuzzyRuleDto.java} (79%) diff --git a/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java b/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java index 52f1d72..91e9e24 100644 --- a/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java +++ b/src/main/java/ru/ulstu/fc/project/service/ProjectRulesService.java @@ -2,10 +2,12 @@ package ru.ulstu.fc.project.service; import org.springframework.stereotype.Service; import ru.ulstu.fc.rule.model.FuzzyRule; +import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto; import ru.ulstu.fc.rule.repository.FuzzyRuleRepository; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; @Service public class ProjectRulesService { @@ -24,4 +26,8 @@ public class ProjectRulesService { } return ruleRepository.findByProject(projectService.getById(projectId)); } + + public List getDtoByProjectId(Integer projectId) { + return getByProjectId(projectId).stream().map(FuzzyRuleDto::new).collect(Collectors.toList()); + } } diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java index e5f6f10..7bc5796 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java @@ -11,8 +11,8 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; -import ru.ulstu.fc.rule.model.FuzzyRuleForm; import ru.ulstu.fc.rule.model.FuzzyTerm; +import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto; import ru.ulstu.fc.rule.model.dto.VariableDto; import ru.ulstu.fc.rule.service.FuzzyRuleService; import ru.ulstu.fc.rule.service.FuzzyTermService; @@ -40,29 +40,29 @@ public class FuzzyRuleController { public String edit(@PathVariable(value = "projectId") Integer projectId, @PathVariable(value = "ruleId") Integer id, Model model) { model.addAttribute("projectId", projectId); - model.addAttribute("fuzzyRuleForm", + model.addAttribute("fuzzyRuleDto", (id != null && id != 0) - ? new FuzzyRuleForm(id, fuzzyRuleService.getById(id)) - : new FuzzyRuleForm(id, projectId)); + ? new FuzzyRuleDto(fuzzyRuleService.getById(id)) + : new FuzzyRuleDto(id, projectId)); return "rule/edit"; } @PostMapping(value = "save", params = "save") - public String save(@Valid FuzzyRuleForm fuzzyRuleForm, BindingResult bindingResult, Model model) { + public String save(@Valid FuzzyRuleDto fuzzyRuleDto, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { - model.addAttribute("projectId", fuzzyRuleForm.getProjectId()); + model.addAttribute("projectId", fuzzyRuleDto.getProjectId()); return "rule/edit"; } - fuzzyRuleService.save(fuzzyRuleForm); - return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); + fuzzyRuleService.save(fuzzyRuleDto); + return "redirect:/project/edit/" + fuzzyRuleDto.getProjectId(); } @PostMapping(value = "save", params = "delete") - public String delete(FuzzyRuleForm fuzzyRuleForm) { - if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) { - fuzzyRuleService.delete(fuzzyRuleForm); + public String delete(FuzzyRuleDto fuzzyRuleDto) { + if (fuzzyRuleDto != null && fuzzyRuleDto.getId() != null) { + fuzzyRuleService.delete(fuzzyRuleDto); } - return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); + return "redirect:/project/edit/" + fuzzyRuleDto.getProjectId(); } @ResponseBody diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java index 5cd2bea..b08fa01 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleRestController.java @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ru.ulstu.fc.project.service.ProjectRulesService; import ru.ulstu.fc.rule.model.FuzzyRule; -import ru.ulstu.fc.rule.model.FuzzyRuleForm; +import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto; import ru.ulstu.fc.rule.service.FuzzyRuleParseService; import ru.ulstu.fc.rule.service.FuzzyRuleService; @@ -31,26 +31,24 @@ public class FuzzyRuleRestController { } @GetMapping("/getAll/{projectId}") - public List getAll(@PathVariable(value = "projectId") Integer projectId) { - //TODO: return dto - return projectRulesService.getByProjectId(projectId); + public List getAll(@PathVariable(value = "projectId") Integer projectId) { + return projectRulesService.getDtoByProjectId(projectId); } @GetMapping("/get/{ruleId}") - public FuzzyRule get(@PathVariable(value = "ruleId") Integer id) { - //TODO: return dto - return ruleService.getById(id); + public FuzzyRuleDto get(@PathVariable(value = "ruleId") Integer id) { + return ruleService.getByIdDto(id); } @PostMapping - public FuzzyRule save(@Valid FuzzyRuleForm fuzzyRuleForm) { - return ruleService.save(fuzzyRuleForm); + public FuzzyRule save(@Valid FuzzyRuleDto fuzzyRuleDto) { + return ruleService.save(fuzzyRuleDto); } @DeleteMapping - public void delete(@Valid FuzzyRuleForm fuzzyRuleForm) { - if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) { - ruleService.delete(fuzzyRuleForm); + public void delete(@Valid FuzzyRuleDto fuzzyRuleDto) { + if (fuzzyRuleDto != null && fuzzyRuleDto.getId() != null) { + ruleService.delete(fuzzyRuleDto); } } diff --git a/src/main/java/ru/ulstu/fc/rule/model/FuzzyRuleForm.java b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDto.java similarity index 79% rename from src/main/java/ru/ulstu/fc/rule/model/FuzzyRuleForm.java rename to src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDto.java index 5ae6d32..726c6b1 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/FuzzyRuleForm.java +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyRuleDto.java @@ -1,9 +1,10 @@ -package ru.ulstu.fc.rule.model; +package ru.ulstu.fc.rule.model.dto; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import ru.ulstu.fc.rule.model.FuzzyRule; -public class FuzzyRuleForm { +public class FuzzyRuleDto { private Integer id; @NotNull private Integer projectId; @@ -11,15 +12,15 @@ public class FuzzyRuleForm { @Size(min = 5, max = 250, message = "Длина от 5 до 250 символов") private String content; - public FuzzyRuleForm() { + public FuzzyRuleDto() { } - public FuzzyRuleForm(Integer id, Integer projectId) { + public FuzzyRuleDto(Integer id, Integer projectId) { this.id = id; this.projectId = projectId; } - public FuzzyRuleForm(Integer id, FuzzyRule fuzzyRule) { + public FuzzyRuleDto(FuzzyRule fuzzyRule) { this.id = fuzzyRule.getId(); this.projectId = fuzzyRule.getProject().getId(); this.content = fuzzyRule.getContent(); diff --git a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java index bde8fb2..bd4b617 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/FuzzyRuleService.java @@ -3,7 +3,7 @@ package ru.ulstu.fc.rule.service; import org.springframework.stereotype.Service; import ru.ulstu.fc.project.service.ProjectService; import ru.ulstu.fc.rule.model.FuzzyRule; -import ru.ulstu.fc.rule.model.FuzzyRuleForm; +import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto; import ru.ulstu.fc.rule.repository.FuzzyRuleRepository; @Service @@ -25,7 +25,11 @@ public class FuzzyRuleService { return fuzzyRule; } - public FuzzyRule save(FuzzyRuleForm ruleForm) { + public FuzzyRuleDto getByIdDto(Integer id) { + return new FuzzyRuleDto(getById(id)); + } + + public FuzzyRule save(FuzzyRuleDto ruleForm) { FuzzyRule rule; if (ruleForm.getId() == null || ruleForm.getId() == 0) { rule = new FuzzyRule(); @@ -37,7 +41,7 @@ public class FuzzyRuleService { return ruleRepository.save(rule); } - public void delete(FuzzyRuleForm ruleForm) { + public void delete(FuzzyRuleDto ruleForm) { ruleRepository.delete(getById(ruleForm.getId())); } diff --git a/src/main/resources/templates/rule/edit.html b/src/main/resources/templates/rule/edit.html index 70fa5ec..b2984c5 100644 --- a/src/main/resources/templates/rule/edit.html +++ b/src/main/resources/templates/rule/edit.html @@ -9,7 +9,7 @@

Редактирование правила:

-
+