Skip to content

Add /api/tips/stats endpoint with topic statistics#7

Open
RohitChavan16 wants to merge 1 commit intotimothywarner-org:mainfrom
RohitChavan16:add-tips-stats-endpoint
Open

Add /api/tips/stats endpoint with topic statistics#7
RohitChavan16 wants to merge 1 commit intotimothywarner-org:mainfrom
RohitChavan16:add-tips-stats-endpoint

Conversation

@RohitChavan16
Copy link

@RohitChavan16 RohitChavan16 commented Jan 7, 2026

Description

This PR adds a new GET /api/tips/stats endpoint that returns tip statistics grouped by topic.
Fixes #5

Changes

  • Added a stats service to aggregate tips by topic
  • Included total tip count in the response
  • Ensured all topics are returned, even when count is zero
  • Exposed stats via a new API endpoint

Why

This enables dashboard-friendly analytics without duplicating logic on the client.

Copy link

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 PR adds a new statistics endpoint to the tips API that aggregates tip counts by topic. The implementation reads metadata from the tips data file to ensure all topics are represented in the response, even if they have zero tips.

Key Changes

  • Added getStats service function to compute tip statistics by topic
  • Created readTipsFile helper to access full file structure including metadata
  • Exposed statistics through new GET /api/tips/stats controller and route

Reviewed changes

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

File Description
src/services/tipService.js Implements stats aggregation logic and new file reader helper
src/routes/tips.js Registers /stats endpoint in router configuration
src/controllers/tipController.js Adds controller handler for stats requests

Comment on lines 12 to +13
router.get('/random', tipController.getRandom);
router.get('/stats', tipController.getStats);
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

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

The /stats route is placed after /random but before /topic/:topic. This ordering is correct and prevents /stats from being matched by the /:id route, but the route should be moved before /random to maintain consistency with the pattern of placing static routes before parameterized ones.

Suggested change
router.get('/random', tipController.getRandom);
router.get('/stats', tipController.getStats);
router.get('/stats', tipController.getStats);
router.get('/random', tipController.getRandom);

Copilot uses AI. Check for mistakes.
Comment on lines +144 to +145
const topic = tip.topic?.toLowerCase();
if (topic && byTopic.hasOwnProperty(topic)) {
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

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

The case-insensitive comparison converts tip.topic to lowercase (line 144), but the metadata topics array is not lowercased when initializing byTopic (lines 139-141). This creates a mismatch where tips won't be counted if metadata.topics contains mixed-case values. Either lowercase the metadata topics during initialization or change the counting logic to use the original case.

Suggested change
const topic = tip.topic?.toLowerCase();
if (topic && byTopic.hasOwnProperty(topic)) {
const topic = tip.topic;
if (topic && Object.prototype.hasOwnProperty.call(byTopic, topic)) {

Copilot uses AI. Check for mistakes.
}
};


Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

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

Extra blank line at line 92. Remove one blank line to maintain consistent spacing between functions.

Suggested change

Copilot uses AI. Check for mistakes.
}
}

async function readTipsFile() {
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

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

Missing JSDoc comment for the readTipsFile function. All public and exported functions should have JSDoc documentation describing their purpose, return value, and any parameters.

Copilot generated this review using guidance from repository custom instructions.
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.

Add endpoint to get tips count by topic

1 participant