#25 -- Fix rules dto
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m4s

This commit is contained in:
Anton Romanov 2025-03-14 14:52:12 +04:00
parent 7ab12d6bbe
commit 37fcc9bdcd
6 changed files with 42 additions and 33 deletions

View File

@ -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<FuzzyRuleDto> getDtoByProjectId(Integer projectId) {
return getByProjectId(projectId).stream().map(FuzzyRuleDto::new).collect(Collectors.toList());
}
}

View File

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

View File

@ -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<FuzzyRule> getAll(@PathVariable(value = "projectId") Integer projectId) {
//TODO: return dto
return projectRulesService.getByProjectId(projectId);
public List<FuzzyRuleDto> 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);
}
}

View File

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

View File

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

View File

@ -9,7 +9,7 @@
</head>
<div class="container" layout:fragment="content">
<h3>Редактирование правила:</h3>
<form th:action="@{/rule/save}" th:object="${fuzzyRuleForm}" method="post">
<form th:action="@{/rule/save}" th:object="${fuzzyRuleDto}" method="post">
<input type="hidden" id="projectId" th:field="*{projectId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">