using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using AutoMapper;
using New_College.Common.HttpContextUser;
using New_College.Common.HttpRestSharp;
using New_College.Common.WebApiClients.HttpApis;
using New_College.Filter;
using New_College.IServices;
using New_College.Model;
using New_College.Model.Models;
using New_College.Model.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace New_College.Controllers
{
///
/// Values控制器
///
[Route("api/[controller]")]
[ApiController]
//[Authorize]
//[Authorize(Roles = "Admin,Client")]
//[Authorize(Policy = "SystemOrAdmin")]
//[Authorize(PermissionNames.Permission)]
[Authorize]
public class ValuesController : ControllerBase
{
private IMapper _mapper;
private readonly IAdvertisementServices _advertisementServices;
private readonly Love _love;
private readonly IRoleModulePermissionServices _roleModulePermissionServices;
private readonly IUser _user;
private readonly IPasswordLibServices _passwordLibServices;
private readonly IBlogApi _blogApi;
private readonly IDoubanApi _doubanApi;
readonly IBlogArticleServices _blogArticleServices;
///
/// ValuesController
///
///
///
///
///
///
///
///
///
///
public ValuesController(IBlogArticleServices blogArticleServices, IMapper mapper, IAdvertisementServices advertisementServices, Love love, IRoleModulePermissionServices roleModulePermissionServices, IUser user, IPasswordLibServices passwordLibServices, IBlogApi blogApi, IDoubanApi doubanApi)
{
// 测试 Authorize 和 mapper
_mapper = mapper;
_advertisementServices = advertisementServices;
_love = love;
_roleModulePermissionServices = roleModulePermissionServices;
// 测试 Httpcontext
_user = user;
// 测试多库
_passwordLibServices = passwordLibServices;
// 测试http请求
_blogApi = blogApi;
_doubanApi = doubanApi;
// 测试AOP加载顺序,配合 return
_blogArticleServices = blogArticleServices;
}
///
/// Get方法
///
///
// GET api/values
[HttpGet]
[AllowAnonymous]
public async Task> Get()
{
var data = new MessageModel();
/*
* 测试 sql 查询
*/
var queryBySql = await _blogArticleServices.QuerySql("SELECT bsubmitter,btitle,bcontent,bCreateTime FROM BlogArticle WHERE bID>5");
/*
* 测试 sql 更新
*
* 【SQL参数】:@bID:5
* @bsubmitter:laozhang619
* @IsDeleted:False
* 【SQL语句】:UPDATE `BlogArticle` SET
* `bsubmitter`=@bsubmitter,`IsDeleted`=@IsDeleted WHERE `bID`=@bID
*/
var updateSql = await _blogArticleServices.Update(new { bsubmitter = $"laozhang{DateTime.Now.Millisecond}", IsDeleted = false, bID = 5 });
// 测试模拟异常,全局异常过滤器拦截
var i = 0;
var d = 3 / i;
// 测试 AOP 缓存
var blogArticles = await _blogArticleServices.GetBlogs();
// 测试多表联查
var roleModulePermissions = await _roleModulePermissionServices.QueryMuchTable();
// 测试多个异步执行时间
var roleModuleTask = _roleModulePermissionServices.Query();
var listTask = _advertisementServices.Query();
var ad = await roleModuleTask;
var list = await listTask;
// 测试service层返回异常
_advertisementServices.ReturnExp();
Love love = null;
love.SayLoveU();
return data;
}
///
/// Get(int id)方法
///
///
///
// GET api/values/5
[HttpGet("{id}")]
[AllowAnonymous]
//[TypeFilter(typeof(DeleteSubscriptionCache),Arguments =new object[] { "1"})]
[TypeFilter(typeof(UseServiceDIAttribute), Arguments = new object[] { "laozhang" })]
public ActionResult Get(int id)
{
var loveu = _love.SayLoveU();
return "value";
}
///
/// 测试参数是必填项
///
///
///
[HttpGet]
[Route("/api/values/RequiredPara")]
public string RequiredP([Required]string id)
{
return id;
}
///
/// 通过 HttpContext 获取用户信息
///
/// 声明类型,默认 jti
///
[HttpGet]
[Route("/api/values/UserInfo")]
public MessageModel> GetUserInfo(string ClaimType = "jti")
{
var getUserInfoByToken = _user.GetUserInfoFromToken(ClaimType);
return new MessageModel>()
{
success = _user.IsAuthenticated(),
msg = _user.IsAuthenticated() ? _user.Name.ObjToString() : "未登录",
response = _user.GetClaimValueByType(ClaimType)
};
}
///
/// to redirect by route template name.
///
[HttpGet("/api/custom/go-destination")]
[AllowAnonymous]
public void Source()
{
var url = Url.RouteUrl("Destination_Route");
Response.Redirect(url);
}
///
/// route with template name.
///
///
[HttpGet("/api/custom/destination", Name = "Destination_Route")]
[AllowAnonymous]
public string Destination()
{
return "555";
}
///
/// 测试 post 一个对象 + 独立参数
///
/// model实体类参数
/// 独立参数
[HttpPost]
[AllowAnonymous]
public object Post([FromBody] BlogArticle blogArticle, int id)
{
return Ok(new { success = true, data = blogArticle, id = id });
}
///
/// 测试 post 参数
///
///
///
[HttpPost]
[Route("TestPostPara")]
[AllowAnonymous]
public object TestPostPara(string name)
{
return Ok(new { success = true, name = name });
}
///
/// 测试http请求 RestSharp Get
///
///
[HttpGet("RestsharpGet")]
[AllowAnonymous]
public TestRestSharpGetDto RestsharpGet()
{
return HttpHelper.GetApi("http://apk.neters.club/", "api/Blog/DetailNuxtNoPer", "id=1");
}
///
/// 测试http请求 RestSharp Post
///
///
[HttpGet("RestsharpPost")]
[AllowAnonymous]
public TestRestSharpPostDto RestsharpPost()
{
return HttpHelper.PostApi("http://apk.neters.club/api/Values/TestPostPara?name=老张", new { age = 18 });
}
///
/// 测试多库连接
///
///
[HttpGet("TestMutiDBAPI")]
[AllowAnonymous]
public async Task