Skip to content

Commit

Permalink
Added form fields to specify versions (#6)
Browse files Browse the repository at this point in the history
* Added form

* Moved Regex out of DEBUG
  • Loading branch information
84634E1A607A authored Jul 20, 2024
1 parent 777c1f9 commit 16721e0
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 59 deletions.
33 changes: 28 additions & 5 deletions ThuInfoWeb/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -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<HomeController> _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<HomeController> logger, Data data, UserManager userManager, VersionManager versionManager)
{
_logger = logger;
Expand Down Expand Up @@ -143,8 +147,26 @@ public async Task<IActionResult> Announce([FromQuery] int page = 1)
public async Task<IActionResult> 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
{
Expand All @@ -153,8 +175,8 @@ public async Task<IActionResult> 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);
Expand Down Expand Up @@ -252,6 +274,7 @@ public IActionResult Stat()
{
return View();
}

#if DEBUG
[Route("Home/Exception")]
public IActionResult Exception()
Expand Down
122 changes: 68 additions & 54 deletions ThuInfoWeb/Views/Home/Announce.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,80 @@
ViewData["Title"] = "Announce Page";
}
<div class="container">
<form method="post" id="form1">
<div class="text">
发布公告
</div>
<div class="mb-3">
<label for="titleinput" class="form-label">标题</label>
<input type="text" name="title" class="form-control" id="titleinput">
</div>
<div class="mb-3">
<label for="contentinput" class="form-label">内容</label>
<input type="text" name="content" class="form-control" id="contentinput">
</div>
<button type="button" onclick="create()" class="btn btn-primary">发布</button>
<form method="post" id="form1">
<h3>
发布公告
</h3>
<div class="mb-3">
<label for="titleinput" class="form-label">标题</label>
<input type="text" name="title" class="form-control" id="titleinput">
</div>
<div class="mb-3">
<label for="contentinput" class="form-label">内容</label>
<input type="text" name="content" class="form-control" id="contentinput">
</div>
<div class="mb-3 d-flex flex-row gap-3">
<div>
<label for="visiblenotafterinput" class="form-label">在不晚于以下版本生效</label>
<input type="text" name="visiblenotafter" class="form-control" id="visiblenotafterinput">
</div>
<div class="flex-fill">
<label for="visibleexactinput" class="form-label">在以下版本生效 (逗号分隔)</label>
<input type="text" name="visibleexact" class="form-control" id="visibleexactinput">
</div>
</div>
<button type="button" onclick="create()" class="btn btn-primary">发布</button>
</form>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">标题</th>
<th scope="col">内容</th>
<th scope="col">生效版本</th>
<th scope="col">作者</th>
<th scope="col">创建时间</th>
<th scope="col">是否生效</th>
<th scope="col">操作</th>
<hr/>
<h3>
管理公告
</h3>
<table class="table">
<thead>
<tr class="text-center">
<th scope="col">ID</th>
<th scope="col">标题</th>
<th scope="col">内容</th>
<th scope="col">生效版本</th>
<th scope="col" style="min-width: 4rem">作者</th>
<th scope="col">创建时间</th>
<th scope="col">是否生效</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
@foreach(var item in Model)
{
<tr>
<th scope="row">@item.Id</th>
<td>@item.Title</td>
<td>@item.Content</td>
<td>
<span>&lt;=</span>
<span>@item.VisibleNotAfter</span>
@if (item.VisibleExact != "")
{
<span>,</span>
<span>@item.VisibleExact</span>
}
</td>
<td>@item.Author</td>
<td>@item.CreatedTime</td>
<td>@item.IsActive</td>
<td>
<a class="btn btn-link" asp-action="DeleteAnnounce" asp-route-id="@item.Id" asp-route-returnpage="@ViewData["page"]">删除</a>
<a class="btn btn-link" asp-action="ChangeAnnounceStatus" asp-route-id="@item.Id" asp-route-returnpage="@ViewData["page"]">更改状态</a>
</td>
</tr>
}
</tbody>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<th class="text-center" scope="row">@item.Id</th>
<td>@item.Title</td>
<td>@item.Content</td>
<td class="text-center">
<span>&lt;=</span>
<span>@item.VisibleNotAfter</span>
@if (item.VisibleExact != "")
{
<span>;</span>
<span>@item.VisibleExact</span>
}
</td>
<td class="text-center">@item.Author</td>
<td class="text-center">@item.CreatedTime</td>
<td class="text-center">@item.IsActive</td>
<td class="text-center">
<a class="btn btn-link" asp-action="DeleteAnnounce" asp-route-id="@item.Id" asp-route-returnpage="@ViewData["page"]">删除</a>
<a class="btn btn-link" asp-action="ChangeAnnounceStatus" asp-route-id="@item.Id" asp-route-returnpage="@ViewData["page"]">更改状态</a>
</td>
</tr>
}
</tbody>
</table>
<div>
@if ((int)ViewData["page"] != 1)
{
<a class="btn btn-primary" asp-action="Announce" asp-route-page='@((int)ViewData["page"]-1)'>上一页</a>
<a class="btn btn-primary" asp-action="Announce" asp-route-page='@((int)ViewData["page"] - 1)'>上一页</a>
}
<a class="btn-primary btn" asp-action="Announce" asp-route-page='@((int)ViewData["page"]+1)'>下一页</a>
<a class="btn-primary btn" asp-action="Announce" asp-route-page='@((int)ViewData["page"] + 1)'>下一页</a>
</div>
</div>
<script>
Expand All @@ -80,4 +94,4 @@
}
});
}
</script>
</script>

0 comments on commit 16721e0

Please sign in to comment.