2022-04-09 16:16:32 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Yi.Framework.Common.Models;
|
|
|
|
|
|
using Yi.Framework.Interface;
|
|
|
|
|
|
using Yi.Framework.Language;
|
|
|
|
|
|
using Yi.Framework.Model.Models;
|
|
|
|
|
|
using Yi.Framework.Repository;
|
|
|
|
|
|
using Yi.Framework.WebCore;
|
|
|
|
|
|
using Yi.Framework.WebCore.AttributeExtend;
|
|
|
|
|
|
using Yi.Framework.WebCore.AuthorizationPolicy;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.ApiMicroservice.Controllers
|
|
|
|
|
|
{
|
|
|
|
|
|
[ApiController]
|
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
|
|
public class TestController : ControllerBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private IStringLocalizer<LocalLanguage> _local;
|
2022-04-12 18:09:13 +08:00
|
|
|
|
private IUserService _iUserService;
|
2022-04-16 00:01:00 +08:00
|
|
|
|
private IRoleService _iRoleService;
|
2022-04-13 21:36:50 +08:00
|
|
|
|
//你可以依赖注入服务层各各接口,也可以注入其他仓储层,怎么爽怎么来!
|
2022-04-16 00:01:00 +08:00
|
|
|
|
public TestController(ILogger<UserEntity> logger, IRoleService iRoleService, IUserService iUserService, IStringLocalizer<LocalLanguage> local)
|
2022-04-09 16:16:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
_local = local;
|
2022-04-12 18:09:13 +08:00
|
|
|
|
_iUserService = iUserService;
|
2022-04-16 00:01:00 +08:00
|
|
|
|
_iRoleService = iRoleService;
|
2022-04-09 16:16:32 +08:00
|
|
|
|
}
|
2022-04-12 18:09:13 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 仓储上下文对象测试
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
2022-04-13 21:36:50 +08:00
|
|
|
|
// 特点:化繁为简!意框架仓储代理上下文对象,用起来就是爽,但最好按规范来爽!
|
2022-04-14 23:13:34 +08:00
|
|
|
|
// 规范:控制器不建议使用切换仓储方法、控制器严禁使用DB上下文对象,其它怎么爽怎么来!
|
2022-04-12 18:09:13 +08:00
|
|
|
|
public async Task<Result> DbTest()
|
|
|
|
|
|
{
|
2022-04-13 21:36:50 +08:00
|
|
|
|
//非常好,使用UserService的特有方法
|
|
|
|
|
|
await _iUserService.DbTest();
|
|
|
|
|
|
|
2022-04-16 00:01:00 +08:00
|
|
|
|
//非常好,依赖注入使用其他Service的特有方法
|
|
|
|
|
|
await _iRoleService.DbTest();
|
2022-04-13 21:36:50 +08:00
|
|
|
|
|
|
|
|
|
|
//很核理,使用仓储的通用方法
|
|
|
|
|
|
await _iUserService._repository.GetListAsync();
|
|
|
|
|
|
|
2022-04-16 00:01:00 +08:00
|
|
|
|
//挺不错,依赖注入其他仓储
|
|
|
|
|
|
await _iRoleService._repository.GetListAsync();
|
2022-04-13 21:36:50 +08:00
|
|
|
|
|
2022-04-16 00:01:00 +08:00
|
|
|
|
//不建议操作,直接切换其他仓储
|
|
|
|
|
|
await _iUserService._repository.ChangeRepository<Repository<RoleEntity>>().GetListAsync();
|
2022-04-13 21:36:50 +08:00
|
|
|
|
|
2022-04-16 00:01:00 +08:00
|
|
|
|
//直接操作Db对象???恭喜你已经毕业了!此后将有一天,接手到这个的软件的程序员将破口大骂。
|
2022-04-13 21:36:50 +08:00
|
|
|
|
await _iUserService._repository._Db.Queryable<UserEntity>().ToListAsync();
|
|
|
|
|
|
|
2022-04-12 18:09:13 +08:00
|
|
|
|
return Result.Success().SetData(await _iUserService.DbTest());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-14 20:44:28 +08:00
|
|
|
|
/// <summary>
|
2022-04-14 23:13:34 +08:00
|
|
|
|
/// 执行Sql返回
|
2022-04-14 20:44:28 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
//简单语句不推荐!
|
|
|
|
|
|
public async Task<Result> SqlTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Success().SetData(await _iUserService._repository.UseSqlAsync<UserEntity>("select * from User"));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-09 16:16:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 国际化测试
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
2022-04-13 21:36:50 +08:00
|
|
|
|
//根据浏览器语言设置来切换输出
|
2022-04-09 16:16:32 +08:00
|
|
|
|
public Result LocalTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Success().SetData(_local["succeed"]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 权限测试
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
[Permission("user:get:test")]
|
|
|
|
|
|
public Result PermissionTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Success();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 策略授权测试
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
[Authorize(PolicyName.Sid)]
|
2022-04-14 23:13:34 +08:00
|
|
|
|
public Result AuthTest()
|
2022-04-09 16:16:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
return Result.Success();
|
|
|
|
|
|
}
|
2022-04-13 21:36:50 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步事务测试
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
//注册一个用户获取它的信息之后再更新它,但是这个年龄可能会报错
|
|
|
|
|
|
//如果一个事务中有任何一个错误,将会把所有执行过的操作进行回滚,确保数据的原子性
|
|
|
|
|
|
public async Task<Result> TranTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
UserEntity user = new() { UserName = $"杰哥{DateTime.Now}", Password = "5201314", Age = 99 };
|
|
|
|
|
|
|
|
|
|
|
|
var res = await _iUserService._repository.UseTranAsync(async () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await _iUserService.Register(user, (o) => user = o);
|
|
|
|
|
|
user.Age = 18 / (new Random().Next(0, 2));
|
|
|
|
|
|
await _iUserService._repository.UpdateAsync(user);
|
|
|
|
|
|
});
|
|
|
|
|
|
if (res)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Success("执行成功!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Error("发生错误,插入已回滚!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-16 00:01:00 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 极爽导航属性
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
|
//Sqlsugar精髓之一!必学!最新版本
|
|
|
|
|
|
public async Task<Result> IncludeTest()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Result.Success().SetData(await _iUserService.GetListInRole());
|
|
|
|
|
|
}
|
2022-04-09 16:16:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|