فهرست منبع

Configurable page size

Lukas Angerer 3 سال پیش
والد
کامیت
411cc16d11

+ 9 - 0
src/RunnersMeet.Server/ApiSettings.cs

@@ -0,0 +1,9 @@
+namespace RunnersMeet.Server;
+
+public class ApiSettings
+{
+	public const string SectionName = "ApiSettings";
+
+	public long GpxFileSizeLimit { get; set; } = 2 << 16; // 64KB
+	public int PageSize { get; set; } = 10;
+}

+ 2 - 1
src/RunnersMeet.Server/AppServer.cs

@@ -82,7 +82,8 @@ public class AppServer
 
 		services.AddSingleton<GpxParser>();
 
-		services.Configure<PersistenceOptions>(config.GetSection(PersistenceOptions.Persistence));
+		services.Configure<PersistenceOptions>(config.GetSection(PersistenceOptions.SectionName));
 		services.AddTransient<IPersistenceOptions, PersistenceOptionsAccessor>();
+		services.Configure<ApiSettings>(config.GetSection(ApiSettings.SectionName));
 	}
 }

+ 7 - 9
src/RunnersMeet.Server/Controllers/TracksController.cs

@@ -2,6 +2,7 @@ using System.Security.Claims;
 using LiteDB;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Options;
 using RunnersMeet.Server.Domain;
 using RunnersMeet.Server.GpxFormat;
 using RunnersMeet.Server.Persistence;
@@ -13,19 +14,17 @@ namespace RunnersMeet.Server.Controllers;
 [Authorize("Tracks")]
 public class TracksController : ControllerBase
 {
-	private const int PageSize = 3;
-
 	private readonly IFileStorage _fileStorage;
 	private readonly GpxParser _gpxParser;
 	private readonly QueryFactory _queryFactory;
-	private readonly IConfiguration _config;
+	private readonly ApiSettings _settings;
 
-	public TracksController(IFileStorage fileStorage, GpxParser gpxParser, QueryFactory queryFactory, IConfiguration config)
+	public TracksController(IFileStorage fileStorage, GpxParser gpxParser, QueryFactory queryFactory, IOptions<ApiSettings> apiOptions)
 	{
 		_fileStorage = fileStorage;
 		_gpxParser = gpxParser;
 		_queryFactory = queryFactory;
-		_config = config;
+		_settings = apiOptions.Value;
 	}
 
 	[HttpGet]
@@ -46,7 +45,7 @@ public class TracksController : ControllerBase
 			query.FilterByName(filter);
 		}
 
-		query.Paging((page ?? 0) * PageSize, PageSize);
+		query.Paging((page ?? 0) * _settings.PageSize, _settings.PageSize);
 
 		return Ok(query.Get());
 	}
@@ -54,10 +53,9 @@ public class TracksController : ControllerBase
 	[HttpPost]
 	public async Task<ActionResult<Track>> CreateTrack([FromForm] IFormFile file, CancellationToken cancellationToken = default)
 	{
-		var limit = _config.GetValue<long>("GpxFileSizeLimit");
-		if (file.Length > limit)
+		if (file.Length > _settings.GpxFileSizeLimit)
 		{
-			throw new ApiException($"Uploaded tracks cannot be larger than {limit / (1024 * 1024)}MB");
+			throw new ApiException($"Uploaded tracks cannot be larger than {_settings.GpxFileSizeLimit / (1024 * 1024)}MB");
 		}
 
 		var fileName = await _fileStorage.UploadFileAsync(file, cancellationToken);

+ 1 - 1
src/RunnersMeet.Server/Persistence/ITracksQuery.cs

@@ -4,5 +4,5 @@ namespace RunnersMeet.Server.Persistence;
 
 public interface ITracksQuery
 {
-	IEnumerable<Track> Get();
+	IList<Track> Get();
 }

+ 2 - 2
src/RunnersMeet.Server/Persistence/TracksQuery.cs

@@ -32,8 +32,8 @@ public class TracksQuery : ITracksQuery
 		return this;
 	}
 
-	public IEnumerable<Track> Get()
+	public IList<Track> Get()
 	{
-		return _query.ToEnumerable();
+		return _query.ToList();
 	}
 }

+ 1 - 1
src/RunnersMeet.Server/PersistenceOptions.cs

@@ -10,7 +10,7 @@ public interface IPersistenceOptions
 
 public class PersistenceOptions : IPersistenceOptions
 {
-	public const string Persistence = "Persistence";
+	public const string SectionName = "Persistence";
 
 	public string DataFilePath { get; set; } = String.Empty;
 	public string FileStorageRootPath { get; set; } = String.Empty;

+ 4 - 1
src/RunnersMeet.Server/appsettings.json

@@ -17,5 +17,8 @@
 		"DataFilePath": "./data/tracks.db",
 		"FileStorageRootPath": "./data/files"
 	},
-	"GpxFileSizeLimit": 2097152
+	"ApiSettings": {
+		"GpxFileSizeLimit": 2097152,
+		"PageSize": 50
+	}
 }