2020-04-14 11:43:48 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-04-15 17:54:33 +08:00
|
|
|
|
using Falcon.Extend;
|
|
|
|
|
using FAuth.DataBase.Tables;
|
2020-05-06 15:57:09 +08:00
|
|
|
|
using FAuth.Extensions;
|
2020-04-15 17:54:33 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-04-14 11:43:48 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace FAuth.Controllers.api
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 角色控制器
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RoleController:ApiControllerBase<RoleController>
|
|
|
|
|
{
|
|
|
|
|
public RoleController(ILogger<RoleController> logger,IServiceProvider service) : base(logger,service) {
|
|
|
|
|
}
|
2020-04-15 17:54:33 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加新角色
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleName">角色名称</param>
|
|
|
|
|
/// <param name="description">角色说明</param>
|
|
|
|
|
/// <returns>角色信息</returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(Roles),200)]
|
|
|
|
|
public Roles AddNewRole(string roleName,string description) {
|
|
|
|
|
if(roleName.IsNullOrEmpty()) {
|
2020-05-06 15:57:09 +08:00
|
|
|
|
throw new ApiArgumentNullException(nameof(roleName));
|
2020-04-15 17:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
var qu = this.Db.Roles.Where(m => m.Name == roleName);
|
|
|
|
|
if(qu.Any()) {
|
2020-05-06 15:57:09 +08:00
|
|
|
|
throw new ApiException($"角色{roleName}已经存在,不可以重复创建!");
|
2020-04-15 17:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
var newRole = new Roles {
|
|
|
|
|
Name = roleName,
|
|
|
|
|
Description = description,
|
|
|
|
|
};
|
|
|
|
|
this.Db.Entry(newRole).State = EntityState.Added;
|
|
|
|
|
this.Db.SaveChangesAsync().Wait();
|
|
|
|
|
return newRole;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询角色是否注册
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleName">角色名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(Roles),200)]
|
2020-05-06 16:02:54 +08:00
|
|
|
|
public Roles GetRoles(string roleName) {
|
2020-04-15 17:54:33 +08:00
|
|
|
|
var qu = this.Db.Roles.Where(m => m.Name == roleName);
|
|
|
|
|
if(qu.Any()) {
|
|
|
|
|
return qu.First();
|
|
|
|
|
} else {
|
2020-05-06 15:57:09 +08:00
|
|
|
|
throw new ApiException("没有找到该角色");
|
2020-04-15 17:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 11:43:48 +08:00
|
|
|
|
}
|
2020-04-14 16:44:05 +08:00
|
|
|
|
|
2020-04-14 11:43:48 +08:00
|
|
|
|
}
|