From d10db93950d44a7e2e9b0d53f8bc06b81cfea012 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 25 Feb 2025 16:09:22 +0400 Subject: [PATCH] #17 -- REST CRUD for variables --- .../controller/FuzzyTermRestController.java | 4 +- .../controller/VariableRestController.java | 47 +++++++++++++++++++ .../rule/repository/VariableRepository.java | 2 + .../fc/rule/service/VariableService.java | 6 +++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java diff --git a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java index 962f5b2..4247ef5 100644 --- a/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java +++ b/src/main/java/ru/ulstu/fc/rule/controller/FuzzyTermRestController.java @@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; 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.RestController; import ru.ulstu.fc.rule.model.FuzzyTerm; import ru.ulstu.fc.rule.model.FuzzyTermForm; @@ -12,7 +13,8 @@ import ru.ulstu.fc.rule.service.FuzzyTermService; import java.util.List; -@RestController("fuzzyTermRest") +@RestController +@RequestMapping("fuzzyTermRest") public class FuzzyTermRestController { private final FuzzyTermService fuzzyTermService; diff --git a/src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java b/src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java new file mode 100644 index 0000000..ac8ed1f --- /dev/null +++ b/src/main/java/ru/ulstu/fc/rule/controller/VariableRestController.java @@ -0,0 +1,47 @@ +package ru.ulstu.fc.rule.controller; + +import jakarta.validation.Valid; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +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.RestController; +import ru.ulstu.fc.rule.model.Variable; +import ru.ulstu.fc.rule.model.VariableForm; +import ru.ulstu.fc.rule.service.VariableService; + +import java.util.List; + +@RestController +@RequestMapping("variableRest") +public class VariableRestController { + private final VariableService variableService; + + public VariableRestController(VariableService variableService) { + this.variableService = variableService; + } + + @GetMapping("/list/{projectId}") + public List getList(@PathVariable(value = "projectId") Integer projectId) { + return variableService.getAllByProject(projectId); + } + + @GetMapping("/get/{projectId}/{variableId}") + public Variable getById(@PathVariable(value = "projectId") Integer projectId, + @PathVariable(value = "variableId") Integer variableId) { + return variableService.getById(variableId); + } + + @PostMapping + public Variable save(@Valid VariableForm variableForm) { + return variableService.save(variableForm); + } + + @DeleteMapping + public void delete(@Valid VariableForm variableForm) { + if (variableForm != null && variableForm.getId() != null) { + variableService.delete(variableForm); + } + } +} diff --git a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java index f228db7..9499f09 100644 --- a/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java +++ b/src/main/java/ru/ulstu/fc/rule/repository/VariableRepository.java @@ -9,4 +9,6 @@ import java.util.List; public interface VariableRepository extends JpaRepository { List findByProject(Project project); + + List getByProject(Project project); } 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 0fcf6fd..20cc790 100644 --- a/src/main/java/ru/ulstu/fc/rule/service/VariableService.java +++ b/src/main/java/ru/ulstu/fc/rule/service/VariableService.java @@ -7,6 +7,8 @@ import ru.ulstu.fc.rule.model.Variable; import ru.ulstu.fc.rule.model.VariableForm; import ru.ulstu.fc.rule.repository.VariableRepository; +import java.util.List; + @Service public class VariableService { private final VariableRepository variableRepository; @@ -59,4 +61,8 @@ public class VariableService { var.getFuzzyTerms().add(ft); variableRepository.save(var); } + + public List getAllByProject(Integer projectId) { + return variableRepository.getByProject(projectService.getById(projectId)); + } }