GenConfigServiceImpl.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package me.zhengjie.service.impl;
  17. import lombok.RequiredArgsConstructor;
  18. import me.zhengjie.domain.GenConfig;
  19. import me.zhengjie.repository.GenConfigRepository;
  20. import me.zhengjie.service.GenConfigService;
  21. import me.zhengjie.utils.StringUtils;
  22. import org.springframework.stereotype.Service;
  23. import java.io.File;
  24. /**
  25. * @author Zheng Jie
  26. * @date 2019-01-14
  27. */
  28. @Service
  29. @RequiredArgsConstructor
  30. public class GenConfigServiceImpl implements GenConfigService {
  31. private final GenConfigRepository genConfigRepository;
  32. @Override
  33. public GenConfig find(String tableName) {
  34. GenConfig genConfig = genConfigRepository.findByTableName(tableName);
  35. if(genConfig == null){
  36. return new GenConfig(tableName);
  37. }
  38. return genConfig;
  39. }
  40. @Override
  41. public GenConfig update(String tableName, GenConfig genConfig) {
  42. String separator = File.separator;
  43. String[] paths;
  44. String symbol = "\\";
  45. if (symbol.equals(separator)) {
  46. paths = genConfig.getPath().split("\\\\");
  47. } else {
  48. paths = genConfig.getPath().split(File.separator);
  49. }
  50. StringBuilder api = new StringBuilder();
  51. for (String path : paths) {
  52. api.append(path);
  53. api.append(separator);
  54. if ("src".equals(path)) {
  55. api.append("api");
  56. break;
  57. }
  58. }
  59. genConfig.setApiPath(api.toString());
  60. return genConfigRepository.save(genConfig);
  61. }
  62. }