@@ -3,24 +3,48 @@ import { type IPaper } from "@/interface";
33import { escapeRegExp } from "@/lib/utils/regex" ;
44import { extractUniqueValues } from "@/lib/utils/paper-aggregation" ;
55import { connectToDatabase } from "../database/mongoose" ;
6+ import CourseCount from "@/db/course" ;
67
78export async function getPapersBySubject ( subject : string ) {
8- if ( ! subject ) {
9- throw new Error ( "Subject query parameter is required" ) ;
10- }
9+ if ( ! subject ) {
10+ throw new Error ( "Subject query parameter is required" ) ;
11+ }
12+ await connectToDatabase ( ) ;
13+
14+ const escapedSubject = escapeRegExp ( subject ) ;
15+ const papers : IPaper [ ] = await Paper . find ( {
16+ subject : { $regex : new RegExp ( `${ escapedSubject } ` , "i" ) } ,
17+ } ) ;
1118
12- await connectToDatabase ( ) ;
13-
14- const escapedSubject = escapeRegExp ( subject ) ;
15- const papers : IPaper [ ] = await Paper . find ( {
16- subject : { $regex : new RegExp ( `${ escapedSubject } ` , "i" ) } ,
17- } ) ;
19+ const uniqueValues = extractUniqueValues ( papers ) ;
1820
19- const uniqueValues = extractUniqueValues ( papers ) ;
21+ return {
22+ papers,
23+ ...uniqueValues ,
24+ }
2025
21- return {
22- papers,
23- ...uniqueValues ,
24- }
26+ }
2527
28+ export async function getPaperById ( id : string ) {
29+ await connectToDatabase ( ) ;
30+ const paper = await Paper . findById ( id ) ;
31+
32+ if ( ! paper ) {
33+ throw new Error ( "Paper not found" ) ; // 404
34+ }
35+
36+ return paper ;
37+ }
38+
39+ export async function getCourseCounts ( ) {
40+ await connectToDatabase ( ) ;
41+
42+ const count = await CourseCount . find ( ) . lean ( ) ;
43+
44+ const formatted = count . map ( ( item ) => ( {
45+ name : item . name ,
46+ count : item . count ,
47+ } ) ) ;
48+
49+ return formatted ;
2650}
0 commit comments