mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: add pagination to user retrieval endpoints; enhance BuildUserResponse to include pagination details
This commit is contained in:
@@ -15,32 +15,32 @@ namespace SharpbinV3.Server.Controllers
|
||||
private readonly AppDbContext _db = db;
|
||||
|
||||
[HttpGet("{username}")]
|
||||
public async Task<IActionResult> GetByUsername(string username)
|
||||
public async Task<IActionResult> GetByUsername(string username, [FromQuery] int page = 1)
|
||||
{
|
||||
var user = await _userService.GetByUsername(username, withPastes: true);
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user));
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user, page));
|
||||
}
|
||||
|
||||
[HttpGet("uuid/{uuid}")]
|
||||
public async Task<IActionResult> GetByUUID(Guid uuid)
|
||||
public async Task<IActionResult> GetByUUID(Guid uuid, [FromQuery] int page = 1)
|
||||
{
|
||||
var user = await _userService.GetByUUID(uuid, withPastes: true);
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user));
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user, page));
|
||||
}
|
||||
|
||||
[HttpGet("me")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetMe()
|
||||
public async Task<IActionResult> GetMe([FromQuery] int page = 1)
|
||||
{
|
||||
var userUUID = HttpContext.User?.FindFirst("UUID")?.Value;
|
||||
if (userUUID is null)
|
||||
return Unauthorized(new { message = "Invalid token." });
|
||||
|
||||
|
||||
var user = await _userService.GetByUUID(Guid.Parse(userUUID), withPastes: true);
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user));
|
||||
return user == null ? NotFound() : Ok(await BuildUserResponse(user, page));
|
||||
}
|
||||
|
||||
private async Task<object> BuildUserResponse(User user)
|
||||
private async Task<object> BuildUserResponse(User user, int page = 1)
|
||||
{
|
||||
var isAuthenticatedUser = HttpContext.User?.FindFirst("UUID")?.Value == user.UUID.ToString();
|
||||
var pasteQuery = _db.Pastes.AsNoTracking().Where(p => p.AuthorUUID == user.UUID);
|
||||
@@ -52,22 +52,46 @@ namespace SharpbinV3.Server.Controllers
|
||||
pasteQuery = pasteQuery.Where(p => p.Visibility == 0);
|
||||
}
|
||||
|
||||
var pastes = await pasteQuery.Select(p => new {
|
||||
p.ID,
|
||||
p.UUID,
|
||||
p.Title,
|
||||
p.Syntax,
|
||||
p.Size,
|
||||
p.TrueSize,
|
||||
p.Views,
|
||||
p.Visibility,
|
||||
p.EditedAt,
|
||||
p.ExpiresAt
|
||||
}).ToListAsync();
|
||||
if (page < 1)
|
||||
page = 1;
|
||||
const int pageSize = 50;
|
||||
var totalCount = await pasteQuery.CountAsync();
|
||||
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
||||
var skip = (page - 1) * pageSize;
|
||||
|
||||
var pastes = await pasteQuery
|
||||
.OrderByDescending(p => p.UUID)
|
||||
.ThenByDescending(p => p.ID)
|
||||
.Skip(skip)
|
||||
.Take(pageSize)
|
||||
.Select(p => new
|
||||
{
|
||||
p.ID,
|
||||
p.UUID,
|
||||
p.Title,
|
||||
p.Syntax,
|
||||
p.Size,
|
||||
p.TrueSize,
|
||||
p.Views,
|
||||
p.Visibility,
|
||||
p.EditedAt,
|
||||
p.ExpiresAt
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
var pagination = new
|
||||
{
|
||||
Page = page,
|
||||
PageSize = pageSize,
|
||||
TotalCount = totalCount,
|
||||
TotalPages = totalPages
|
||||
};
|
||||
|
||||
if (isAuthenticatedUser)
|
||||
{
|
||||
return new {
|
||||
return new
|
||||
{
|
||||
user.UID,
|
||||
user.Username,
|
||||
user.UUID,
|
||||
@@ -76,17 +100,20 @@ namespace SharpbinV3.Server.Controllers
|
||||
user.LastLogin,
|
||||
user.Roles,
|
||||
user.Visibility,
|
||||
Pastes = pastes
|
||||
Pastes = pastes,
|
||||
Pagination = pagination
|
||||
};
|
||||
}
|
||||
|
||||
return new {
|
||||
return new
|
||||
{
|
||||
user.UID,
|
||||
user.Username,
|
||||
user.UUID,
|
||||
user.DisplayName,
|
||||
user.Roles,
|
||||
Pastes = pastes
|
||||
Pastes = pastes,
|
||||
Pagination = pagination
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user