From 1d04ff040ff96a5b7135e24e99e61fd1bbe8e055 Mon Sep 17 00:00:00 2001 From: sixwaaaay Date: Wed, 10 Jan 2024 23:04:55 +0800 Subject: [PATCH] feat: add index hints --- .../content.Tests/endpoints/EndpointsTest.cs | 47 +++++++++++++++++++ sharp/content/endpoints/Endpoints.cs | 34 +++++++++++--- sharp/content/repository/Content.cs | 4 +- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/sharp/content.Tests/endpoints/EndpointsTest.cs b/sharp/content.Tests/endpoints/EndpointsTest.cs index 7aeff7ac..c805b5e8 100644 --- a/sharp/content.Tests/endpoints/EndpointsTest.cs +++ b/sharp/content.Tests/endpoints/EndpointsTest.cs @@ -221,4 +221,51 @@ public void Validate_WithInvalidVideoUrl_ThrowsArgumentException() Assert.Throws(() => request.Validate()); } + + [Fact] + public void EnsurePageAndSize_WithValidParameters_DoesNotThrowException() + { + // Arrange + long? page = 1; + int? size = 10; + + // Act + var ex = Record.Exception(() => Endpoints.EnsurePageAndSize(page, size)); + + // Assert + Assert.Null(ex); + } + + [Fact] + public void EnsurePageAndSize_WithNegativePage_ThrowsArgumentOutOfRangeException() + { + // Arrange + long? page = -1; + int? size = 10; + + // Act & Assert + Assert.Throws(() => Endpoints.EnsurePageAndSize(page, size)); + } + + [Fact] + public void EnsurePageAndSize_WithNegativeSize_ThrowsArgumentOutOfRangeException() + { + // Arrange + long? page = 1; + int? size = -1; + + // Act & Assert + Assert.Throws(() => Endpoints.EnsurePageAndSize(page, size)); + } + + [Fact] + public void EnsurePageAndSize_WithSizeGreaterThan20_ThrowsArgumentOutOfRangeException() + { + // Arrange + long? page = 1; + int? size = 21; + + // Act & Assert + Assert.Throws(() => Endpoints.EnsurePageAndSize(page, size)); + } } \ No newline at end of file diff --git a/sharp/content/endpoints/Endpoints.cs b/sharp/content/endpoints/Endpoints.cs index 712b18f4..8f488f57 100644 --- a/sharp/content/endpoints/Endpoints.cs +++ b/sharp/content/endpoints/Endpoints.cs @@ -21,15 +21,24 @@ public class VideoRequest public static class Endpoints { - public static Task> UserVideos(IDomainService service, long userId, long? page, int? size) => - service.FindByUserId(userId, page ?? long.MaxValue, size ?? 10); + public static Task> UserVideos(IDomainService service, long userId, long? page, int? size) + { + EnsurePageAndSize(page, size); + return service.FindByUserId(userId, page ?? long.MaxValue, size ?? 10); + } - public static Task> Videos(IDomainService service, long? page, int? size) => - service.FindRecent(page ?? long.MaxValue, size ?? 10); + public static Task> Videos(IDomainService service, long? page, int? size) + { + EnsurePageAndSize(page, size); + return service.FindRecent(page ?? long.MaxValue, size ?? 10); + } - public static Task> Likes(IDomainService service, long userId, long? page, int? size) => - service.VotedVideos(userId, page ?? long.MaxValue, size ?? 10); + public static Task> Likes(IDomainService service, long userId, long? page, int? size) + { + EnsurePageAndSize(page, size); + return service.VotedVideos(userId, page ?? long.MaxValue, size ?? 10); + } public static void Vote(IDomainService service, VoteRequest request) => service.Vote( @@ -87,4 +96,17 @@ public static void Validate(this VideoRequest request) throw new ArgumentException("Video url is null or empty", nameof(request.VideoUrl)); } } + + public static void EnsurePageAndSize(long? page, int? size) + { + if (page is < 0) + { + throw new ArgumentOutOfRangeException(nameof(page)); + } + + if (size is < 0 or > 20) + { + throw new ArgumentOutOfRangeException(nameof(size)); + } + } } \ No newline at end of file diff --git a/sharp/content/repository/Content.cs b/sharp/content/repository/Content.cs index 63693013..b9e15fbf 100644 --- a/sharp/content/repository/Content.cs +++ b/sharp/content/repository/Content.cs @@ -93,7 +93,7 @@ public async Task> FindByUserId(long userId, long page, int await using var connection = await dataSource.OpenConnectionAsync(); var videos = await connection.QueryAsync