From f64d7a812620f8744823a7603a27ad70c66c98ab Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Mon, 9 Jan 2023 18:58:27 -0600 Subject: [PATCH] Add basic settings page --- .gitignore | 1 + .../Controllers/SettingsController.cs | 38 ++++++++++++ LANCommander/Models/Settings.cs | 8 +++ LANCommander/Services/SettingService.cs | 37 +++++++++++ LANCommander/Views/Settings/General.cshtml | 61 +++++++++++++++++++ .../Views/Settings/_SidebarPartial.cshtml | 7 +++ .../Views/Shared/_LoginPartial.cshtml | 1 + 7 files changed, 153 insertions(+) create mode 100644 LANCommander/Controllers/SettingsController.cs create mode 100644 LANCommander/Models/Settings.cs create mode 100644 LANCommander/Services/SettingService.cs create mode 100644 LANCommander/Views/Settings/General.cshtml create mode 100644 LANCommander/Views/Settings/_SidebarPartial.cshtml diff --git a/.gitignore b/.gitignore index 6aec633..b02cbd6 100644 --- a/.gitignore +++ b/.gitignore @@ -350,3 +350,4 @@ MigrationBackup/ .ionide/ Upload/ LANCommander/Icon/ +LANCommander/Settings.yml diff --git a/LANCommander/Controllers/SettingsController.cs b/LANCommander/Controllers/SettingsController.cs new file mode 100644 index 0000000..4a7b867 --- /dev/null +++ b/LANCommander/Controllers/SettingsController.cs @@ -0,0 +1,38 @@ +using LANCommander.Models; +using LANCommander.Services; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace LANCommander.Controllers +{ + [Authorize] + public class SettingsController : Controller + { + private readonly SettingService SettingService; + + public SettingsController(SettingService settingService) + { + SettingService = settingService; + } + + public IActionResult Index() + { + return RedirectToAction(nameof(General)); + } + + public IActionResult General() + { + var settings = SettingService.GetSettings(); + + return View(settings); + } + + [HttpPost] + public IActionResult General(LANCommanderSettings settings) + { + SettingService.SaveSettings(settings); + + return RedirectToAction(nameof(General)); + } + } +} diff --git a/LANCommander/Models/Settings.cs b/LANCommander/Models/Settings.cs new file mode 100644 index 0000000..55ab868 --- /dev/null +++ b/LANCommander/Models/Settings.cs @@ -0,0 +1,8 @@ +namespace LANCommander.Models +{ + public class LANCommanderSettings + { + public string IGDBClientId { get; set; } + public string IGDBClientSecret { get; set; } + } +} diff --git a/LANCommander/Services/SettingService.cs b/LANCommander/Services/SettingService.cs new file mode 100644 index 0000000..ffedde5 --- /dev/null +++ b/LANCommander/Services/SettingService.cs @@ -0,0 +1,37 @@ +using LANCommander.Models; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace LANCommander.Services +{ + public class SettingService + { + private const string SettingsFilename = "Settings.yml"; + + public LANCommanderSettings GetSettings() + { + if (File.Exists(SettingsFilename)) + { + var contents = File.ReadAllText(SettingsFilename); + + var deserializer = new DeserializerBuilder() + .IgnoreUnmatchedProperties() + .WithNamingConvention(PascalCaseNamingConvention.Instance) + .Build(); + + return deserializer.Deserialize(contents); + } + else + return new LANCommanderSettings(); + } + + public void SaveSettings(LANCommanderSettings settings) + { + var serializer = new SerializerBuilder() + .WithNamingConvention(PascalCaseNamingConvention.Instance) + .Build(); + + File.WriteAllText(SettingsFilename, serializer.Serialize(settings)); + } + } +} diff --git a/LANCommander/Views/Settings/General.cshtml b/LANCommander/Views/Settings/General.cshtml new file mode 100644 index 0000000..cd5f9b5 --- /dev/null +++ b/LANCommander/Views/Settings/General.cshtml @@ -0,0 +1,61 @@ +@model LANCommander.Models.LANCommanderSettings + +@{ + ViewData["Title"] = "Settings"; +} + +
+ + +
+ +
+
+
+
+ @{ + await Html.RenderPartialAsync("_SidebarPartial"); + } + +
+
+

General

+

IGDB Credentials

+

In order to use IGDB metadata, you need a Twitch developer account. Click here for more details.

+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+
+
+ +@section Scripts { + @{ + await Html.RenderPartialAsync("_ValidationScriptsPartial"); + } +} diff --git a/LANCommander/Views/Settings/_SidebarPartial.cshtml b/LANCommander/Views/Settings/_SidebarPartial.cshtml new file mode 100644 index 0000000..ca6df5a --- /dev/null +++ b/LANCommander/Views/Settings/_SidebarPartial.cshtml @@ -0,0 +1,7 @@ +
+
+
+ General +
+
+
\ No newline at end of file diff --git a/LANCommander/Views/Shared/_LoginPartial.cshtml b/LANCommander/Views/Shared/_LoginPartial.cshtml index 57ab534..7b52e53 100644 --- a/LANCommander/Views/Shared/_LoginPartial.cshtml +++ b/LANCommander/Views/Shared/_LoginPartial.cshtml @@ -13,6 +13,7 @@