我们系统的业务模块有:
根据上图,可以得出5个表,部门表(Department)、插件表(CADPlug)、成员表(User)、插件使用权限表(UserPlugAuthority)和插件使用记录表(UserLog)
///
/// 插件
///
public class CADPlug: EntityBase
{
///
/// 插件名字
///
public string Name { get; set; }
///
/// 插件所属部门ID
///
public int DepartmentId { get; set; }
///
/// 部门表
///
public Department Department { get; set; }
///
/// 插件使用权限中间表
///
public ICollection<UserPlugAuthority> UserPlugAuthorityList { get; set; }
///
/// 插件用户
///
public ICollection<User> UserList { get; set; }
public CADPlug() { }
}
///
/// 用户表
///
public class User:EntityBase,IEntityTypeBuilder<User>
{
public string IP { get; set; }
///
/// 部门Id
///
public int DepartmentId { get; set; }
///
/// 部门
///
public Department Department { get; set; }
///
/// 多对多CAD插件表
///
public ICollection<CADPlug> CADPlugList { get; set; }
///
/// 用户权限使用中间表
///
public ICollection<UserPlugAuthority> UserPlugAuthorityList { get; set; }
///
/// 配置多对多的关系
///
///
///
///
public void Configure(EntityTypeBuilder<User> entityBuilder, DbContext dbContext, Type dbContextLocator)
{
entityBuilder.HasMany(u => u.CADPlugList)
.WithMany(c => c.UserList)
.UsingEntity<UserPlugAuthority>(a => a.HasOne(b => b.CADPlug).WithMany(c => c.UserPlugAuthorityList).HasForeignKey(c => c.CADPlugId), a => a.HasOne(b => b.User).WithMany(u => u.UserPlugAuthorityList).HasForeignKey(c => c.UserId));
}
public User() { }
}
///
/// 插件使用授权
///
public class UserPlugAuthority : Entity
{
public UserPlugAuthority()
{
CreatedTime = DateTime.Now;
}
///
/// 使用期限
///
public DateTime UserDataTime { get; set; }
///
/// 用户Id
///
public int UserId { get; set; }
///
/// 用户主表
///
public User User { get; set; }
///
/// 插件Id
///
public int CADPlugId { get; set; }
///
/// 插件信息
///
public CADPlug CADPlug { get; set; }
}
///
/// 插件使用记录
///
public class UserLog : Entity
{
public UserLog()
{
CreatedTime = DateTime.Now;
}
///
/// 用户IP
///
public string UserIp { get; set; }
///
/// 插件方法名
///
public string PlugFunctiontName { get; set; }
///
/// 插件名
///
public string CADPlugName { get; set; }
}
public class RightsManagementDbContext : AppDbContext<RightsManagementDbContext>
{
public RightsManagementDbContext(DbContextOptions<RightsManagementDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
}
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDatabaseAccessor(options =>
{
string dbValue = App.Configuration["DataProvider"];
string connString = "";
string connDbProvider = "";
switch (dbValue)
{
case "Sqlite":
connString = App.Configuration["ConnectionString:SqliteConnection"];
connDbProvider = DbProvider.Sqlite;
break;
case "MySql":
connString = App.Configuration["ConnectionString:MySqlConnection"];
connDbProvider = $"{DbProvider.MySql}@8.0.22";
break;
}
options.AddDbPool<RightsManagementDbContext>(connDbProvider, connectionMetadata:connString);
}, "RightsManagementSystems.Database.Migrations");
}
}
{
"$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
}
},
"ConnectionString": {
"SqliteConnection": "Data Source = ./RightsManagementSystem.db",
"MySqlConnection": ""
},
"DataProvider": "Sqlite",
}
}
根据上诉模块,分别实现对应的接口类
下面展示接口仅实现部门接口的类使用,其余可以去资源免费下载
///
/// 部门操作类
///
[DynamicApiController]
public class DepartmentAppService
{
private readonly IDepartmentService _departmentService;
public DepartmentAppService(IDepartmentService departmentService)
{
this._departmentService = departmentService;
}
///
/// 新建部门
///
///
///
public async Task<IActionResult> PostDepartmentCreate([FromBody] DepartmentCreateDto departmentCreateDto) => await _departmentService.DepartmentCreateAsync(departmentCreateDto);
///
/// 修改部门名字
///
///
///
///
public async Task<IActionResult> PutDepartmentUpdate(int departmentId,[FromBody]DepartmentUpdateDto departmentUpdateDto)=>await _departmentService.DepartmentUpdateAsync(departmentId, departmentUpdateDto);
///
/// 删除部门
///
///
///
[HttpDelete]
public async Task<IActionResult> DepartmentDelete(int departmentId) => await _departmentService.DepartmentDeleteAsync(departmentId);
///
/// 取得一个部门
///
///
///
public async Task<IActionResult> GetDepartment(int departmentId) => await _departmentService.DepartmentGetAsync(departmentId);
///
/// 得到所有的部门信息
///
///
public async Task<IActionResult> GetAllDepartment() => await _departmentService.DepartmentGetAllAsync();
}
通过IOC容器反向代理得到IDepartmentService类
public class DepartmentService : ITransient, IDepartmentService
{
private readonly IRepository<Department> _departmentRepository;
public DepartmentService(IRepository<Department> departmentRepository)
{
this._departmentRepository = departmentRepository;
}
public async Task<IActionResult> DepartmentCreateAsync(DepartmentCreateDto departmentCreateDto)
{
try
{
if (departmentCreateDto == null) return ApiResponse.Error("null");
if (string.IsNullOrEmpty(departmentCreateDto.Name)) return ApiResponse.Error("部门名字为null");
Department department = new Department() { Name = departmentCreateDto.Name };
await _departmentRepository.InsertNowAsync(department);
return ApiResponse.OK("部门添加成功");
}
catch (Exception ex)
{
return ApiResponse.Error(ex.Message);
}
}
public async Task<IActionResult> DepartmentDeleteAsync(int departmentId)
{
try
{
if (departmentId < 0) return ApiResponse.Error("部门Id不存在");
await _departmentRepository.DeleteNowAsync(departmentId);
return ApiResponse.OK("删除成功");
}
catch (Exception ex)
{
return ApiResponse.Error(ex.Message);
}
}
public async Task<IActionResult> DepartmentGetAsync(int departmentId)
{
if (departmentId < 0) return ApiResponse.Error("部门Id不存在");
Department department = await _departmentRepository.Include(x => x.CADPlugList).Include(x => x.UserList).FirstOrDefaultAsync(x=>x.Id == departmentId);
return ApiResponse.OK(department);
}
public async Task<IActionResult> DepartmentGetAllAsync()
{
List<Department> allDepartmentList = await _departmentRepository.Include(x=>x.CADPlugList).Include(x=>x.UserList).AsQueryable().ToListAsync();
return ApiResponse.OK(allDepartmentList);
}
public async Task<IActionResult> DepartmentUpdateAsync(int departmentId, DepartmentUpdateDto departmentUpdateDto)
{
try
{
if (departmentUpdateDto == null) return ApiResponse.Error("null");
Department updateDepartment = await _departmentRepository.FindOrDefaultAsync(departmentId);
if (updateDepartment == null) return ApiResponse.Error("找不到部门");
if (await _departmentRepository.FirstOrDefaultAsync(x => x.Name.Equals(departmentUpdateDto.Name)) != null) return ApiResponse.Error("修改的名字已经存在");
if (!string.IsNullOrEmpty(departmentUpdateDto.Name)) updateDepartment.Name = departmentUpdateDto.Name;
var updateResult = await _departmentRepository.UpdateNowAsync(updateDepartment);
return ApiResponse.OK(updateResult.Entity);
}
catch (Exception ex)
{
return ApiResponse.Error(ex.Message);
}
}
}
services.AddControllers().AddInjectWithUnifyResult().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});