25-controllers #28

Merged
romanov73 merged 3 commits from 25-controllers into master 2025-03-14 15:22:16 +04:00
6 changed files with 42 additions and 33 deletions
Showing only changes of commit 37fcc9bdcd - Show all commits

View File

@ -2,10 +2,12 @@ package ru.ulstu.fc.project.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.model.FuzzyRule; import ru.ulstu.fc.rule.model.FuzzyRule;
import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto;
import ru.ulstu.fc.rule.repository.FuzzyRuleRepository; import ru.ulstu.fc.rule.repository.FuzzyRuleRepository;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class ProjectRulesService { public class ProjectRulesService {
@ -24,4 +26,8 @@ public class ProjectRulesService {
} }
return ruleRepository.findByProject(projectService.getById(projectId)); 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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; 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.FuzzyTerm;
import ru.ulstu.fc.rule.model.dto.FuzzyRuleDto;
import ru.ulstu.fc.rule.model.dto.VariableDto; import ru.ulstu.fc.rule.model.dto.VariableDto;
import ru.ulstu.fc.rule.service.FuzzyRuleService; import ru.ulstu.fc.rule.service.FuzzyRuleService;
import ru.ulstu.fc.rule.service.FuzzyTermService; import ru.ulstu.fc.rule.service.FuzzyTermService;
@ -40,29 +40,29 @@ public class FuzzyRuleController {
public String edit(@PathVariable(value = "projectId") Integer projectId, public String edit(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "ruleId") Integer id, Model model) { @PathVariable(value = "ruleId") Integer id, Model model) {
model.addAttribute("projectId", projectId); model.addAttribute("projectId", projectId);
model.addAttribute("fuzzyRuleForm", model.addAttribute("fuzzyRuleDto",
(id != null && id != 0) (id != null && id != 0)
? new FuzzyRuleForm(id, fuzzyRuleService.getById(id)) ? new FuzzyRuleDto(fuzzyRuleService.getById(id))
: new FuzzyRuleForm(id, projectId)); : new FuzzyRuleDto(id, projectId));
return "rule/edit"; return "rule/edit";
} }
@PostMapping(value = "save", params = "save") @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()) { if (bindingResult.hasErrors()) {
model.addAttribute("projectId", fuzzyRuleForm.getProjectId()); model.addAttribute("projectId", fuzzyRuleDto.getProjectId());
return "rule/edit"; return "rule/edit";
} }
fuzzyRuleService.save(fuzzyRuleForm); fuzzyRuleService.save(fuzzyRuleDto);
return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); return "redirect:/project/edit/" + fuzzyRuleDto.getProjectId();
} }
@PostMapping(value = "save", params = "delete") @PostMapping(value = "save", params = "delete")
public String delete(FuzzyRuleForm fuzzyRuleForm) { public String delete(FuzzyRuleDto fuzzyRuleDto) {
if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) { if (fuzzyRuleDto != null && fuzzyRuleDto.getId() != null) {
fuzzyRuleService.delete(fuzzyRuleForm); fuzzyRuleService.delete(fuzzyRuleDto);
} }
return "redirect:/project/edit/" + fuzzyRuleForm.getProjectId(); return "redirect:/project/edit/" + fuzzyRuleDto.getProjectId();
} }
@ResponseBody @ResponseBody

View File

@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.fc.project.service.ProjectRulesService; import ru.ulstu.fc.project.service.ProjectRulesService;
import ru.ulstu.fc.rule.model.FuzzyRule; 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.FuzzyRuleParseService;
import ru.ulstu.fc.rule.service.FuzzyRuleService; import ru.ulstu.fc.rule.service.FuzzyRuleService;
@ -31,26 +31,24 @@ public class FuzzyRuleRestController {
} }
@GetMapping("/getAll/{projectId}") @GetMapping("/getAll/{projectId}")
public List<FuzzyRule> getAll(@PathVariable(value = "projectId") Integer projectId) { public List<FuzzyRuleDto> getAll(@PathVariable(value = "projectId") Integer projectId) {
//TODO: return dto return projectRulesService.getDtoByProjectId(projectId);
return projectRulesService.getByProjectId(projectId);
} }
@GetMapping("/get/{ruleId}") @GetMapping("/get/{ruleId}")
public FuzzyRule get(@PathVariable(value = "ruleId") Integer id) { public FuzzyRuleDto get(@PathVariable(value = "ruleId") Integer id) {
//TODO: return dto return ruleService.getByIdDto(id);
return ruleService.getById(id);
} }
@PostMapping @PostMapping
public FuzzyRule save(@Valid FuzzyRuleForm fuzzyRuleForm) { public FuzzyRule save(@Valid FuzzyRuleDto fuzzyRuleDto) {
return ruleService.save(fuzzyRuleForm); return ruleService.save(fuzzyRuleDto);
} }
@DeleteMapping @DeleteMapping
public void delete(@Valid FuzzyRuleForm fuzzyRuleForm) { public void delete(@Valid FuzzyRuleDto fuzzyRuleDto) {
if (fuzzyRuleForm != null && fuzzyRuleForm.getId() != null) { if (fuzzyRuleDto != null && fuzzyRuleDto.getId() != null) {
ruleService.delete(fuzzyRuleForm); 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.NotNull;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
import ru.ulstu.fc.rule.model.FuzzyRule;
public class FuzzyRuleForm { public class FuzzyRuleDto {
private Integer id; private Integer id;
@NotNull @NotNull
private Integer projectId; private Integer projectId;
@ -11,15 +12,15 @@ public class FuzzyRuleForm {
@Size(min = 5, max = 250, message = "Длина от 5 до 250 символов") @Size(min = 5, max = 250, message = "Длина от 5 до 250 символов")
private String content; private String content;
public FuzzyRuleForm() { public FuzzyRuleDto() {
} }
public FuzzyRuleForm(Integer id, Integer projectId) { public FuzzyRuleDto(Integer id, Integer projectId) {
this.id = id; this.id = id;
this.projectId = projectId; this.projectId = projectId;
} }
public FuzzyRuleForm(Integer id, FuzzyRule fuzzyRule) { public FuzzyRuleDto(FuzzyRule fuzzyRule) {
this.id = fuzzyRule.getId(); this.id = fuzzyRule.getId();
this.projectId = fuzzyRule.getProject().getId(); this.projectId = fuzzyRule.getProject().getId();
this.content = fuzzyRule.getContent(); this.content = fuzzyRule.getContent();

View File

@ -3,7 +3,7 @@ package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.service.ProjectService; import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.FuzzyRule; 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; import ru.ulstu.fc.rule.repository.FuzzyRuleRepository;
@Service @Service
@ -25,7 +25,11 @@ public class FuzzyRuleService {
return fuzzyRule; return fuzzyRule;
} }
public FuzzyRule save(FuzzyRuleForm ruleForm) { public FuzzyRuleDto getByIdDto(Integer id) {
return new FuzzyRuleDto(getById(id));
}
public FuzzyRule save(FuzzyRuleDto ruleForm) {
FuzzyRule rule; FuzzyRule rule;
if (ruleForm.getId() == null || ruleForm.getId() == 0) { if (ruleForm.getId() == null || ruleForm.getId() == 0) {
rule = new FuzzyRule(); rule = new FuzzyRule();
@ -37,7 +41,7 @@ public class FuzzyRuleService {
return ruleRepository.save(rule); return ruleRepository.save(rule);
} }
public void delete(FuzzyRuleForm ruleForm) { public void delete(FuzzyRuleDto ruleForm) {
ruleRepository.delete(getById(ruleForm.getId())); ruleRepository.delete(getById(ruleForm.getId()));
} }

View File

@ -9,7 +9,7 @@
</head> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<h3>Редактирование правила:</h3> <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" id="projectId" th:field="*{projectId}">
<input type="hidden" th:field="*{id}"> <input type="hidden" th:field="*{id}">
<div class="form-group"> <div class="form-group">