This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat: get top 20 popular icons from backend #9819
Merged
eddiejaoude
merged 1 commit into
EddieHubCommunity:fix-pr-9819
from
bikrantjajware:feat/popular-icons
Nov 27, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logger from "@config/logger"; | ||
import connectMongo from "@config/mongo"; | ||
import Link from "@models/Link"; | ||
|
||
export default async function handler(req, res) { | ||
if (req.method != "GET") { | ||
return res | ||
.status(400) | ||
.json({ error: "Invalid request: GET request required" }); | ||
} | ||
|
||
const icons = await getPopularIcons(); | ||
return res.status(200).json(icons); | ||
} | ||
|
||
export async function getPopularIcons() { | ||
await connectMongo(); | ||
let icons = []; | ||
try { | ||
icons = await Link.aggregate([ | ||
{ | ||
$group: { | ||
_id: "$icon", | ||
count: { $count: {} }, | ||
}, | ||
}, | ||
{ | ||
$sort:{ | ||
count: -1 | ||
} | ||
}, | ||
{ | ||
$limit: 20 | ||
}, | ||
{ | ||
$project: { _id: 0, icon: "$_id"}, | ||
} | ||
]).exec(); | ||
icons = icons.map( icon => icon.icon ) | ||
} catch (e) { | ||
logger.error(e, "Failed to load popular icons"); | ||
icons = []; | ||
} | ||
|
||
return JSON.parse(JSON.stringify(icons)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the formatting of this code is not inline with the project, check you have Prettier VScode plugin installed, it will fix this on save