using System;
using System.Collections.Generic;
using System.Linq;
using Falcon.Extend;
using FAuth.DataBase.Tables;
using FAuth.Extensions;
using FAuth.Extensions.Account;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace FAuth.Controllers.api
{
///
/// 应用程序控制器
///
public class AppController:ApiControllerBase
{
public AccountHelper Account { get; set; }
public AppController(ILogger logger,IServiceProvider service) : base(logger,service) {
this.Account = this.Services.GetService();
}
///
/// 添加新应用
///
/// 应用名称
/// 应用说明
/// 应用信息
[HttpPost]
[ProducesResponseType(typeof(Apps),200)]
public Apps AddNewApp(string appName,string description) {
if(appName.IsNullOrEmpty()) {
throw new ApiArgumentNullException(nameof(appName));
}
var qu = this.Db.Apps.Where(m => m.Name == appName);
if(qu.Any()) {
throw new ApiException($"应用{appName}已经存在,不可以重复创建!");
}
var newApp = new Apps {
Name = appName,
Description = description,
};
this.Db.Entry(newApp).State = EntityState.Added;
this.Db.SaveChangesAsync().Wait();
return newApp;
}
///
/// 删除一个应用
///
/// 应用编号
/// 删除结果
[HttpPost]
public bool RemoveApp(int id) {
if(id == 1) {
throw new ApiException("系统应用不可以删除");
}
var qu = this.Db.Apps.Where(m => m.Id == id);
foreach(var item in qu) {
this.Db.Entry(item).State = EntityState.Deleted;
}
this.Db.SaveChangesAsync().Wait();
return true;
}
///
/// 查询app是否注册
///
/// app名称
///
[HttpPost]
[ProducesResponseType(typeof(Apps),200)]
public Apps GetApps(string appName) {
if(string.IsNullOrEmpty(appName))
throw new ApiArgumentNullException(nameof(appName));
var qu = this.Db.Apps.Where(m => m.Name == appName);
if(qu.Any()) {
return qu.First();
} else {
throw new ApiException("没有找到该应用");
}
}
///
/// 通过管理员凭据获取应用列表
///
/// 管理员凭据
/// 应用列表
[HttpGet]
[ProducesResponseType(typeof(IEquatable),200)]
public IEnumerable GetAppList(string adminTicket) {
if(this.Account.IsNotSystemAdmin(adminTicket)) {
throw new ApiException("需要提供管理员票据");
}
var list = this.Cache.GetObj>("");
var qu = this.Db.Apps;
return qu.ToList();
}
}
}