forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchController.ts
More file actions
159 lines (139 loc) · 3.51 KB
/
Copy pathsearchController.ts
File metadata and controls
159 lines (139 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import Prompt from "../models/Prompt";
interface SearchFilters {
query?: string;
category?: string;
minPrice?: number;
maxPrice?: number;
sortBy?: "recent" | "price-low" | "price-high" | "sales" | "rating";
page?: number;
limit?: number;
}
interface SearchResponse {
prompts: any[];
total: number;
page: number;
totalPages: number;
hasMore: boolean;
}
/**
* Search prompts with advanced filtering and pagination
*/
export async function searchPrompts(filters: SearchFilters): Promise<SearchResponse> {
const {
query = "",
category,
minPrice = 0,
maxPrice = 1000000,
sortBy = "recent",
page = 1,
limit = 20,
} = filters;
// Build the base query
const baseQuery: any = {
isActive: true,
listingStatus: "published",
price: { $gte: minPrice, $lte: maxPrice },
};
// Add category filter if specified
if (category && category !== "") {
baseQuery.category = category;
}
// Add text search if query is provided
let searchQuery = Prompt.find(baseQuery);
if (query && query.trim() !== "") {
const searchRegex = new RegExp(query.trim(), "i");
searchQuery = searchQuery.or([
{ title: searchRegex },
{ content: searchRegex },
{ category: searchRegex },
]);
}
// Get total count for pagination
const total = await Prompt.countDocuments(searchQuery.getFilter());
// Apply sorting
let sortOptions: any;
switch (sortBy) {
case "price-low":
sortOptions = { price: 1 };
break;
case "price-high":
sortOptions = { price: -1 };
break;
case "sales":
sortOptions = { salesCount: -1 };
break;
case "rating":
sortOptions = { rating: -1 };
break;
case "recent":
default:
sortOptions = { createdAt: -1 };
break;
}
// Execute query with pagination
const prompts = await searchQuery
.sort(sortOptions)
.skip((page - 1) * limit)
.limit(limit)
.populate("owner", "walletAddress username rating")
.lean();
const totalPages = Math.ceil(total / limit);
const hasMore = page < totalPages;
return {
prompts,
total,
page,
totalPages,
hasMore,
};
}
/**
* Get search suggestions based on query
*/
export async function getSearchSuggestions(query: string, limit: number = 5) {
if (!query || query.trim().length < 2) {
return { titles: [], categories: [] };
}
const searchRegex = new RegExp(query.trim(), "i");
const [titles, categories] = await Promise.all([
Prompt.find({ title: searchRegex, isActive: true })
.select("title")
.limit(limit)
.lean(),
Prompt.distinct("category", { category: searchRegex, isActive: true }).then((cats: string[]) =>
cats.slice(0, limit),
),
]);
return {
titles: titles.map((p: any) => p.title),
categories,
};
}
/**
* Get available categories with counts
*/
export async function getCategoriesWithCounts() {
const categories = await Prompt.aggregate([
{ $match: { isActive: true, listingStatus: "published" } },
{ $group: { _id: "$category", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
]);
return categories.map((cat: any) => ({
name: cat._id,
count: cat.count,
}));
}
/**
* Get featured/top prompts
*/
export async function getFeaturedPrompts(limit: number = 6) {
const prompts = await Prompt.find({
isActive: true,
listingStatus: "published",
})
.sort({ salesCount: -1, rating: -1 })
.limit(limit)
.populate("owner", "walletAddress username rating")
.lean();
return prompts;
}