Skip to content

新建模块

1、新建模块实体类

在 MyCompanyName.MyProjectName.Api\Domain 目录下新建目录Module,并在Module目录下新建实体类ModuleEntity

cs
using ZhonTai.Admin.Core.Entities;
using FreeSql.DataAnnotations;
using System;

namespace MyCompanyName.MyProjectName.Api.Domain.Module;

/// <summary>
/// 模块
/// </summary>
[Table(Name = ApiConsts.AreaName + "_module")]
[Index("idx_{tablename}_01", nameof(TenantId) + "," + nameof(Name), true)]
public partial class ModuleEntity : EntityTenant
{
    /// <summary>
    /// 名称
    /// </summary>
    [Column(StringLength = 50)]
    public string Name { get; set; }
}

约定:实体类以 Entity 命名结尾,Module 可替换成实际业务模块名。表名和索引采用下划线命名法,表名前缀采用 ApiConsts.AreaName 项目前缀开头,索引以 idx 开头

继承EntityTenant后查询数据会根据租户编号TenantId作为条件筛选数据

2、新建输入输出Dto

在 MyCompanyName.MyProjectName.Api.Contracts\Services 目录下新建Input输入或Output输出目录

Input目录下新建添加输入 DtoModuleAddInput

cs
namespace MyCompanyName.MyProjectName.Api.Contracts.Services.Module.Input;

/// <summary>
/// 添加
/// </summary>
public class ModuleAddInput
{
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }
}

Input目录下新建添加输入 DtoModuleUpdateInput

cs
using System.ComponentModel.DataAnnotations;
using ZhonTai.Admin.Core.Validators;

namespace MyCompanyName.MyProjectName.Api.Contracts.Services.Module.Input;

/// <summary>
/// 修改模块
/// </summary>
public partial class ModuleUpdateInput : ModuleAddInput
{
    /// <summary>
    /// 编号
    /// </summary>
    [Required]
    [ValidateRequired("请选择模块")]
    public long Id { get; set; }
}

Input目录下新建添加输入 DtoModuleGetPageInput

cs
namespace MyCompanyName.MyProjectName.Api.Contracts.Services.Module.Input;

/// <summary>
/// 模块分页
/// </summary>
public partial class ModuleGetPageInput
{
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }
}

Output目录下新建查询输出 DtoModuleGetOutput

cs
using MyCompanyName.MyProjectName.Api.Contracts.Services.Module.Input;

namespace MyCompanyName.MyProjectName.Api.Services.Module.Output;

public class ModuleGetOutput : ModuleUpdateInput
{
}

Output目录下新建查询输出 DtoModuleGetPageOutput

cs
namespace MyCompanyName.MyProjectName.Api.Contracts.Services.Module.Output;

/// <summary>
/// 模块列表
/// </summary>
public class ModuleGetPageOutput
{
    /// <summary>
    /// 主键
    /// </summary>
    public long Id { get; set; }

    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }
}

3、新建模块接口

在 MyCompanyName.MyProjectName.Api\Services 目录下新建目录Module,并在Module目录下新建模块服务ModuleService

服务方法如何生成动态接口:在ModuleService模块服务上增加 [DynamicApi(Area = ApiConsts.AreaName)] 特性,并继承 IDynamicApi 接口

提示

使用项目模板开发时用AppRepositoryBase基础仓储来创建实体仓储,Admin.Core源码开发时请用AdminRepositoryBase基础仓储来创建实体仓储

cs
using System.Threading.Tasks;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Services;
using MyCompanyName.MyProjectName.Api.Domain.Module;
using MyCompanyName.MyProjectName.Api.Domain.Module.Dto;
using MyCompanyName.MyProjectName.Api.Services.Module.Input;
using MyCompanyName.MyProjectName.Api.Services.Module.Output;
using MyCompanyName.MyProjectName.Api.Core.Consts;
using Newtonsoft.Json;
using ZhonTai;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;
using Microsoft.AspNetCore.Mvc;
using FreeScheduler;

namespace MyCompanyName.MyProjectName.Api.Services.Module;

/// <summary>
/// 模块服务
/// </summary>
[DynamicApi(Area = ApiConsts.AreaName)]
public class ModuleService : BaseService, IDynamicApi
{
    private readonly AppRepositoryBase<ModuleEntity> _moduleRep;

    public ModuleService(AppRepositoryBase<ModuleEntity> moduleRep)
    {
        _moduleRep = moduleRep;
    }

    /// <summary>
    /// 查询模块
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<ModuleGetOutput> GetAsync(long id)
    {
        var result = await _moduleRep.GetAsync<ModuleGetOutput>(id);
        return result;
    }

    /// <summary>
    /// 查询分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<PageOutput<ModuleGetPageOutput>> GetPageAsync(PageInput<ModuleGetPageInput> input)
    {
        var key = input.Filter?.Name;

        var list = await _moduleRep.Select
        .WhereIf(key.NotNull(), a => a.Name.Contains(key))
        .Count(out var total)
        .OrderByDescending(true, c => c.Id)
        .Page(input.CurrentPage, input.PageSize)
        .ToListAsync<ModuleGetPageOutput>();

        var data = new PageOutput<ModuleGetPageOutput>()
        {
            List = list,
            Total = total
        };

        return data;
    }

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<long> AddAsync(ModuleAddInput input)
    {
        var entity = Mapper.Map<ModuleEntity>(input);
        await _moduleRep.InsertAsync(entity);

        return entity.Id;
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task UpdateAsync(ModuleUpdateInput input)
    {
        var entity = await _moduleRep.GetAsync(input.Id);
        if (!(entity?.Id > 0))
        {
            throw ResultOutput.Exception("模块不存在");
        }

        Mapper.Map(input, entity);
        await _moduleRep.UpdateAsync(entity);
    }

    /// <summary>
    /// 彻底删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task DeleteAsync(long id)
    {
        await _moduleRep.DeleteAsync(m => m.Id == id);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task SoftDeleteAsync(long id)
    {
        await _moduleRep.SoftDeleteAsync(id);
    }

    /// <summary>
    /// 批量删除
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    public async Task BatchSoftDeleteAsync(long[] ids)
    {
        await _moduleRep.SoftDeleteAsync(ids);
    }
}

接口权限说明

  • [Login]标记用户需登录才能访问,该标记方便测试未授权的接口
cs
[Login]
public async Task<ModuleGetOutput> GetAsync(long id)
{
    var result = await _moduleRepository.GetAsync<ModuleGetOutput>(id);
    return result;
}
  • [AllowAnonymous]标记用户可匿名访问,该标记方便测试未授权且未登录的接口
cs
[AllowAnonymous]
public async Task<ModuleGetOutput> GetAsync(long id)
{
    var result = await _moduleRepository.GetAsync<ModuleGetOutput>(id);
    return result;
}

注意

授权接口需删除[Login]和[AllowAnonymous]标记