#2 -- Fix using dto
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m9s

This commit is contained in:
Anton Romanov 2025-03-03 11:59:38 +04:00
parent 0dc7124bdf
commit 131449c8c3
6 changed files with 59 additions and 19 deletions

View File

@ -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<Variable> getVariables(@PathVariable("projectId") Integer projectId,
final HttpServletResponse response) {
public List<VariableDto> 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

View File

@ -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;
}
}

View File

@ -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<FuzzyTermDto> terms;
private List<FuzzyTermDto> 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<FuzzyTermDto> terms) {
this.terms = terms;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}

View File

@ -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<VariableDto> getAllDtoByProject(Integer projectId) {
return variableRepository.getByProject(projectService.getById(projectId))
.stream()
.map(VariableDto::new)
.toList();
}
public void checkIsCurrentUserVariableWithThrow(Variable variable) {
projectService.checkIsCurrentUserProjectWithThrow(variable.getProject());
}

View File

@ -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));

View File

@ -61,8 +61,8 @@
<script type="text/javascript">
function addRule(index, el, rule) {
let ruleHtml = "<div class='col col-md-12'><span class='badge badge-light'>" + (index + 1) + ". Если</span></div>"
let antecedentComponents = getAntecedentComponents(getAntecedent(rule));
let consequentComponents = getConsequentComponents(getConsequent(rule));
let antecedentComponents = getRuleComponents(getAntecedent(rule));
let consequentComponents = getRuleComponents(getConsequent(rule));
for (let i = 0; i < antecedentComponents.length; i++) {
let a = antecedentComponents[i];
if (i > 0) {