DefaultApiServiceProvider.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package org.ssssssss.magicapi.provider.impl;
  2. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  3. import org.springframework.jdbc.core.JdbcTemplate;
  4. import org.springframework.jdbc.core.RowMapper;
  5. import org.ssssssss.magicapi.config.ApiInfo;
  6. import org.ssssssss.magicapi.provider.ApiServiceProvider;
  7. import java.util.List;
  8. import java.util.UUID;
  9. public class DefaultApiServiceProvider implements ApiServiceProvider {
  10. private final String COMMON_COLUMNS = "id,\n" +
  11. "api_name name,\n" +
  12. "api_group_name group_name,\n" +
  13. "api_group_prefix group_prefix,\n" +
  14. "api_path path,\n" +
  15. "api_method method";
  16. private final String SCRIPT_COLUMNS = "api_script script,\n" +
  17. "api_parameter parameter,\n" +
  18. "api_output output,\n" +
  19. "api_option option_value\n";
  20. private RowMapper<ApiInfo> rowMapper = new BeanPropertyRowMapper<>(ApiInfo.class);
  21. private JdbcTemplate template;
  22. public DefaultApiServiceProvider(JdbcTemplate template) {
  23. this.template = template;
  24. }
  25. public boolean delete(String id) {
  26. String deleteById = "delete from magic_api_info where id = ?";
  27. return template.update(deleteById, id) > 0;
  28. }
  29. public boolean deleteGroup(String groupName) {
  30. String deleteByGroupName = "delete from magic_api_info where api_group_name = ?";
  31. return template.update(deleteByGroupName, groupName) > 0;
  32. }
  33. public List<ApiInfo> list() {
  34. String selectList = "select " + COMMON_COLUMNS + " from magic_api_info order by api_update_time desc";
  35. return template.query(selectList, rowMapper);
  36. }
  37. public List<ApiInfo> listWithScript() {
  38. String selectListWithScript = "select " + COMMON_COLUMNS + "," + SCRIPT_COLUMNS + " from magic_api_info";
  39. List<ApiInfo> infos = template.query(selectListWithScript, rowMapper);
  40. if (infos != null) {
  41. for (ApiInfo info : infos) {
  42. unwrap(info);
  43. }
  44. }
  45. return infos;
  46. }
  47. public ApiInfo get(String id) {
  48. String selectOne = "select " + COMMON_COLUMNS + "," + SCRIPT_COLUMNS + " from magic_api_info where id = ?";
  49. ApiInfo info = template.queryForObject(selectOne, rowMapper, id);
  50. unwrap(info);
  51. return info;
  52. }
  53. public boolean exists(String method, String path) {
  54. String exists = "select count(*) from magic_api_info where api_method = ? and api_path = ?";
  55. return template.queryForObject(exists, Integer.class, method, path) > 0;
  56. }
  57. @Override
  58. public boolean updateGroup(String oldGroupName, String groupName, String groupPrefix) {
  59. String updateGroup = "update magic_api_info set api_group_name = ?,api_group_prefix=?,api_update_time = ? where api_group_name = ?";
  60. return template.update(updateGroup, groupName, groupPrefix, System.currentTimeMillis(), oldGroupName) > 0;
  61. }
  62. public boolean existsWithoutId(String method, String path, String id) {
  63. String existsWithoutId = "select count(*) from magic_api_info where api_method = ? and api_path = ? and id !=?";
  64. return template.queryForObject(existsWithoutId, Integer.class, method, path, id) > 0;
  65. }
  66. public boolean insert(ApiInfo info) {
  67. info.setId(UUID.randomUUID().toString().replace("-", ""));
  68. wrap(info);
  69. long time = System.currentTimeMillis();
  70. String insert = "insert into magic_api_info(id,api_method,api_path,api_script,api_name,api_group_name,api_parameter,api_option,api_output,api_group_prefix,api_create_time,api_update_time) values(?,?,?,?,?,?,?,?,?,?,?,?)";
  71. return template.update(insert, info.getId(), info.getMethod(), info.getPath(), info.getScript(), info.getName(), info.getGroupName(), info.getParameter(), info.getOption(), info.getOutput(), info.getGroupPrefix(), time, time) > 0;
  72. }
  73. public boolean update(ApiInfo info) {
  74. wrap(info);
  75. String update = "update magic_api_info set api_method = ?,api_path = ?,api_script = ?,api_name = ?,api_group_name = ?,api_parameter = ?,api_option = ?,api_output = ?,api_group_prefix = ?,api_update_time = ? where id = ?";
  76. return template.update(update, info.getMethod(), info.getPath(), info.getScript(), info.getName(), info.getGroupName(), info.getParameter(), info.getOption(), info.getOutput(), info.getGroupPrefix(), System.currentTimeMillis(), info.getId()) > 0;
  77. }
  78. @Override
  79. public void backup(String apiId) {
  80. String backupSql = "insert into magic_api_info_his select * from magic_api_info where id = ?";
  81. template.update(backupSql, apiId);
  82. }
  83. @Override
  84. public List<Long> backupList(String apiId) {
  85. return template.queryForList("select api_update_time from magic_api_info_his where id = ? order by api_update_time desc",Long.class,apiId);
  86. }
  87. @Override
  88. public ApiInfo backupInfo(String apiId, Long timestamp) {
  89. String selectOne = "select " + COMMON_COLUMNS + "," + SCRIPT_COLUMNS + " from magic_api_info_his where id = ? and api_update_time = ? limit 1";
  90. ApiInfo info = template.queryForObject(selectOne, rowMapper, apiId,timestamp);
  91. unwrap(info);
  92. return info;
  93. }
  94. }