|
|
@@ -1,17 +1,22 @@
|
|
|
package com.izouma.walkchina.web;
|
|
|
|
|
|
import com.izouma.walkchina.constant.AppConstants;
|
|
|
+import com.izouma.walkchina.domain.SystemVariable;
|
|
|
import com.izouma.walkchina.dto.Result;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import com.izouma.walkchina.repo.SystemVariableRepository;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/systemVariable")
|
|
|
public class SystemVariableController {
|
|
|
+ @Autowired
|
|
|
+ private SystemVariableRepository systemVariableRepository;
|
|
|
|
|
|
@GetMapping("/conversionFactor")
|
|
|
public Result getConversionFactor() {
|
|
|
@@ -20,4 +25,39 @@ public class SystemVariableController {
|
|
|
map.put("DISTANCE_TO_COIN_RATE", AppConstants.DISTANCE_TO_COIN_RATE);
|
|
|
return Result.ok(map);
|
|
|
}
|
|
|
+
|
|
|
+ @GetMapping("/all")
|
|
|
+ public Result all() {
|
|
|
+ List<SystemVariable> list = systemVariableRepository.findAll();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ for (SystemVariable systemVariable : list) {
|
|
|
+ if (Pattern.matches("(?i)(true|false)", systemVariable.getValue())) {
|
|
|
+ map.put(systemVariable.getName(), Boolean.parseBoolean(systemVariable.getValue()));
|
|
|
+ } else if (Pattern.matches("\\d+", systemVariable.getValue())) {
|
|
|
+ map.put(systemVariable.getName(), Long.parseLong(systemVariable.getValue()));
|
|
|
+ } else if (Pattern.matches("\\d+\\.\\d+", systemVariable.getValue())) {
|
|
|
+ map.put(systemVariable.getName(), Double.parseDouble(systemVariable.getValue()));
|
|
|
+ } else {
|
|
|
+ map.put(systemVariable.getName(), systemVariable.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.ok(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/save")
|
|
|
+ public Result save(@RequestBody Map<String, String> body) {
|
|
|
+ body.forEach((name, value) -> {
|
|
|
+ SystemVariable systemVariable = systemVariableRepository.findByName(name);
|
|
|
+ if (systemVariable == null) {
|
|
|
+ systemVariable = SystemVariable.builder()
|
|
|
+ .name(name)
|
|
|
+ .value(value)
|
|
|
+ .build();
|
|
|
+ } else {
|
|
|
+ systemVariable.setValue(value);
|
|
|
+ }
|
|
|
+ systemVariableRepository.save(systemVariable);
|
|
|
+ });
|
|
|
+ return Result.ok();
|
|
|
+ }
|
|
|
}
|