Skip to content

Commit

Permalink
[ADD] 新增租户管理及对应的单元测试
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Yeh <[email protected]>
  • Loading branch information
alan-yeh committed Nov 18, 2024
1 parent 962966c commit a8eb277
Show file tree
Hide file tree
Showing 5 changed files with 687 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.saas.controller;

import central.bean.Page;
import central.data.saas.Tenant;
import central.starter.web.param.IdsParams;
import central.starter.web.query.IdQuery;
import central.studio.dashboard.controller.saas.param.TenantParams;
import central.studio.dashboard.controller.saas.query.TenantPageQuery;
import central.studio.dashboard.logic.saas.TenantLogic;
import central.validation.group.Insert;
import central.validation.group.Update;
import jakarta.validation.groups.Default;
import lombok.Setter;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* Tenant Controller
* <p>
* 租户管理
*
* @author Alan Yeh
* @since 2024/11/19
*/
@RestController
@RequiresAuthentication
@RequestMapping("/dashboard/api/saas/tenants")
public class TenantController {

public interface Permissions {
String VIEW = "saas:tenant:view";
String ADD = "saas:tenant:add";
String EDIT = "saas:tenant:edit";
String DELETE = "saas:tenant:delete";
String ENABLE = "saas:tenant:enable";
String DISABLE = "saas:tenant:disable";
}


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

/**
* 按条件分页查询列表
*
* @param query 查询
* @return 分页结果
*/
@GetMapping("/page")
public Page<Tenant> page(@Validated TenantPageQuery query) {
return this.logic.pageBy(query.getPageIndex(), query.getPageSize(), query.build(), null);
}

/**
* 根据主键查询详情
*
* @param query 查询
* @return 详情
*/
@GetMapping("/details")
public Tenant details(@Validated IdQuery<Tenant> query) {
return this.logic.findById(query.getId());
}

/**
* 更新行政数据
*
* @param params 数据入参
* @param accountId 当前登录帐号
* @return 新增后的数据
*/
@PostMapping
public Tenant add(@RequestBody @Validated({Insert.class, Default.class}) TenantParams params, @RequestAttribute String accountId) {
return this.logic.insert(params.toInput(), accountId);
}

/**
* 更新数据
*
* @param params 数据入参
* @param accountId 当前登录帐号
* @return 更新后的数据
*/
@PutMapping
public Tenant update(@RequestBody @Validated({Update.class, Default.class}) TenantParams params, @RequestAttribute String accountId) {
return this.logic.update(params.toInput(), accountId);
}

/**
* 根据主键删除数据
*
* @param params 待删除主键列表
* @param accountId 当前登录帐号
* @return 受影响数据行数
*/
@DeleteMapping
public long delete(@Validated IdsParams params, @RequestAttribute String accountId) {
return this.logic.deleteByIds(params.getIds(), accountId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.saas.param;

import central.data.saas.TenantInput;
import central.validation.Label;
import central.validation.group.Insert;
import central.validation.group.Update;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Tenant Params
* <p>
* 租户参数
*
* @author Alan Yeh
* @since 2024/11/19
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TenantParams {

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

@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 databaseId;

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

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

public TenantInput toInput() {
return TenantInput.builder()
.id(this.getId())
.code(this.getCode())
.name(this.getName())
.databaseId(this.getDatabaseId())
.enabled(this.getEnabled())
.remark(this.getRemark())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.saas.query;

import central.data.saas.Tenant;
import central.lang.Stringx;
import central.sql.query.Conditions;
import central.starter.web.query.PageQuery;
import central.validation.Label;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;

/**
* Tenant Page Query
* <p>
* 租户分页查询
*
* @author Alan Yeh
* @since 2024/11/19
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class TenantPageQuery extends PageQuery<Tenant> {
@Serial
private static final long serialVersionUID = -6601338520107100701L;

@Label("主键")
private String id;

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

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

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

// 精确字段搜索
if (Stringx.isNotEmpty(this.getId())) {
conditions.eq(Tenant::getId, this.getId());
}
if (Stringx.isNotEmpty(this.getName())) {
conditions.like(Tenant::getName, this.getName());
}
if (Stringx.isNotEmpty(this.getCode())) {
conditions.like(Tenant::getCode, this.getCode());
}

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

0 comments on commit a8eb277

Please sign in to comment.