|
1 | 1 | import { Predicate } from '@syncfusion/ej2-data'; |
2 | 2 | import { NextResponse, NextRequest } from "next/server"; |
3 | 3 | import { DataManager, Query } from '@syncfusion/ej2-data'; |
4 | | -import { doctorDetails } from '../../../data/health_care_Entities'; |
| 4 | +import { doctorDetails } from '../../../data/health_care_Entities'; |
5 | 5 |
|
6 | 6 | // CORS headers configuration |
7 | 7 | const CORS_HEADERS = { |
8 | | - 'Access-Control-Allow-Origin': '*', |
9 | | - 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
10 | | - 'Access-Control-Allow-Headers': 'Content-Type', |
| 8 | + 'Access-Control-Allow-Origin': '*', |
| 9 | + 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
| 10 | + 'Access-Control-Allow-Headers': 'Content-Type', |
11 | 11 | }; |
12 | 12 |
|
13 | 13 | // Create a method to send responses with CORS headers |
14 | 14 | const corsResponse = (body: any, status = 200) => { |
15 | | - return new NextResponse(JSON.stringify(body), { |
16 | | - status, |
17 | | - headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }, |
18 | | - }); |
| 15 | + return new NextResponse(JSON.stringify(body), { |
| 16 | + status, |
| 17 | + headers: { 'Content-Type': 'application/json', ...CORS_HEADERS }, |
| 18 | + }); |
19 | 19 | }; |
20 | 20 |
|
21 | 21 | // Handle the preflight request for CORS |
22 | 22 | export async function OPTIONS(request: NextRequest) { |
23 | | - return new NextResponse(null, { headers: CORS_HEADERS }); |
| 23 | + return new NextResponse(null, { headers: CORS_HEADERS }); |
24 | 24 | } |
25 | | - |
26 | | -// Helper function: Apply filtering based on predicates |
27 | | -const performFiltering = (filterArray: any[], query: any) => { |
28 | | - const predicateCollection = Array.isArray(filterArray) ? filterArray[0] : filterArray; |
29 | | - |
30 | | - if (!predicateCollection || !Array.isArray(predicateCollection.predicates) || predicateCollection.predicates.length === 0) { |
31 | | - return; |
32 | | - } |
33 | | - |
34 | | - const condition = (predicateCollection.condition || 'and').toLowerCase(); |
35 | | - const ignoreCase = predicateCollection.ignoreCase !== undefined ? !!predicateCollection.ignoreCase : true; |
36 | | - |
37 | | - let combinedPredicate: any = null; |
38 | | - |
39 | | - predicateCollection.predicates.forEach((p: any) => { |
40 | | - if (p.isComplex && Array.isArray(p.predicates)) { |
41 | | - const nested = buildNestedPredicate(p, ignoreCase); |
42 | | - if (nested) { |
43 | | - combinedPredicate = combinedPredicate |
44 | | - ? (condition === 'or' ? combinedPredicate.or(nested) : combinedPredicate.and(nested)) |
45 | | - : nested; |
46 | | - } |
47 | | - return; |
48 | | - } |
49 | | - |
50 | | - const singlePredicate: any = new Predicate(p.field, p.operator, p.value, true); |
51 | | - combinedPredicate = combinedPredicate |
52 | | - ? (condition === 'or' ? combinedPredicate.or(singlePredicate) : combinedPredicate.and(singlePredicate)) |
53 | | - : singlePredicate; |
54 | | - }); |
55 | | - |
56 | | - if (combinedPredicate) { |
57 | | - query.where(combinedPredicate); |
58 | | - } |
| 25 | +// Normalize condition string (default to 'and') |
| 26 | +const normalize = (condition?: string) => (condition || 'and').toLowerCase(); |
| 27 | + |
| 28 | +// Recursively build predicate tree |
| 29 | +const buildPredicate = (node: any, ignoreCase: boolean): any => |
| 30 | + node?.isComplex && node.predicates?.length |
| 31 | + ? node.predicates |
| 32 | + .map((p: Predicate) => buildPredicate(p, ignoreCase)) |
| 33 | + .filter(Boolean) |
| 34 | + .reduce((acc: any, cur: any) => |
| 35 | + acc ? (normalize(node.condition) === 'or' ? acc.or(cur) : acc.and(cur)) : cur, null) |
| 36 | + : (node?.field && node?.operator ? new Predicate(node.field, node.operator, node.value, ignoreCase) : null); |
| 37 | + |
| 38 | +// Apply filtering based on predicates |
| 39 | +const performFiltering = (input: any, query: Query) => { |
| 40 | + const filter = Array.isArray(input) ? input[0] : input; |
| 41 | + if (!filter?.predicates?.length) return; |
| 42 | + const ignoreCase = filter.ignoreCase !== undefined ? !!filter.ignoreCase : true; |
| 43 | + const condition = normalize(filter.condition); |
| 44 | + const combined = filter.predicates |
| 45 | + .map((p: Predicate) => buildPredicate(p, ignoreCase)) |
| 46 | + .filter(Boolean) |
| 47 | + .reduce((acc: any, cur: any) => acc ? (condition === 'or' ? acc.or(cur) : acc.and(cur)) : cur, null); |
| 48 | + if (combined) query.where(combined); |
59 | 49 | }; |
60 | 50 |
|
61 | | -// Helper function: Build nested predicates for complex filtering |
62 | | -function buildNestedPredicate(block: any, ignoreCase: boolean) { |
63 | | - const condition = (block.condition || 'and').toLowerCase(); |
64 | | - let mergedPredicate: any | null = null; |
65 | | - |
66 | | - block.predicates.forEach((p: any) => { |
67 | | - let node; |
68 | | - if (p.isComplex && Array.isArray(p.predicates)) { |
69 | | - node = buildNestedPredicate(p, ignoreCase); |
70 | | - } else { |
71 | | - node = new Predicate(p.field, p.operator, p.value, ignoreCase); |
72 | | - } |
73 | | - if (node) { |
74 | | - mergedPredicate = mergedPredicate |
75 | | - ? (condition === 'or' ? mergedPredicate.or(node) : mergedPredicate.and(node)) |
76 | | - : node; |
77 | | - } |
78 | | - }); |
79 | | - |
80 | | - return mergedPredicate; |
81 | | -} |
82 | | - |
83 | 51 | // Helper function: Apply search functionality |
84 | 52 | const performSearching = (searchParam: any, query: any) => { |
85 | 53 | const { fields, key, operator, ignoreCase } = searchParam[0]; |
86 | 54 | query.search(key, fields, operator, ignoreCase); |
87 | 55 | }; |
88 | 56 |
|
89 | 57 | // Helper function: Apply sorting |
90 | | -const performSorting = (sortArray: any[], query: any) => { |
| 58 | +const performSorting = (sortArray: any[], query: Query) => { |
91 | 59 | for (let i = 0; i < sortArray.length; i++) { |
92 | 60 | const { name, direction } = sortArray[i]; |
93 | 61 | query.sortBy(name, direction); |
94 | 62 | } |
95 | 63 | }; |
96 | 64 |
|
97 | | -// Helper function: Apply paging |
98 | | -const performPaging = (data: any[], gridState: any) => { |
99 | | - if (!gridState.take || gridState.take <= 0) { |
100 | | - return data; |
101 | | - } |
102 | | - const pageSkip = gridState.skip || 0; |
103 | | - const pageSize = gridState.take; |
104 | | - return data.slice(pageSkip, pageSkip + pageSize); |
105 | | -}; |
106 | | - |
107 | 65 | // GET - Retrieve all data |
108 | 66 | export async function GET(request: NextRequest) { |
109 | 67 |
|
@@ -134,13 +92,16 @@ export async function GET(request: NextRequest) { |
134 | 92 | performSorting(gridState.sorted, query); |
135 | 93 | } |
136 | 94 |
|
137 | | - // Execute query on data |
138 | | - const resultantData = new DataManager(doctorDetails).executeLocal(query); |
139 | | - const count: any = resultantData.length; |
140 | | - let result: any = resultantData; |
141 | | - |
142 | 95 | // Paging |
143 | | - result = performPaging(result, gridState); |
| 96 | + if (gridState.take && gridState.take > 0) { |
| 97 | + const skip = gridState.skip || 0; |
| 98 | + const take = gridState.take; |
| 99 | + query.page(skip / take + 1, take); |
| 100 | + } |
| 101 | + |
| 102 | + // Execute query on data |
| 103 | + const result: object[] = new DataManager(doctorDetails).executeLocal(query); |
| 104 | + const count: number = result.length; |
144 | 105 |
|
145 | 106 | return corsResponse({ result, count }); |
146 | 107 | } |
@@ -183,7 +144,7 @@ export async function DELETE(request: NextRequest) { |
183 | 144 | } |
184 | 145 | const deletedDoctor = doctorDetails[doctorIndex]; |
185 | 146 | doctorDetails.splice(doctorIndex, 1); |
186 | | - return corsResponse({ message: "Doctor deleted" }); |
| 147 | + return corsResponse({ message: "Doctor deleted" }); |
187 | 148 | } |
188 | 149 | } |
189 | 150 |
|
|
0 commit comments