diff --git a/sharp/content.Tests/endpoints/EndpointsTest.cs b/sharp/content.Tests/endpoints/EndpointsTest.cs index a7b323da..2e62f33a 100644 --- a/sharp/content.Tests/endpoints/EndpointsTest.cs +++ b/sharp/content.Tests/endpoints/EndpointsTest.cs @@ -234,4 +234,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 67cb0573..6d3bfbe4 100644 --- a/sharp/content/endpoints/Endpoints.cs +++ b/sharp/content/endpoints/Endpoints.cs @@ -34,15 +34,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( @@ -100,4 +109,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 3af131c0..6e38dd1a 100644 --- a/sharp/content/repository/Content.cs +++ b/sharp/content/repository/Content.cs @@ -106,7 +106,7 @@ public async Task> FindByUserId(long userId, long page, int await using var connection = await dataSource.OpenConnectionAsync(); var videos = await connection.QueryAsync