refactor: 优化 api 接口参数规范

This commit is contained in:
Argo Window10 2019-11-13 16:40:20 +08:00
parent f7dcdb857d
commit c9d2333e59
16 changed files with 55 additions and 75 deletions

View File

@ -8,7 +8,7 @@ using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 网站分析控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -16,11 +16,11 @@ namespace Bootstrap.Admin.Controllers.Api
public class AnalyseController : ControllerBase
{
/// <summary>
///
/// 通过 logType 查询分析数据接口
/// </summary>
/// <returns></returns>
[HttpGet()]
public ActionResult<AnalyseData> Get([FromQuery]string logType = "")
public ActionResult<AnalyseData> Get([FromQuery]string logType)
{
var ret = new AnalyseData();
if (logType.Equals("LoginUsers", StringComparison.OrdinalIgnoreCase))
@ -60,19 +60,19 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// 分析数据实体类
/// </summary>
public class AnalyseData
{
/// <summary>
///
/// 获得/设置 折线数据集合
/// </summary>
public IEnumerable<string> Polylines { get; set; } = new List<string>();
public IEnumerable<string> Polylines { get; set; } = new string[0];
/// <summary>
///
/// 获得 数据集合
/// </summary>
public List<KeyValuePair<string, string>> Datas { get; set; } = new List<KeyValuePair<string, string>>();
public List<KeyValuePair<string, string>> Datas { get; } = new List<KeyValuePair<string, string>>();
}
}
}

View File

@ -1,4 +1,4 @@
using Bootstrap.DataAccess;
using Bootstrap.DataAccess;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
@ -6,7 +6,7 @@ using System.Collections.Generic;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 应用程序控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -14,7 +14,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class AppsController : ControllerBase
{
/// <summary>
///
/// 通过角色ID获取其授权的所有应用程序集合
/// </summary>
/// <param name="id"></param>
/// <returns></returns>

View File

@ -15,7 +15,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class CategoryController : ControllerBase
{
/// <summary>
/// 获取字典表中所有Category数据
/// 获取字典表中所有 Category 数据
/// </summary>
/// <returns></returns>
[HttpGet]
@ -26,7 +26,7 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// 获取所有菜单数据
/// </summary>
/// <returns></returns>
[HttpGet]
@ -36,7 +36,7 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// 获取所有父级菜单数据
/// </summary>
/// <returns></returns>
[HttpGet]

View File

@ -10,7 +10,7 @@ using System.Collections.Generic;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 字典表维护控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -18,7 +18,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class DictsController : ControllerBase
{
/// <summary>
///
/// 获取所有字典表数据方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
@ -28,7 +28,7 @@ namespace Bootstrap.Admin.Controllers.Api
return value.RetrieveData();
}
/// <summary>
///
/// 保存字典方法
/// </summary>
/// <param name="value"></param>
[HttpPost]
@ -38,7 +38,7 @@ namespace Bootstrap.Admin.Controllers.Api
return DictHelper.Save(value);
}
/// <summary>
///
/// 删除字典项方法
/// </summary>
/// <param name="value"></param>
[HttpDelete]

View File

@ -80,12 +80,12 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// 查询服务器端日志文件参数类
/// </summary>
public class ExceptionFileQuery
{
/// <summary>
///
/// 获取/设置 文件名称
/// </summary>
public string FileName { get; set; } = "";
}

View File

@ -10,7 +10,7 @@ using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 部门维护控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -18,7 +18,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class GroupsController : ControllerBase
{
/// <summary>
///
/// 部门数据查询方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
@ -29,18 +29,7 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
public Group Get(string id)
{
return GroupHelper.Retrieves().FirstOrDefault(t => t.Id == id);
}
/// <summary>
///
/// 保存部门方法
/// </summary>
/// <param name="value"></param>
[HttpPost]
@ -51,7 +40,7 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// 删除部门方法
/// </summary>
/// <param name="value"></param>
[HttpDelete]
@ -64,7 +53,7 @@ namespace Bootstrap.Admin.Controllers.Api
/// <summary>
/// 获取部门授权
/// </summary>
/// <param name="id"></param>
/// <param name="id">用户ID或者角色ID</param>
/// <param name="type"></param>
/// <returns></returns>
[HttpPost("{id}")]

View File

@ -7,7 +7,7 @@ using System.Linq;
namespace Bootstrap.Admin.Controllers
{
/// <summary>
///
/// 新用户注册控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]

View File

@ -11,7 +11,7 @@ using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 个人中心控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -30,9 +30,6 @@ namespace Bootstrap.Admin.Controllers.Api
public JsonResult Post(string id, [FromServices]IWebHostEnvironment env, [FromForm]DeleteFileCollection files)
{
if (!id.Equals("Delete", StringComparison.OrdinalIgnoreCase)) return new JsonResult(new object());
var previewUrl = string.Empty;
long fileSize = 0;
var userName = User.Identity.Name;
var fileName = files.Key;
@ -41,9 +38,9 @@ namespace Bootstrap.Admin.Controllers.Api
fileName = "default.jpg";
var webSiteUrl = DictHelper.RetrieveIconFolderPath();
var filePath = Path.Combine(env.WebRootPath, webSiteUrl.Replace("~", string.Empty).Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar) + fileName);
fileSize = new FileInfo(filePath).Length;
var fileSize = new FileInfo(filePath).Length;
var iconName = $"{fileName}?v={DateTime.Now.Ticks}";
previewUrl = Url.Content($"{webSiteUrl}{iconName}");
var previewUrl = Url.Content($"{webSiteUrl}{iconName}");
if (!string.IsNullOrEmpty(userName)) UserHelper.SaveUserIconByName(userName, iconName);
return new JsonResult(new
@ -57,12 +54,12 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// 待删除文件集合类
/// </summary>
public class DeleteFileCollection
{
/// <summary>
///
/// 获得/设置 文件名称
/// </summary>
public string Key { get; set; } = "";
}
@ -109,7 +106,7 @@ namespace Bootstrap.Admin.Controllers.Api
}
/// <summary>
///
/// 个人中心操作方法 更改样式 更改显示名称 更改默认应用
/// </summary>
/// <returns></returns>
[HttpPut]

View File

@ -11,7 +11,7 @@ using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 角色维护控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -19,7 +19,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class RolesController : ControllerBase
{
/// <summary>
///
/// 获取所有角色数据
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
@ -37,7 +37,7 @@ namespace Bootstrap.Admin.Controllers.Api
[HttpPost("{id}")]
public IEnumerable<object> Post(string id, [FromQuery]string type)
{
IEnumerable<Role> ret = new List<Role>();
IEnumerable<Role> ret = new Role[0];
switch (type)
{
case "user":
@ -53,7 +53,7 @@ namespace Bootstrap.Admin.Controllers.Api
return ret.Select(m => new { m.Id, m.Checked, m.RoleName, m.Description });
}
/// <summary>
/// 保存角色
/// 保存角色授权方法
/// </summary>
/// <param name="id">角色ID</param>
/// <param name="values">选中的ID集合</param>
@ -82,7 +82,7 @@ namespace Bootstrap.Admin.Controllers.Api
return ret;
}
/// <summary>
///
/// 保存角色方法
/// </summary>
/// <param name="value"></param>
[HttpPost]
@ -92,7 +92,7 @@ namespace Bootstrap.Admin.Controllers.Api
return RoleHelper.Save(value);
}
/// <summary>
///
/// 删除角色方法
/// </summary>
/// <param name="value"></param>
[HttpDelete]

View File

@ -9,7 +9,7 @@ using System.Collections.Generic;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 网站设置控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -17,7 +17,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class SettingsController : ControllerBase
{
/// <summary>
///
/// 保存网站设置方法
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
@ -26,7 +26,7 @@ namespace Bootstrap.Admin.Controllers.Api
public bool Post([FromBody]BootstrapDict value) => DictHelper.SaveSettings(value);
/// <summary>
///
/// 获取网站缓存站点集合
/// </summary>
[HttpGet]
public IEnumerable<ICacheCorsItem> Get() => CacheManager.CorsSites;

View File

@ -7,7 +7,7 @@ using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 任务管理控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]
@ -15,7 +15,7 @@ namespace Bootstrap.Admin.Controllers.Api
public class TasksController : ControllerBase
{
/// <summary>
///
/// 获取所有任务数据
/// </summary>
/// <returns></returns>
[HttpGet]

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// 任务日志控制器
/// </summary>
[Route("api/[controller]")]
[Authorize]

View File

@ -6,20 +6,22 @@ using System.Linq;
namespace Bootstrap.Admin.Query
{
/// <summary>
///
/// 程序异常查询条件类
/// </summary>
public class QueryExceptionOption : PaginationOption
{
/// <summary>
///
/// 获得/设置 开始时间
/// </summary>
public DateTime? StartTime { get; set; }
/// <summary>
///
/// 获得/设置 结束时间
/// </summary>
public DateTime? EndTime { get; set; }
/// <summary>
///
/// 查询方法
/// </summary>
/// <returns></returns>
public QueryData<object> Retrieves()

View File

@ -34,7 +34,7 @@ namespace Bootstrap.DataAccess
public virtual IEnumerable<App> RetrievesByRoleId(string roleId)
{
using var db = DbManager.Create();
var ret = db.Fetch<App>($"select d.Code as Id, d.Name as AppName, case ra.AppId when d.Code then 'checked' else '' end Checked from Dicts d left join RoleApp ra on d.Code = ra.AppId and ra.RoleId = @1 where d.Code > '0' and d.Category = @0", "应用程序", roleId);
var ret = db.Fetch<App>($"select d.Code as Id, d.Name as AppName, case ra.AppId when d.Code then 'checked' else '' end Checked from Dicts d left join RoleApp ra on d.Code = ra.AppId and ra.RoleId = @1 where d.Category = @0", "应用程序", roleId);
// 判断是否为Administrators
var role = RoleHelper.Retrieves().FirstOrDefault(r => r.Id == roleId);

View File

@ -36,10 +36,10 @@ namespace Bootstrap.DataAccess
private static bool UserChecker(User user)
{
if (user.Description?.Length > 500) user.Description = user.Description.Substring(0, 500);
if (user.UserName?.Length > 16) user.UserName = user.UserName.Substring(0, 16);
if (user.Password?.Length > 50) user.Password = user.Password.Substring(0, 50);
if (user.DisplayName?.Length > 20) user.DisplayName = user.DisplayName.Substring(0, 20);
if (user.Description.Length > 500) user.Description = user.Description.Substring(0, 500);
if (user.UserName.Length > 16) user.UserName = user.UserName.Substring(0, 16);
if (user.Password.Length > 50) user.Password = user.Password.Substring(0, 50);
if (user.DisplayName.Length > 20) user.DisplayName = user.DisplayName.Substring(0, 20);
var pattern = @"^[a-zA-Z0-9_@.]*$";
return Regex.IsMatch(user.UserName, pattern);
}

View File

@ -22,14 +22,6 @@ namespace Bootstrap.Admin.Api.SqlServer
Assert.Single(qd.rows);
}
[Fact]
public async void GetById_Ok()
{
var id = GroupHelper.Retrieves().Where(gp => gp.GroupName == "Admin").First().Id;
var g = await Client.GetAsJsonAsync<Group>(id);
Assert.Equal("Admin", g.GroupName);
}
[Fact]
public async void PostAndDelete_Ok()
{