File tree 7 files changed +85
-4
lines changed
7 files changed +85
-4
lines changed Original file line number Diff line number Diff line change 1
- import { NextResponse } from 'next/server' ;
1
+ import { NextRequest , NextResponse } from 'next/server' ;
2
2
import { getProductById } from '../../../../src/utils/products' ;
3
3
4
- export async function GET ( request : { url : string | URL } ) {
4
+ export async function GET ( request : Request | NextRequest ) {
5
5
try {
6
6
const { pathname } = new URL ( request . url ) ;
7
7
const segments = pathname . split ( '/' ) ;
Original file line number Diff line number Diff line change 1
- import { NextResponse } from 'next/server' ;
1
+ import { NextRequest , NextResponse } from 'next/server' ;
2
2
import { getProducts , searchProducts } from '../../../src/utils/products' ;
3
3
4
4
// Handle GET requests
5
- export async function GET ( request : { url : string | URL } ) {
5
+ export async function GET ( request : Request | NextRequest ) {
6
6
try {
7
7
const { searchParams } = new URL ( request . url ) ;
8
8
const limit = searchParams . get ( 'limit' ) ;
Original file line number Diff line number Diff line change
1
+ import { NextResponse } from 'next/server' ;
2
+ import { getOrdersByUserId } from '../../../../../src/utils/orders' ;
3
+
4
+ export async function GET ( _request : any , { params } : any ) {
5
+ const { id } = params ;
6
+
7
+ const orders = getOrdersByUserId ( id ) ;
8
+ if ( orders . length > 0 ) {
9
+ return NextResponse . json ( orders ) ;
10
+ } else {
11
+ return NextResponse . json ( { message : 'No orders found for this user' } , { status : 404 } ) ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ import { NextResponse } from 'next/server' ;
2
+ import { getUserById } from '../../../../src/utils/users' ;
3
+
4
+ export async function GET ( _request : any , { params } : any ) {
5
+ const { id } = params ;
6
+
7
+ // Implement auth
8
+
9
+ const user = getUserById ( id ) ;
10
+ if ( user ) {
11
+ return NextResponse . json ( user ) ;
12
+ } else {
13
+ return NextResponse . json ( { message : 'User not found' } , { status : 404 } ) ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ import { NextResponse } from 'next/server' ;
2
+ import { getOrdersByUserId , calculateTotalSpent } from '../../../../../src/utils/orders' ;
3
+
4
+ export async function GET ( _request : any , { params } : any ) {
5
+ const { id } = params ;
6
+
7
+ const orders = getOrdersByUserId ( id ) ;
8
+ if ( orders . length > 0 ) {
9
+ const totalSpent = calculateTotalSpent ( orders ) ;
10
+ return NextResponse . json ( { totalSpent } ) ;
11
+ } else {
12
+ return NextResponse . json ( { message : 'No orders found for this user' , totalSpent : 0 } ) ;
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ // import ordersLargeData from '../mock/large/orders.json';
2
+ import ordersSmallData from '../mock/small/orders.json' ;
3
+
4
+ import productsSmallData from '../mock/small/products.json' ;
5
+ import productsLargeData from '../mock/large/products.json' ;
6
+
7
+ const orders = [ ...ordersSmallData /* ...ordersLargeData */ ] . flat ( ) ;
8
+ const products = [ ...productsSmallData , productsLargeData ] . flat ( ) ;
9
+
10
+ export function getOrdersByUserId ( userId : string ) {
11
+ return orders . filter ( ( order ) => order . user === userId ) ;
12
+ }
13
+
14
+ export function calculateTotalSpent ( orders : any [ ] ) {
15
+ let total = 0 ;
16
+ orders . forEach ( ( order ) => {
17
+ order . items . forEach ( ( item : { id : string ; price : number ; quantity : number } ) => {
18
+ const product = products . find ( ( p ) => p . id === item . id ) ;
19
+ if ( product ) {
20
+ total += item . price * item . quantity ;
21
+ }
22
+ } ) ;
23
+ } ) ;
24
+ return total ;
25
+ }
Original file line number Diff line number Diff line change
1
+ import usersLargeData from '../mock/large/users.json' ;
2
+ import usersSmallData from '../mock/small/users.json' ;
3
+
4
+ const users = [ ...usersSmallData , ...usersLargeData ] ;
5
+
6
+ /**
7
+ * Retrieves a user object from the combined user datasets based on the provided user ID.
8
+ *
9
+ * @param {string } id - The unique identifier for the user.
10
+ * @returns {Object | undefined } The user object if found, otherwise undefined.
11
+ */
12
+ export function getUserById ( id : string ) {
13
+ return users . find ( ( user ) => user . id === id ) ;
14
+ }
You can’t perform that action at this time.
0 commit comments