Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated SDK & deps; Added fields to control announcement shown version #3

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,6 @@ MigrationBackup/
# Fody - auto-generated XML schema
FodyWeavers.xsd
/ThuInfoWeb/test.db

# Rider .idea
.idea/
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: 5 additions & 5 deletions ThuInfoWeb/ThuInfoWeb.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -11,10 +11,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.682" />
<PackageReference Include="FreeSql.Provider.SqliteCore" Version="3.2.682" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.1.5" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.2" />
<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.833" />
<PackageReference Include="FreeSql.Provider.SqliteCore" Version="3.2.833" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.11" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.8" />
</ItemGroup>

</Project>
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
4 changes: 2 additions & 2 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM bitnami/dotnet-sdk:6 AS build-env
FROM bitnami/dotnet-sdk:8 AS build-env
WORKDIR /build
COPY . ./
RUN dotnet publish -c Release -o out

FROM bitnami/aspnet-core:6
FROM bitnami/aspnet-core:8
WORKDIR /app
COPY --from=build-env /build/out .
ENTRYPOINT [ "dotnet", "ThuInfoWeb.dll"]
Loading