User Search API (#680)

* Added User Search API

* Applied limit to user search

* Update ProjectLighthouse.Servers.API/Controllers/UserEndpoints.cs

Co-authored-by: Josh <josh@slendy.pw>

* Update ProjectLighthouse.Servers.API/Controllers/UserEndpoints.cs

Co-authored-by: Josh <josh@slendy.pw>

* Update ProjectLighthouse.Servers.API/Controllers/UserEndpoints.cs

Co-authored-by: Josh <josh@slendy.pw>

* 1 line fix, woo!

---------

Co-authored-by: Josh <josh@slendy.pw>
This commit is contained in:
Zaprit 2023-02-20 04:37:37 +00:00 committed by GitHub
parent 8030301739
commit e483d325ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -50,6 +50,29 @@ public class UserEndpoints : ApiEndpointController
return this.Ok(user);
}
/// <summary>
/// Searches for the user based on the query
/// </summary>
/// <param name="query">The search query</param>
/// <returns>A list of users</returns>
/// <response code="200">The list of users, if any were found</response>
/// <response code="404">No users matched the query</response>
[HttpGet("search/user")]
[ProducesResponseType(typeof(User), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> SearchUsers(string query)
{
List<User> users = await this.database.Users
.Where(u => u.PermissionLevel != PermissionLevel.Banned && u.Username.Contains(query))
.Where(u => u.ProfileVisibility == PrivacyType.All) // TODO: change check for when user is logged in
.OrderByDescending(b => b.UserId)
.Take(20)
.ToListAsync();
if (!users.Any()) return this.NotFound();
return this.Ok(users);
}
/// <summary>
/// Gets a user and their information from the database.
/// </summary>