From 131449c8c3b371dcd78e1ccbfc48390ccec205dd Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Mon, 3 Mar 2025 11:59:38 +0400 Subject: [PATCH] #2 -- Fix using dto --- .../rule/controller/FuzzyRuleController.java | 9 ++++---- .../ulstu/fc/rule/model/dto/FuzzyTermDto.java | 20 ++++++++++++++++ .../ulstu/fc/rule/model/dto/VariableDto.java | 23 ++++++++++++++++++- .../fc/rule/service/VariableService.java | 8 +++++++ src/main/resources/public/js/fuzzyRule.js | 14 +++-------- .../resources/templates/project/edit.html | 4 ++-- 6 files changed, 59 insertions(+), 19 deletions(-) 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 a19f79f..4ae9a30 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyRuleController.java @@ -12,7 +12,7 @@ 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.Variable; +import ru.ulstu.fc.rule.model.dto.VariableDto; import ru.ulstu.fc.rule.service.FuzzyRuleService; import ru.ulstu.fc.rule.service.FuzzyTermService; import ru.ulstu.fc.rule.service.VariableService; @@ -65,11 +65,10 @@ public class FuzzyRuleController { @ResponseBody @GetMapping("/getVariables/{projectId}") - public List getVariables(@PathVariable("projectId") Integer projectId, - final HttpServletResponse response) { + public List getVariables(@PathVariable("projectId") Integer projectId, + final HttpServletResponse response) { response.addHeader("Cache-Control", "max-age=60, must-revalidate, no-transform"); - //TODO: return DTO without terms - return variableService.getAllByProject(projectId); + return variableService.getAllDtoByProject(projectId); } @ResponseBody diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java index 3dd831a..85a3bf9 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/FuzzyTermDto.java @@ -1,9 +1,21 @@ package ru.ulstu.fc.rule.model.dto; +import ru.ulstu.fc.rule.model.FuzzyTerm; + public class FuzzyTermDto { + private Integer id; private String description; private Double crispValue; + public FuzzyTermDto() { + } + + public FuzzyTermDto(FuzzyTerm fuzzyTerm) { + this.description = fuzzyTerm.getDescription(); + this.crispValue = fuzzyTerm.getCrispValue(); + this.id = fuzzyTerm.getId(); + } + public String getDescription() { return description; } @@ -19,4 +31,12 @@ public class FuzzyTermDto { public void setCrispValue(Double crispValue) { this.crispValue = crispValue; } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } } diff --git a/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java index 46828b8..f708cfa 100644 --- a/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java +++ b/src/main/java/ru/ulstu/fc/rule/model/dto/VariableDto.java @@ -1,10 +1,23 @@ package ru.ulstu.fc.rule.model.dto; +import ru.ulstu.fc.rule.model.Variable; + +import java.util.ArrayList; import java.util.List; public class VariableDto { + private Integer id; private String name; - private List terms; + private List terms = new ArrayList<>(); + + public VariableDto() { + } + + public VariableDto(Variable variable) { + this.id = variable.getId(); + this.name = variable.getName(); + this.terms = variable.getFuzzyTerms().stream().map(FuzzyTermDto::new).toList(); + } public String getName() { return name; @@ -21,4 +34,12 @@ public class VariableDto { public void setTerms(List terms) { this.terms = terms; } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } } diff --git a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java index 732aebb..44e94c3 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -5,6 +5,7 @@ import ru.ulstu.fc.project.service.ProjectService; import ru.ulstu.fc.rule.model.FuzzyTerm; import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.model.VariableForm; +import ru.ulstu.fc.rule.model.dto.VariableDto; import ru.ulstu.fc.rule.repository.VariableRepository; import java.util.List; @@ -69,6 +70,13 @@ public class VariableService { return variableRepository.getByProject(projectService.getById(projectId)); } + public List getAllDtoByProject(Integer projectId) { + return variableRepository.getByProject(projectService.getById(projectId)) + .stream() + .map(VariableDto::new) + .toList(); + } + public void checkIsCurrentUserVariableWithThrow(Variable variable) { projectService.checkIsCurrentUserProjectWithThrow(variable.getProject()); } diff --git a/src/main/resources/public/js/fuzzyRule.js b/src/main/resources/public/js/fuzzyRule.js index e678cd9..9838bd7 100644 --- a/src/main/resources/public/js/fuzzyRule.js +++ b/src/main/resources/public/js/fuzzyRule.js @@ -5,8 +5,7 @@ function getAntecedent(rule) { return withoutIf[1].trim().split('then')[0].trim(); } -// TODO: remove duplicate -function getAntecedentComponents(antecedent) { +function getRuleComponents(antecedent) { return antecedent.split('and').map((i) => i.trim()); } @@ -16,11 +15,6 @@ function getConsequent(rule) { return withoutIf[1].trim().split('then')[1].trim(); } -// TODO: remove duplicate -function getConsequentComponents(consequent) { - return consequent.split('and').map((i) => i.trim()); -} - // common function getVariable(variableComponents) { return variableComponents.split('is')[0].trim(); @@ -56,8 +50,6 @@ function showFeedbackMessage(message, type) { /* exported errorHandler */ function errorHandler(response, callBack, errorCallBack) { if (!isEmpty(response.error)) { - // TODO: add l10n - // showFeedbackMessage(response.error.code + ": " + response.error.message, MessageTypesEnum.DANGER); if (!isEmpty(errorCallBack)) { errorCallBack(response.data); } @@ -237,7 +229,7 @@ function addConsequent(parentElement, projectId, variableVal, termVal) { } function addAntecedentFromRule(parentElement, projectId, ruleContent) { - let antecedentComponents = getAntecedentComponents(getAntecedent(ruleContent)); + let antecedentComponents = getRuleComponents(getAntecedent(ruleContent)); for (let i = 0; i < antecedentComponents.length; i++) { let a = antecedentComponents[i]; addAntecedent(parentElement, projectId, getVariable(a), getVariableValue(a)); @@ -245,7 +237,7 @@ function addAntecedentFromRule(parentElement, projectId, ruleContent) { } function addConsequentFromRule(parentElement, projectId, ruleContent) { - let consequentComponents = getConsequentComponents(getConsequent(ruleContent)); + let consequentComponents = getRuleComponents(getConsequent(ruleContent)); for (let i = 0; i < consequentComponents.length; i++) { let c = consequentComponents[i]; addConsequent(parentElement, projectId, getVariable(c), getVariableValue(c)); diff --git a/src/main/resources/templates/project/edit.html b/src/main/resources/templates/project/edit.html index 9ab2669..209e768 100644 --- a/src/main/resources/templates/project/edit.html +++ b/src/main/resources/templates/project/edit.html @@ -61,8 +61,8 @@