Skip to content

Commit

Permalink
Added fields to control when an announcement should be shown
Browse files Browse the repository at this point in the history
  • Loading branch information
84634E1A607A committed Jul 20, 2024
1 parent d4be306 commit 08d21ec
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
The web server application of [THUInfo](https://github.com/UNIDY2002/THUInfo)
# Build Instruction
## Step 1
Install dotnet sdk 6.0.
Install dotnet sdk 8.0.
If you are running RHEL or CentOS, just use
```
$ sudo dnf install dotnet-sdk-6.0
$ sudo dnf install dotnet-sdk-8.0
```
Others should follow this [installation instruction](https://docs.microsoft.com/zh-cn/dotnet/core/install/linux)
## Step 2
Expand Down
70 changes: 38 additions & 32 deletions ThuInfoWeb/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class HomeController : Controller

public HomeController(ILogger<HomeController> logger, Data data, UserManager userManager, VersionManager versionManager)
{
this._logger = logger;
this._data = data;
this._userManager = userManager;
this._versionManager = versionManager;
_logger = logger;
_data = data;
_userManager = userManager;
_versionManager = versionManager;
}
public IActionResult Register()
{
Expand All @@ -33,29 +33,29 @@ public async Task<IActionResult> Register(RegisterViewModel vm)
ModelState.AddModelError(nameof(vm.Name), "禁止注册新用户");
return View(vm);

if (await _data.CheckUserAsync(vm.Name))
{
ModelState.AddModelError(nameof(vm.Name), "用户名已被注册");
return View(vm);
}
var user = new User()
{
Name = vm.Name,
PasswordHash = vm.Password.ToSHA256Hex(),
CreatedTime = DateTime.Now,
IsAdmin = false
};
var result = await _data.CreateUserAsync(user);
if (result == 1)
{
await _userManager.DoLoginAsync(vm.Name, false);
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError(nameof(vm.Name), "发生未知错误");
return View(vm);
}
// if (await _data.CheckUserAsync(vm.Name))
// {
// ModelState.AddModelError(nameof(vm.Name), "用户名已被注册");
// return View(vm);
// }
// var user = new User()
// {
// Name = vm.Name,
// PasswordHash = vm.Password.ToSHA256Hex(),
// CreatedTime = DateTime.Now,
// IsAdmin = false
// };
// var result = await _data.CreateUserAsync(user);
// if (result == 1)
// {
// await _userManager.DoLoginAsync(vm.Name, false);
// return RedirectToAction("Index");
// }
// else
// {
// ModelState.AddModelError(nameof(vm.Name), "发生未知错误");
// return View(vm);
// }
}
public IActionResult Login()
{
Expand Down Expand Up @@ -133,22 +133,28 @@ public async Task<IActionResult> Announce([FromQuery] int page = 1)
Title = a.Title,
Author = a.Author,
CreatedTime = a.CreatedTime,
IsActive = a.IsActive
IsActive = a.IsActive,
VisibleNotAfter = a.VisibleNotAfter,
VisibleExact = a.VisibleExact
}).ToList());
}

[HttpPost, Authorize(Roles = "admin")]
public async Task<IActionResult> CreateAnnounce(AnnounceViewModel vm)
{
if (vm.Title is null || vm.Content is null) return BadRequest("标题或内容为空");
var user = HttpContext.User.Identity.Name;
var a = new Announce()
vm.VisibleNotAfter ??= "9.9.9";
vm.VisibleExact ??= "";
var user = HttpContext.User.Identity?.Name;
var a = new Announce
{
Title = vm.Title,
Content = vm.Content,
Author = user,
Author = user ?? "",
CreatedTime = DateTime.Now,
IsActive = vm.IsActive
IsActive = vm.IsActive,
VisibleNotAfter = vm.VisibleNotAfter,
VisibleExact = vm.VisibleExact
};
var result = await _data.CreateAnnounceAsync(a);
if (result != 1) return BadRequest(ModelState);
Expand Down
13 changes: 12 additions & 1 deletion ThuInfoWeb/DBModels/Announce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ public class Announce
{
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }

public string Title { get; set; }

[Column(StringLength = -1)]
public string Content { get; set; }

public string Author { get; set; }

public DateTime CreatedTime { get; set; }

[JsonIgnore]
public bool IsActive { get; set; }

[Column(StringLength = 10, IsNullable = false)]
public string VisibleNotAfter { get; set; }

[Column(StringLength = 30, IsNullable = false)]
public string VisibleExact { get; set; }
}
}
}
17 changes: 14 additions & 3 deletions ThuInfoWeb/Models/AnnounceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ namespace ThuInfoWeb.Models
public class AnnounceViewModel
{
[Required, Display(Name = "标题")]
public string Title { get; set; }
public string? Title { get; set; }

[Required, Display(Name = "内容")]
public string Content { get; set; }
public string? Content { get; set; }

public int Id { get; set; }
public string Author { get; set; }

public string? Author { get; set; }

public DateTime CreatedTime { get; set; }

public bool IsActive { get; set; } = true;

[Display(Name = "对此版本及之前版本可见")]
public string? VisibleNotAfter { get; set; }

[Display(Name = "对这些版本可见 (使用逗号分隔)")]
public string? VisibleExact { get; set; }
}
}
10 changes: 10 additions & 0 deletions ThuInfoWeb/Views/Home/Announce.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<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>
Expand All @@ -36,6 +37,15 @@
<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>
Expand Down

0 comments on commit 08d21ec

Please sign in to comment.