Skip to content

Commit

Permalink
feat: enhance searching and use string.Contains
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Mar 31, 2024
1 parent 37d9cb9 commit 43d2154
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
18 changes: 11 additions & 7 deletions src/GZCTF/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,21 @@ public async Task<IActionResult> AddUsers([FromBody] UserCreateModel[] model, Ca
/// <response code="403">禁止访问</response>
[HttpPost("Users/Search")]
[ProducesResponseType(typeof(ArrayResponse<UserInfoModel>), StatusCodes.Status200OK)]
public async Task<IActionResult> SearchUsers([FromQuery] string hint, CancellationToken token = default) =>
Ok((await userManager.Users.Where(item =>
EF.Functions.Like(item.UserName!, $"%{hint}%") ||
EF.Functions.Like(item.StdNumber, $"%{hint}%") ||
EF.Functions.Like(item.Email!, $"%{hint}%") ||
EF.Functions.Like(item.Id.ToString(), $"%{hint}%") ||
EF.Functions.Like(item.RealName, $"%{hint}%")
public async Task<IActionResult> SearchUsers([FromQuery] string hint, CancellationToken token = default)
{
var loweredHint = hint.ToLower();
return Ok((await userManager.Users.Where(item =>
item.UserName!.ToLower().Contains(loweredHint) ||
item.StdNumber.ToLower().Contains(loweredHint) ||
item.Email!.ToLower().Contains(loweredHint) ||
item.PhoneNumber!.ToLower().Contains(loweredHint) ||
item.Id.ToString().ToLower().Contains(loweredHint) ||
item.RealName.ToLower().Contains(loweredHint)
)
.OrderBy(e => e.Id).Take(30).ToArrayAsync(token))
.Select(UserInfoModel.FromUserInfo)
.ToResponse());
}

/// <summary>
/// 获取全部队伍信息
Expand Down
12 changes: 9 additions & 3 deletions src/GZCTF/Repositories/TeamRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ public Task DeleteTeam(Team team, CancellationToken token = default)
Context.Teams.Where(t => t.Members.Any(u => u.Id == user.Id))
.Include(t => t.Members).ToArrayAsync(token);

public Task<Team[]> SearchTeams(string hint, CancellationToken token = default) =>
Context.Teams.Include(t => t.Members).Where(item => EF.Functions.Like(item.Name, $"%{hint}%"))
.OrderBy(t => t.Id).Take(30).ToArrayAsync(token);
public Task<Team[]> SearchTeams(string hint, CancellationToken token = default)
{
var loweredHint = hint.ToLower();
var query = int.TryParse(hint, out int id)
? Context.Teams.Include(t => t.Members).Where(item => item.Name.ToLower().Contains(loweredHint) || item.Id == id)
: Context.Teams.Include(t => t.Members).Where(item => item.Name.ToLower().Contains(loweredHint));

return query.OrderBy(t => t.Id).Take(30).ToArrayAsync(token);
}

public Task Transfer(Team team, UserInfo user, CancellationToken token = default)
{
Expand Down

0 comments on commit 43d2154

Please sign in to comment.