Skip to content

Adding Get Activities By Date Range#74

Merged
willvelida merged 2 commits intomainfrom
feature/activity.api/date_range
Aug 19, 2025
Merged

Adding Get Activities By Date Range#74
willvelida merged 2 commits intomainfrom
feature/activity.api/date_range

Conversation

@willvelida
Copy link
Owner

This pull request adds comprehensive support for retrieving activity documents within a given date range, including pagination and robust validation, and thoroughly tests this new functionality. The changes cover endpoint registration, handler logic, repository querying, and extensive unit tests to ensure correct behavior and error handling.

New Feature: Date Range Activity Retrieval

  • Added the GetActivitiesByDateRange endpoint to EndpointRouteBuilderExtensions, allowing clients to fetch activity documents for a specified date range, with optional pagination parameters.
  • Implemented the GetActivitiesByDateRange handler in ActivityHandlers, including validation for date formats and date range logic, and default pagination support.

Unit Tests: Endpoint and Handler

  • Added multiple tests to ActivityHandlersShould to verify correct responses for valid and invalid date ranges, pagination defaults and overrides, and edge cases such as same-day ranges and invalid dates.

Unit Tests: Repository Layer

  • Added comprehensive tests to CosmosRepositoryShould to verify correct query construction, partition key usage, pagination metadata, logging, error handling, and behavior for empty or single-day results.

These changes ensure that the date range retrieval feature is robust, well-validated, and thoroughly tested across all layers of the application.

Signed-off-by: Will Velida <willvelida@hotmail.co.uk>
@willvelida willvelida requested a review from Copilot August 19, 2025 04:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds comprehensive date range querying functionality to the Biotrackr Activity API, enabling clients to retrieve activity documents within specified date ranges with full pagination support and robust validation.

  • Implements a new REST endpoint /range/{startDate}/{endDate} with optional pagination query parameters
  • Adds complete validation for date formats and logical date range constraints in the handler layer
  • Extends the repository layer with efficient Cosmos DB querying using proper partitioning and count operations

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
ICosmosRepository.cs Adds interface method signature for date range querying with pagination
CosmosRepository.cs Implements date range query logic with separate count and data retrieval operations
EndpointRouteBuilderExtensions.cs Registers new REST endpoint with OpenAPI documentation
ActivityHandlers.cs Implements request handler with date validation and pagination defaults
CosmosRepositoryShould.cs Comprehensive unit tests covering repository functionality, edge cases, and error scenarios
ActivityHandlersShould.cs Unit tests for handler validation logic, pagination behavior, and error cases

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

{
try
{
QueryDefinition queryDefinition = new QueryDefinition("SELECT VALUE COUNT(1) FROM c WHERE c.date >= @startDate AND c.date <= @endDate")
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The count query is missing the document type filter that's present in the main query. This could return incorrect counts if the container has other document types. Add AND c.documentType = 'Activity' to match the filtering logic in the main query.

Suggested change
QueryDefinition queryDefinition = new QueryDefinition("SELECT VALUE COUNT(1) FROM c WHERE c.date >= @startDate AND c.date <= @endDate")
QueryDefinition queryDefinition = new QueryDefinition("SELECT VALUE COUNT(1) FROM c WHERE c.date >= @startDate AND c.date <= @endDate AND c.documentType = 'Activity'")

Copilot uses AI. Check for mistakes.

// Get paginated results
QueryDefinition queryDefinition = new QueryDefinition(
"SELECT * FROM c WHERE c.documentType = 'Activity' AND c.date >= @startDate AND c.date <= @endDate ORDER BY c.date ASC OFFSET @offset LIMIT @limit")
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Using SELECT * may retrieve unnecessary data and impact performance. Consider selecting only the required fields if the ActivityDocument contains large properties that aren't always needed.

Suggested change
"SELECT * FROM c WHERE c.documentType = 'Activity' AND c.date >= @startDate AND c.date <= @endDate ORDER BY c.date ASC OFFSET @offset LIMIT @limit")
"SELECT c.id, c.date, c.documentType FROM c WHERE c.documentType = 'Activity' AND c.date >= @startDate AND c.date <= @endDate ORDER BY c.date ASC OFFSET @offset LIMIT @limit")

Copilot uses AI. Check for mistakes.
while (iterator.HasMoreResults)
{
var response = await iterator.ReadNextAsync();
results.AddRange(response.ToList());
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iterator loop pattern may return more results than the LIMIT clause specifies due to how Cosmos DB pagination works. Consider breaking the loop once the desired page size is reached to avoid processing extra data.

Suggested change
results.AddRange(response.ToList());
while (iterator.HasMoreResults && results.Count < paginationRequest.PageSize)
{
var response = await iterator.ReadNextAsync();
var batch = response.ToList();
int remaining = paginationRequest.PageSize - results.Count;
if (batch.Count > remaining)
{
results.AddRange(batch.Take(remaining));
break;
}
else
{
results.AddRange(batch);
}

Copilot uses AI. Check for mistakes.
Signed-off-by: Will Velida <willvelida@hotmail.co.uk>
@willvelida willvelida merged commit 1515da8 into main Aug 19, 2025
9 checks passed
@willvelida willvelida deleted the feature/activity.api/date_range branch August 19, 2025 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants