Skip to content

Commit

Permalink
fix(admin): team page & error message
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Jun 30, 2023
1 parent dde5357 commit c51a373
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
29 changes: 19 additions & 10 deletions src/GZCTF/ClientApp/src/pages/admin/Teams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ScrollArea,
Code,
Badge,
Input,
} from '@mantine/core'
import { useInputState } from '@mantine/hooks'
import { showNotification } from '@mantine/notifications'
Expand Down Expand Up @@ -184,7 +185,7 @@ const Teams: FC = () => {
<Table className={classes.table}>
<thead>
<tr>
<th style={{ width: '20rem' }}>队伍</th>
<th style={{ width: '23rem' }}>队伍</th>
<th>队员</th>
<th>签名</th>
<th />
Expand All @@ -201,20 +202,28 @@ const Teams: FC = () => {
return (
<tr key={team.id}>
<td>
<Group position="apart">
<Group position="apart" spacing={0}>
<Group position="left">
<Avatar alt="avatar" src={team.avatar} radius="xl">
{team.name?.slice(0, 1)}
</Avatar>
<Text lineClamp={1} weight="bold">
{team.name}
</Text>
</Group>
<Group position="right">
<Badge size="sm" color={team.locked ? 'yellow' : 'gray'}>
{team.locked ? '已锁定' : '未锁定'}
</Badge>
<Input
variant="unstyled"
value={team.name ?? 'team'}
readOnly
sx={() => ({
input: {
userSelect: 'none',
fontWeight: 'bold',
width: '14rem',
},
})}
/>
</Group>

<Badge size="sm" color={team.locked ? 'yellow' : 'gray'}>
{team.locked ? '已锁定' : '未锁定'}
</Badge>
</Group>
</td>
<td>
Expand Down
8 changes: 6 additions & 2 deletions src/GZCTF/Models/Request/Admin/AdminTeamModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CTFServer.Models.Request.Admin;
using System.ComponentModel.DataAnnotations;

namespace CTFServer.Models.Request.Admin;

/// <summary>
/// 队伍信息更改(Admin)
Expand All @@ -8,11 +10,13 @@ public class AdminTeamModel
/// <summary>
/// 队伍名称
/// </summary>
public string? Name { get; set; }
[MaxLength(15, ErrorMessage = "队伍名称过长")]
public string? Name { get; set; } = string.Empty;

/// <summary>
/// 队伍签名
/// </summary>
[MaxLength(31, ErrorMessage = "队伍签名过长")]
public string? Bio { get; set; }

/// <summary>
Expand Down
31 changes: 23 additions & 8 deletions src/GZCTF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,7 @@

builder.Services.AddControllersWithViews().ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var errmsg = context.ModelState.Values.FirstOrDefault()?.Errors.FirstOrDefault()?.ErrorMessage;
return new JsonResult(new RequestResponse(errmsg is not null && errmsg.Length > 0 ? errmsg : "校验失败,请检查输入。"))
{
StatusCode = 400
};
};
options.InvalidModelStateResponseFactory = InvalidModelStateHandler;
});

var app = builder.Build();
Expand Down Expand Up @@ -342,4 +335,26 @@ public static void ExitWithFatalMessage(string msg)
Thread.Sleep(30000);
Environment.Exit(1);
}

public static IActionResult InvalidModelStateHandler(ActionContext context)
{
string? errmsg = null;

if (context.ModelState.ErrorCount > 0)
{
foreach (var val in context.ModelState.Values)
{
if (val.Errors.Count > 0)
{
errmsg = val.Errors.FirstOrDefault()?.ErrorMessage;
break;
}
}
}

return new JsonResult(new RequestResponse(errmsg is not null && errmsg.Length > 0 ? errmsg : "校验失败,请检查输入。"))
{
StatusCode = 400
};
}
}

0 comments on commit c51a373

Please sign in to comment.