using System;
using System.Linq;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
{
///
/// 应用角色组对应
///
public class App_RoleGroupController:ApiControllerBase
{
public App_RoleGroupController(ILogger logger,IServiceProvider service) : base(logger,service) {
}
///
/// 绑定指定应用和角色组
///
/// 应用编号
/// 角色组编号
/// 是否成功
[HttpPost]
public bool Binding(int appId,int roleGroupId) {
var aQu = this.Db.Apps.Where(m => m.Id == appId);
if(!aQu.Any()) {
throw new ApiException("没有找到指定的应用");
}
var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rQu.Any()) {
throw new ApiException("没有找到指定的角色组");
}
var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId);
if(!arQu.Any()) {
var model = new App_RoleGroup {
AppId = appId,
RoleGroupId = roleGroupId,
};
this.Db.Entry(model).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
}
return true;
}
///
/// 解除指定应用和角色组绑定
///
/// 应用编号
/// 角色组编号
/// 解绑是否成功
[HttpPost]
public bool Unbind(int appId,int roleGroupId) {
var aQu = this.Db.Apps.Where(m => m.Id == appId);
if(!aQu.Any()) {
throw new ApiException("没有找到指定的应用");
}
var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId);
if(!rQu.Any()) {
throw new ApiException("没有找到指定的角色组");
}
var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId);
if(arQu.Any()) {
foreach(var item in arQu) {
this.Db.Entry(item).State = EntityState.Deleted;
}
this.Db.SaveChangesAsync().Wait();
}
return true;
}
}
}