外观
新建模块
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 = "app_module")]
[Index("idx_{tablename}_01", nameof(Name) + "," + nameof(TenantId), true)]
public partial class ModuleEntity : EntityFull, ITenant
{
/// <summary>
/// 租户Id
/// </summary>
[Column(Position = -10, CanUpdate = false)]
public long? TenantId { get; set; }
/// <summary>
/// 名称
/// </summary>
[Column(StringLength = 50)]
public string Name { get; set; }
}
}
约定:实体类以 Entity 命名结尾,Module 可替换成实际业务模块名。表名和索引采用下划线命名法,表名前缀采用 app 项目前缀开头,索引以 idx 开头
2、新建输入输出Dto
在 MyCompanyName.MyProjectName.Api\Domain 或在 MyCompanyName.MyProjectName.Api\Services 目录下新建Input
输入或Output
输出目录
在Input
目录下新建添加输入 DtoModuleAddInput
cs
namespace MyCompanyName.MyProjectName.Api.Services.Module.Input;
/// <summary>
/// 添加
/// </summary>
public class ModuleAddInput
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
}
在Output
目录下新建查询输出 DtoModuleGetOutput
cs
using MyCompanyName.MyProjectName.Api.Services.Module.Input;
namespace MyCompanyName.MyProjectName.Api.Services.Module.Output;
public class ModuleGetOutput : ModuleUpdateInput
{
}
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<ModuleListOutput>> GetPageAsync(PageInput<ModuleGetPageDto> 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<ModuleListOutput>();
var data = new PageOutput<ModuleListOutput>()
{
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]标记