Skip to content

Commit

Permalink
[ADD] 添加数据库逻辑
Browse files Browse the repository at this point in the history
1. 新增数据库业务逻辑类
2. 新增数据库控制器类
3. 新增数据库单元测试

Signed-off-by: Alan Yeh <[email protected]>
  • Loading branch information
alan-yeh committed Oct 19, 2024
1 parent 62625eb commit f02d566
Show file tree
Hide file tree
Showing 6 changed files with 880 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,33 @@

package central.studio.dashboard.controller.system.controller;

import central.bean.Page;
import central.data.system.Database;
import central.starter.web.param.IdsParams;
import central.starter.web.query.IdQuery;
import central.studio.dashboard.controller.system.param.DatabaseParams;
import central.studio.dashboard.controller.system.query.DatabasePageQuery;
import central.studio.dashboard.logic.system.DatabaseLogic;
import central.validation.group.Insert;
import central.validation.group.Update;
import central.web.XForwardedHeaders;
import jakarta.validation.groups.Default;
import lombok.Setter;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* Database Controller
* <p>
* 数据源管理
*
* @author Alan Yeh
* @since 2023/11/21
* @since 2024/10/20
*/
@RestController
@RequiresAuthentication
@RequiresPermissions(DatabaseController.Permissions.VIEW)
@RequestMapping("/dashboard/api/system/databases")
public class DatabaseController {

Expand All @@ -54,4 +65,69 @@ public interface Permissions {
String ENABLE = "${application}:system:database:enable";
String DISABLE = "${application}:system:database:disable";
}

@Setter(onMethod_ = @Autowired)
private DatabaseLogic logic;

/**
* 按条件分页查询字典列表
*
* @param query 查询
* @param tenant 租户标识
* @return 分页结果
*/
@GetMapping("/page")
public Page<Database> page(@Validated DatabasePageQuery query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.pageBy(query.getPageIndex(), query.getPageSize(), query.build(), null, tenant);
}

/**
* 根据主键查询字典详情
*
* @param query 查询
* @param tenant 租户标识
* @return 详情
*/
@GetMapping("/details")
public Database details(@Validated IdQuery<Database> query, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.findById(query.getId(), tenant);
}

/**
* 新增字典
*
* @param params 字典入参
* @param tenant 租户标识
* @return 新增后的字典
*/
@PostMapping
public Database add(@RequestBody @Validated({Insert.class, Default.class}) DatabaseParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.insert(params.toInput(), accountId, tenant);
}

/**
* 更新字典
*
* @param params 字典入参
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 更新后字典数据
*/
@PutMapping
public Database update(@RequestBody @Validated({Update.class, Default.class}) DatabaseParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.update(params.toInput(), accountId, tenant);
}

/**
* 根据主键删除字典数据
*
* @param params 待删除主键列表
* @param accountId 当前登录帐号
* @param tenant 租户标识
* @return 受影响数据行数
*/
@DeleteMapping
public long delete(@Validated IdsParams params, @RequestAttribute String accountId, @RequestHeader(XForwardedHeaders.TENANT) String tenant) {
return this.logic.deleteByIds(params.getIds(), accountId, tenant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.system.param;

import central.data.system.DatabaseInput;
import central.util.Listx;
import central.validation.Label;
import central.validation.group.Insert;
import central.validation.group.Update;
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
* Database Params
* <p>
* 数据库入参
*
* @author Alan Yeh
* @since 2024/10/20
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DatabaseParams {

@Label("主键")
@Null(groups = Insert.class)
@NotBlank(groups = Update.class)
@Size(min = 1, max = 32, groups = Insert.class)
private String id;

@Label("应用主键")
@NotNull
@Size(min = 1, max = 32)
private String applicationId;

@Label("标识")
@NotBlank
@Size(min = 1, max = 32)
@Pattern(regexp = "^[0-9a-zA-Z_-]+$", message = "${label}[${property}]只能由数字、英文字母、中划线、下划线组成")
@Pattern(regexp = "^(?!-)(?!_).*$", message = "${label}[${property}]不能由中划线或下划线开头")
@Pattern(regexp = "^(?!.*?[-_]$).*$", message = "${label}[${property}]不能由中划线或下划线结尾")
@Pattern(regexp = "^(?!.*(--).*$).*$", message = "${label}[${property}]不能出现连续中划线")
@Pattern(regexp = "^(?!.*(__).*$).*$", message = "${label}[${property}]不能出现连续下划线")
@Pattern(regexp = "^(?!.*(-_|_-).*$).*$", message = "${label}[${property}]不能出现连续中划线、下划线")
private String code;

@Label("名称")
@NotBlank
@Size(min = 1, max = 50)
private String name;

@Label("类型")
@NotBlank
@Size(min = 1, max = 32)
private String type;

@Label("是否启用")
@NotNull
private Boolean enabled;

@Label("备注")
@Size(max = 1024)
private String remark;

@Label("主数据库")
@NotNull
@Valid
private DatabasePropertiesParams master;

@Label("从数据库")
@Valid
private List<DatabasePropertiesParams> slaves;

@Label("初始化参数")
@NotBlank
@Size(min = 1, max = 5 * 1024 * 1024)
private String params;

public DatabaseInput toInput() {
return DatabaseInput.builder()
.id(this.getId())
.applicationId(this.getApplicationId())
.code(this.getCode())
.name(this.getName())
.type(this.getType())
.enabled(this.getEnabled())
.remark(this.getRemark())
.master(this.getMaster().toInput())
.slaves(Listx.asStream(this.getSlaves()).map(DatabasePropertiesParams::toInput).toList())
.params(this.getParams())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.system.param;

import central.data.system.DatabasePropertiesInput;
import central.validation.Label;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Database Properties Params
* <p>
* 数据库配置入参
*
* @author Alan Yeh
* @since 2024/10/20
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DatabasePropertiesParams {
@Label("驱动")
@NotBlank
@Size(min = 1, max = 1024)
private String driver;

@Label("连接字符串")
@NotBlank
@Size(min = 1, max = 1024)
private String url;

@Label("用户名")
@Size(max = 50)
private String username;

@Label("密码")
@Size(max = 50)
private String password;

public DatabasePropertiesInput toInput() {
return DatabasePropertiesInput.builder()
.driver(this.getDriver())
.url(this.getUrl())
.username(this.getUsername())
.password(this.getPassword())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* MIT License
*
* Copyright (c) 2022-present Alan Yeh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package central.studio.dashboard.controller.system.query;

import central.data.system.Database;
import central.lang.Stringx;
import central.sql.query.Conditions;
import central.starter.web.query.PageQuery;
import central.validation.Label;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;

/**
* Database Page Query
* <p>
* 数据库分页查询
*
* @author Alan Yeh
* @since 2024/10/20
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class DatabasePageQuery extends PageQuery<Database> {

Check warning on line 48 in central-dashboard/src/main/java/central/studio/dashboard/controller/system/query/DatabasePageQuery.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Non-serializable class with 'serialVersionUID'

Non-serializable class `DatabasePageQuery` defines a 'serialVersionUID' field
@Serial
private static final long serialVersionUID = 5563847496249217986L;

@NotBlank
@Label("应用主键")
private String applicationId;

@Label("标识")
private String code;

@Label("名称")
private String name;

@Override
public Conditions<Database> build() {
var conditions = Conditions.of(Database.class);

conditions.eq(Database::getApplicationId, this.getApplicationId());

// 精确搜索
if (Stringx.isNotBlank(this.getCode())) {
conditions.like(Database::getCode, this.getCode());
}
if (Stringx.isNotBlank(this.getName())) {
conditions.like(Database::getName, this.getName());
}

// 模糊搜索
for (var keyword : this.getKeywords()) {
conditions.and(filter -> filter.like(Database::getCode, keyword).or().like(Database::getName, keyword));
}

return conditions;
}
}
Loading

0 comments on commit f02d566

Please sign in to comment.