inotify/Inotify/Controllers/SetttingSysControlor.cs
2021-03-24 23:09:35 +08:00

130 lines
5.1 KiB
C#

using Inotify.Data;
using Inotify.Data.Models;
using Inotify.Data.Models.System;
using Inotify.Sends;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inotify.Controllers
{
[ApiController]
[Route("api/settingsys")]
public class SetttingSysControlor : BaseController
{
[HttpGet, Route("GetGlobal"), Authorize(Policys.Systems)]
public IActionResult GetGlobal()
{
var proxyenable = SendCacheStore.GetSystemValue("proxyenable");
var githubEnable = SendCacheStore.GetSystemValue("githubEnable");
return OK(new
{
sendthread = SendCacheStore.GetSystemValue("sendthread"),
proxy = SendCacheStore.GetSystemValue("proxy"),
proxyenable = proxyenable != "" && bool.Parse(proxyenable),
administrators = SendCacheStore.GetSystemValue("administrators"),
githubClientID = SendCacheStore.GetSystemValue("githubClientID"),
githubClientSecret = SendCacheStore.GetSystemValue("githubClientSecret"),
githubEnable = githubEnable != "" && bool.Parse(githubEnable),
});
}
[HttpPost, Route("SetGlobal"), Authorize(Policys.Systems)]
public IActionResult SetGlobal(
string? sendthread,
string? administrators,
string? proxy,
string? proxyenable,
string? githubClientID,
string? githubClientSecret,
string? githubEnable)
{
SendCacheStore.SetSystemValue("sendthread", sendthread);
SendCacheStore.SetSystemValue("administrators", administrators);
SendCacheStore.SetSystemValue("proxy", proxy);
SendCacheStore.SetSystemValue("proxyenable", proxyenable);
SendCacheStore.SetSystemValue("githubClientID", githubClientID);
SendCacheStore.SetSystemValue("githubClientSecret", githubClientSecret);
SendCacheStore.SetSystemValue("githubEnable", githubEnable);
SendTaskManager.Instance.Stop();
SendTaskManager.Instance.Run();
return OK();
}
[HttpGet, Route("GetJWT"), Authorize(Policys.Systems)]
public IActionResult GetJWT()
{
return OK(DBManager.Instance.JWT);
}
[HttpPost, Route("SetJWT"), Authorize(Policys.Systems)]
public IActionResult SetJWT(JwtInfo jwt)
{
DBManager.Instance.JWT = jwt;
StartUpManager.Load().Restart();
return OK();
}
[HttpPost, Route("DeleteUser"), Authorize(Policys.Systems)]
public IActionResult DeleteUser(string userName)
{
var userInfo = DBManager.Instance.GetUser(userName);
var userId = userInfo.Id;
if (userInfo != null)
{
DBManager.Instance.DBase.Delete(userInfo);
DBManager.Instance.DBase.DeleteMany<SendAuthInfo>().Where(e => e.UserId == userId);
return OK();
}
return Fail();
}
[HttpPost, Route("ActiveUser"), Authorize(Policys.Systems)]
public IActionResult ActiveUser(string userName, bool active)
{
var userInfo = DBManager.Instance.GetUser(userName);
if (userInfo != null)
{
userInfo.Active = active;
DBManager.Instance.DBase.Update(userInfo, e => e.Active);
return OK();
}
return Fail();
}
[HttpGet, Route("GetUsers"), Authorize(Policys.Systems)]
public IActionResult GetUsers(string? query, int page, int pageSize)
{
if (query == null)
return OK(DBManager.Instance.DBase.Query<SendUserInfo>().ToPage(page, pageSize));
else return OK(DBManager.Instance.DBase.Query<SendUserInfo>().Where(e => e.UserName.Contains(query) || e.Email.Contains(query)).ToPage(page, pageSize));
}
[HttpGet, Route("GetSendInfos"), Authorize(Policys.Systems)]
public IActionResult GetSendInfos(string? start, string? end)
{
var templates = SendTaskManager.Instance.GetInputTemeplates();
var sendInfos = DBManager.Instance.DBase.Fetch<SendInfo>();
var sendInfoQuerys = sendInfos.Where(e => int.Parse(e.Date) >= int.Parse(start) && int.Parse(e.Date) <= int.Parse(end)).ToList();
var sendInfoGroups = sendInfoQuerys.GroupBy(e => e.Date).Select(e => new { date = e.Key, count = e.Sum(item => item.Count) }).ToList();
var sendTypeInfoGroups = sendInfoQuerys.GroupBy(e => e.TemplateID).Select(e => new { date = e.Key, count = e.Sum(item => item.Count) }).ToList();
var result = new
{
dataX = sendInfoGroups.Select(e => e.date).ToList(),
dataY = sendInfoGroups.Select(e => e.count).ToList(),
items = sendTypeInfoGroups.Select(e => new { name = templates[e.date].Name, value = e.count })
};
return OK(result);
}
}
}