Updated web ui to razor
This commit is contained in:
71
WebUI_Old/Controllers/HomeController.cs
Normal file
71
WebUI_Old/Controllers/HomeController.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using DiscordBotCore.Bot;
|
||||
using DiscordBotCore.PluginManagement.Loading;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ILogger = DiscordBotCore.Logging.ILogger;
|
||||
|
||||
namespace WebUI_OLD.Controllers;
|
||||
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IDiscordBotApplication _discordBotApplication;
|
||||
private readonly IPluginLoader _pluginLoader;
|
||||
|
||||
public HomeController(ILogger logger, IDiscordBotApplication discordBotApplication, IPluginLoader pluginLoader)
|
||||
{
|
||||
_logger = logger;
|
||||
_discordBotApplication = discordBotApplication;
|
||||
_pluginLoader = pluginLoader;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Index()
|
||||
{
|
||||
_logger.Log("Index page loaded", this);
|
||||
ViewBag.IsRunning = _discordBotApplication.IsReady;
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> StartApplication()
|
||||
{
|
||||
if (_discordBotApplication.IsReady)
|
||||
{
|
||||
_logger.Log("Application already started", this);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
await _discordBotApplication.StartAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> StopApplication()
|
||||
{
|
||||
if (!_discordBotApplication.IsReady)
|
||||
{
|
||||
_logger.Log("Application already stopped", this);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
await _discordBotApplication.StopAsync();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public JsonResult GetLogs()
|
||||
{
|
||||
var logText = _logger.GetLogsHistory();
|
||||
var logs = logText.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
||||
return Json(logs);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> LoadPlugins()
|
||||
{
|
||||
_logger.Log("Loading plugins", this);
|
||||
await _pluginLoader.LoadPlugins();
|
||||
//_logger.Log("Plugins loaded", this);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
}
|
||||
117
WebUI_Old/Controllers/PluginsController.cs
Normal file
117
WebUI_Old/Controllers/PluginsController.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using DiscordBotCore.PluginManagement;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebUI_OLD.Models;
|
||||
using ILogger = DiscordBotCore.Logging.ILogger;
|
||||
|
||||
namespace WebUI_OLD.Controllers;
|
||||
|
||||
public class PluginsController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IPluginManager _pluginManager;
|
||||
|
||||
public PluginsController(ILogger logger, IPluginManager pluginManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_pluginManager = pluginManager;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> OnlinePlugins()
|
||||
{
|
||||
_logger.Log("Getting plugins page", this);
|
||||
var plugins = await _pluginManager.GetPluginsList();
|
||||
|
||||
_logger.Log($"{plugins.Count} Plugins loaded", this);
|
||||
List<OnlinePluginViewModel> pluginViewModels = new List<OnlinePluginViewModel>();
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
OnlinePluginViewModel pluginViewModel = new OnlinePluginViewModel();
|
||||
pluginViewModel.Name = plugin.Name;
|
||||
pluginViewModel.Description = plugin.Description;
|
||||
pluginViewModel.Author = plugin.Author;
|
||||
pluginViewModel.Version = plugin.Version;
|
||||
pluginViewModel.DownloadUrl = plugin.DownloadLink;
|
||||
pluginViewModel.Id = plugin.Id;
|
||||
|
||||
pluginViewModels.Add(pluginViewModel);
|
||||
}
|
||||
|
||||
return View(pluginViewModels);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> InstalledPlugins()
|
||||
{
|
||||
_logger.Log("Getting plugins page", this);
|
||||
var plugins = await _pluginManager.GetInstalledPlugins();
|
||||
_logger.Log($"{plugins.Count} Plugins loaded", this);
|
||||
List<InstalledPluginViewModel> pluginViewModels = new List<InstalledPluginViewModel>();
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
InstalledPluginViewModel pluginViewModel = new InstalledPluginViewModel();
|
||||
pluginViewModel.Name = plugin.PluginName;
|
||||
pluginViewModel.Version = plugin.PluginVersion;
|
||||
pluginViewModel.IsOfflineAdded = plugin.IsOfflineAdded;
|
||||
|
||||
pluginViewModels.Add(pluginViewModel);
|
||||
}
|
||||
|
||||
return View(pluginViewModels);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> DeletePlugin(string pluginName)
|
||||
{
|
||||
_logger.Log($"Deleting plugin {pluginName}", this);
|
||||
//TODO: Implement delete plugin
|
||||
return RedirectToAction("InstalledPlugins");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> GetPluginDetails(string? pluginName)
|
||||
{
|
||||
if (pluginName == null)
|
||||
{
|
||||
_logger.Log("The plugin name is invalid", this);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
_logger.Log($"Gathering information about {pluginName}", this);
|
||||
|
||||
var pluginData = await _pluginManager.GetPluginDataByName(pluginName);
|
||||
if (pluginData is null)
|
||||
{
|
||||
_logger.Log($"Plugin {pluginName} not found", this);
|
||||
return NotFound("Plugin not found");
|
||||
}
|
||||
|
||||
PluginDetailsViewModel model = new PluginDetailsViewModel
|
||||
{
|
||||
PluginName = pluginName,
|
||||
Author = pluginData.Author,
|
||||
Description = pluginData.Description,
|
||||
Version = pluginData.Version
|
||||
};
|
||||
|
||||
return View("PluginDetails", model);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> InstallPlugin(int pluginId)
|
||||
{
|
||||
var pluginData = await _pluginManager.GetPluginDataById(pluginId);
|
||||
if (pluginData is null)
|
||||
{
|
||||
_logger.Log($"Plugin with ID {pluginId} not found", this);
|
||||
return RedirectToAction("OnlinePlugins");
|
||||
}
|
||||
|
||||
IProgress<float> progress = new Progress<float>(f => _logger.Log($"Installing: {f}"));
|
||||
|
||||
await _pluginManager.InstallPlugin(pluginData, progress);
|
||||
|
||||
_logger.Log($"Plugin {pluginData.Name} installed", this);
|
||||
return RedirectToAction("OnlinePlugins");
|
||||
}
|
||||
}
|
||||
70
WebUI_Old/Controllers/SettingsController.cs
Normal file
70
WebUI_Old/Controllers/SettingsController.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebUI_OLD.Models;
|
||||
using IConfiguration = DiscordBotCore.Configuration.IConfiguration;
|
||||
using ILogger = DiscordBotCore.Logging.ILogger;
|
||||
|
||||
namespace WebUI_OLD.Controllers;
|
||||
|
||||
public class SettingsController : Controller
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
public SettingsController(ILogger logger, IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
_logger.Log("Getting settings page", this);
|
||||
SettingsViewModel model = new SettingsViewModel
|
||||
{
|
||||
Token = _configuration.Get<string>("token", string.Empty),
|
||||
Prefix = _configuration.Get<string>("prefix", string.Empty),
|
||||
ServerIds = _configuration.Get<List<ulong>>("ServerIds", new List<ulong>()),
|
||||
};
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> SaveSettings([FromForm] SettingsViewModel model)
|
||||
{
|
||||
_logger.Log("Saving settings", this);
|
||||
|
||||
if (string.IsNullOrEmpty(model.Token))
|
||||
{
|
||||
ModelState.AddModelError("Token", "Token cannot be empty");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(model.Prefix))
|
||||
{
|
||||
ModelState.AddModelError("Prefix", "Prefix cannot be empty");
|
||||
}
|
||||
|
||||
if (model.ServerIds == null || model.ServerIds.Count == 0)
|
||||
{
|
||||
ModelState.AddModelError("ServerIds", "At least one Server ID must be provided");
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return View("Index", model);
|
||||
}
|
||||
|
||||
_configuration.Set("token", model.Token);
|
||||
_configuration.Set("prefix", model.Prefix);
|
||||
_configuration.Set("serverIds", model.ServerIds);
|
||||
|
||||
await _configuration.SaveToFile();
|
||||
_logger.Log("Settings saved successfully", this);
|
||||
|
||||
TempData["Notification"] = "Settings saved successfully!";
|
||||
|
||||
return RedirectToAction("Index", "Home");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user