From 16721e020a59095a53919b99bea42f360dfc1b67 Mon Sep 17 00:00:00 2001 From: 84634E1A607A <60734649+84634E1A607A@users.noreply.github.com> Date: Sat, 20 Jul 2024 18:13:47 +0800 Subject: [PATCH] Added form fields to specify versions (#6) * Added form * Moved Regex out of DEBUG --- ThuInfoWeb/Controllers/HomeController.cs | 33 +++++- ThuInfoWeb/Views/Home/Announce.cshtml | 122 +++++++++++++---------- 2 files changed, 96 insertions(+), 59 deletions(-) diff --git a/ThuInfoWeb/Controllers/HomeController.cs b/ThuInfoWeb/Controllers/HomeController.cs index 4b10b04..09a8653 100644 --- a/ThuInfoWeb/Controllers/HomeController.cs +++ b/ThuInfoWeb/Controllers/HomeController.cs @@ -1,18 +1,22 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using System.Text.RegularExpressions; using ThuInfoWeb.DBModels; using ThuInfoWeb.Models; namespace ThuInfoWeb.Controllers { - public class HomeController : Controller + public partial class HomeController : Controller { private readonly ILogger _logger; private readonly Data _data; private readonly UserManager _userManager; private readonly VersionManager _versionManager; + [GeneratedRegex(@"^\d+\.\d+\.\d+$")] + private static partial Regex VersionRegex(); + public HomeController(ILogger logger, Data data, UserManager userManager, VersionManager versionManager) { _logger = logger; @@ -143,8 +147,26 @@ public async Task Announce([FromQuery] int page = 1) public async Task CreateAnnounce(AnnounceViewModel vm) { if (vm.Title is null || vm.Content is null) return BadRequest("标题或内容为空"); - vm.VisibleNotAfter ??= "9.9.9"; - vm.VisibleExact ??= ""; + var visibleNotAfter = vm.VisibleNotAfter?.Trim() ?? "9.9.9"; + var visibleExact = vm.VisibleExact ?? ""; + + if (!VersionRegex().IsMatch(visibleNotAfter)) + { + return BadRequest("\"在不晚于以下版本生效\"中的版本号格式错误"); + } + + var visibleExactList = visibleExact.Split(',') + .Select(x => x.Trim()) + .Where(x => !string.IsNullOrWhiteSpace(x)) + .ToList(); + + if (visibleExactList.Any(x => !VersionRegex().IsMatch(x))) + { + return BadRequest("\"在以下版本生效\"中的版本号格式错误"); + } + + visibleExact = string.Join(',', visibleExactList); + var user = HttpContext.User.Identity!.Name!; var a = new Announce { @@ -153,8 +175,8 @@ public async Task CreateAnnounce(AnnounceViewModel vm) Author = user, CreatedTime = DateTime.Now, IsActive = vm.IsActive, - VisibleNotAfter = vm.VisibleNotAfter, - VisibleExact = vm.VisibleExact + VisibleNotAfter = visibleNotAfter, + VisibleExact = visibleExact }; var result = await _data.CreateAnnounceAsync(a); if (result != 1) return BadRequest(ModelState); @@ -252,6 +274,7 @@ public IActionResult Stat() { return View(); } + #if DEBUG [Route("Home/Exception")] public IActionResult Exception() diff --git a/ThuInfoWeb/Views/Home/Announce.cshtml b/ThuInfoWeb/Views/Home/Announce.cshtml index c3c03a3..c53317e 100644 --- a/ThuInfoWeb/Views/Home/Announce.cshtml +++ b/ThuInfoWeb/Views/Home/Announce.cshtml @@ -3,66 +3,80 @@ ViewData["Title"] = "Announce Page"; }
-
-
- 发布公告 -
-
- - -
-
- - -
- + +

+ 发布公告 +

+
+ + +
+
+ + +
+
+
+ + +
+
+ + +
+
+
- - - - - - - - - - - +
+

+ 管理公告 +

+
ID标题内容生效版本作者创建时间是否生效操作
+ + + + + + + + + + - - - @foreach(var item in Model) - { - - - - - - - - - - - } - + + + @foreach (var item in Model) + { + + + + + + + + + + + } +
ID标题内容生效版本作者创建时间是否生效操作
@item.Id@item.Title@item.Content - <= - @item.VisibleNotAfter - @if (item.VisibleExact != "") - { - , - @item.VisibleExact - } - @item.Author@item.CreatedTime@item.IsActive - 删除 - 更改状态 -
@item.Id@item.Title@item.Content + <= + @item.VisibleNotAfter + @if (item.VisibleExact != "") + { + ; + @item.VisibleExact + } + @item.Author@item.CreatedTime@item.IsActive + 删除 + 更改状态 +
@if ((int)ViewData["page"] != 1) { - 上一页 + 上一页 } - 下一页 + 下一页
+ \ No newline at end of file