Improved paged endpoint rage validation. Fixed smaller request model validation issues

This commit is contained in:
2025-08-26 01:52:43 +02:00
parent 5e371edf2b
commit 51aeb67ad6
6 changed files with 33 additions and 28 deletions

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Extended.Models;
using MoonCore.Models;
using Moonlight.ApiServer.Database.Entities;
using Moonlight.ApiServer.Services;
@@ -27,18 +28,15 @@ public class ApiKeysController : Controller
[HttpGet]
[Authorize(Policy = "permissions:admin.apikeys.get")]
public async Task<IPagedData<ApiKeyResponse>> Get(
[FromQuery] [Range(0, int.MaxValue)] int page,
[FromQuery] [Range(1, 100)] int pageSize
)
public async Task<IPagedData<ApiKeyResponse>> Get([FromQuery] PagedOptions options)
{
var count = await ApiKeyRepository.Get().CountAsync();
var apiKeys = await ApiKeyRepository
.Get()
.OrderBy(x => x.Id)
.Skip(page * pageSize)
.Take(pageSize)
.Skip(options.Page * options.PageSize)
.Take(options.PageSize)
.ToArrayAsync();
var mappedApiKey = apiKeys
@@ -53,11 +51,11 @@ public class ApiKeysController : Controller
return new PagedData<ApiKeyResponse>()
{
CurrentPage = page,
CurrentPage = options.Page,
Items = mappedApiKey,
PageSize = pageSize,
PageSize = options.PageSize,
TotalItems = count,
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
TotalPages = count == 0 ? 0 : (count - 1) / options.PageSize
};
}

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Extended.Models;
using MoonCore.Models;
using Moonlight.ApiServer.Database.Entities;
using Moonlight.ApiServer.Mappers;
@@ -25,17 +26,14 @@ public class ThemesController : Controller
[HttpGet]
[Authorize(Policy = "permissions:admin.system.customisation.themes.read")]
public async Task<PagedData<ThemeResponse>> Get(
[FromQuery] [Range(0, int.MaxValue)] int page,
[FromQuery] [Range(1, 100)] int pageSize
)
public async Task<PagedData<ThemeResponse>> Get([FromQuery] PagedOptions options)
{
var count = await ThemeRepository.Get().CountAsync();
var items = await ThemeRepository
.Get()
.Skip(page * pageSize)
.Take(pageSize)
.Skip(options.Page * options.PageSize)
.Take(options.PageSize)
.ToArrayAsync();
var mappedItems = items
@@ -44,11 +42,11 @@ public class ThemesController : Controller
return new PagedData<ThemeResponse>()
{
CurrentPage = page,
CurrentPage = options.Page,
Items = mappedItems,
PageSize = pageSize,
PageSize = options.PageSize,
TotalItems = count,
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
TotalPages = count == 0 ? 0 : (count - 1) / options.PageSize
};
}

View File

@@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Extended.Helpers;
using MoonCore.Extended.Models;
using MoonCore.Models;
using Moonlight.ApiServer.Database.Entities;
using Moonlight.ApiServer.Services;
@@ -27,18 +28,15 @@ public class UsersController : Controller
[HttpGet]
[Authorize(Policy = "permissions:admin.users.get")]
public async Task<IPagedData<UserResponse>> Get(
[FromQuery] [Range(0, int.MaxValue)] int page,
[FromQuery] [Range(1, 100)] int pageSize
)
public async Task<IPagedData<UserResponse>> Get([FromQuery] PagedOptions options)
{
var count = await UserRepository.Get().CountAsync();
var users = await UserRepository
.Get()
.OrderBy(x => x.Id)
.Skip(page * pageSize)
.Take(pageSize)
.Skip(options.Page * options.PageSize)
.Take(options.PageSize)
.ToArrayAsync();
var mappedUsers = users
@@ -53,11 +51,11 @@ public class UsersController : Controller
return new PagedData<UserResponse>()
{
CurrentPage = page,
CurrentPage = options.Page,
Items = mappedUsers,
PageSize = pageSize,
PageSize = options.PageSize,
TotalItems = count,
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
TotalPages = count == 0 ? 0 : (count - 1) / options.PageSize
};
}

View File

@@ -1,8 +1,15 @@
namespace Moonlight.Shared.Http.Requests.Admin.Sys.Files;
using System.ComponentModel.DataAnnotations;
namespace Moonlight.Shared.Http.Requests.Admin.Sys.Files;
public class DecompressRequest
{
[Required(ErrorMessage = "You need to provide a format")]
public string Format { get; set; }
[Required(ErrorMessage = "You need to provide a path")]
public string Path { get; set; }
[Required(ErrorMessage = "You need to provide a destination")]
public string Destination { get; set; }
}

View File

@@ -1,6 +1,9 @@
using System.ComponentModel.DataAnnotations;
namespace Moonlight.Shared.Http.Requests.Admin.Sys;
public class GenerateDiagnoseRequest
{
[Required(ErrorMessage = "You need to define providers")]
public string[] Providers { get; set; } = [];
}

View File

@@ -17,5 +17,6 @@ public class CreateUserRequest
[MaxLength(256, ErrorMessage = "Your password should not exceed the length of 256 characters")]
public string Password { get; set; }
[Required(ErrorMessage = "You need to provide permissions")]
public string[] Permissions { get; set; } = [];
}