using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
namespace NCC.DynamicApiController
{
///
/// 常量、公共方法配置类
///
internal static class Penetrates
{
///
/// 分组分隔符
///
internal const string GroupSeparator = "@";
///
/// 请求动词映射字典
///
internal static ConcurrentDictionary VerbToHttpMethods { get; private set; }
///
/// 控制器排序集合
///
internal static ConcurrentDictionary ControllerOrderCollection { get; set; }
///
/// 构造函数
///
static Penetrates()
{
ControllerOrderCollection = new ConcurrentDictionary();
VerbToHttpMethods = new ConcurrentDictionary
{
["post"] = "POST",
["add"] = "POST",
["create"] = "POST",
["insert"] = "POST",
["submit"] = "POST",
["get"] = "GET",
["find"] = "GET",
["fetch"] = "GET",
["query"] = "GET",
//["getlist"] = "GET",
//["getall"] = "GET",
["put"] = "PUT",
["update"] = "PUT",
["delete"] = "DELETE",
["remove"] = "DELETE",
["clear"] = "DELETE",
["patch"] = "PATCH"
};
IsApiControllerCached = new ConcurrentDictionary();
}
///
/// 缓存集合
///
private static readonly ConcurrentDictionary IsApiControllerCached;
///
/// 是否是Api控制器
///
/// type
///
internal static bool IsApiController(Type type)
{
return IsApiControllerCached.GetOrAdd(type, Function);
// 本地静态方法
static bool Function(Type type)
{
// 不能是非公开、基元类型、值类型、抽象类、接口、泛型类
if (!type.IsPublic || type.IsPrimitive || type.IsValueType || type.IsAbstract || type.IsInterface || type.IsGenericType) return false;
// 继承 ControllerBase 或 实现 IDynamicApiController 的类型 或 贴了 [DynamicApiController] 特性
if ((!typeof(Controller).IsAssignableFrom(type) && typeof(ControllerBase).IsAssignableFrom(type)) || typeof(IDynamicApiController).IsAssignableFrom(type) || type.IsDefined(typeof(DynamicApiControllerAttribute), true))
{
// 不是能被导出忽略的接口
if (type.IsDefined(typeof(ApiExplorerSettingsAttribute), true) && type.GetCustomAttribute(true).IgnoreApi) return false;
return true;
}
return false;
}
}
}
}