using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
{
///
/// 角色控制器
///
public class RoleController:ApiControllerBase
{
public RoleController(ILogger logger,IServiceProvider service) : base(logger,service) {
}
///
/// 添加新角色
///
/// 角色名称
/// 角色说明
/// 角色信息
[HttpPost]
[ProducesResponseType(typeof(Roles),200)]
public Roles AddNewRole(string roleName,string description) {
if(roleName.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(roleName));
}
var qu = this.Db.Roles.Where(m => m.Name == roleName);
if(qu.Any()) {
throw new Exception($"角色{roleName}已经存在,不可以重复创建!");
}
var newRole = new Roles {
Name = roleName,
Description = description,
};
this.Db.Entry(newRole).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
return newRole;
}
///
/// 查询角色是否注册
///
/// 角色名称
///
[HttpPost]
[ProducesResponseType(typeof(Roles),200)]
public Roles GetApps(string roleName) {
if(string.IsNullOrEmpty(roleName))
throw new ArgumentException("message",nameof(roleName));
var qu = this.Db.Roles.Where(m => m.Name == roleName);
if(qu.Any()) {
return qu.First();
} else {
throw new Exception("没有找到该角色");
}
}
}
}