-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jonathan
committed
Oct 12, 2023
1 parent
e38281f
commit 9201f1f
Showing
6 changed files
with
130 additions
and
59 deletions.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
"extends": ["./node_modules/standard/eslintrc.json", "next/core-web-vitals"] | ||
} |
24 changes: 15 additions & 9 deletions
24
pruebas/02-bazar-universal/jfilgairacordon/src/app/api/items/[id]/route.ts
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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import { NextResponse } from 'next/server' | ||
import { findProductById } from '../../services/find' | ||
|
||
export async function GET () { | ||
const parameters = NextRequest.arguments; | ||
console.log(parameters); | ||
|
||
return NextResponse.json({ | ||
message: 'Hello World' | ||
}) | ||
} | ||
export async function GET ( | ||
request: Request, | ||
{ params }: { params: { id: number } } | ||
) { | ||
const { id } = params | ||
// usamos el service que irá a buscar el producto a la bbdd. | ||
try { | ||
const product = await findProductById({ id }) | ||
return NextResponse.json(product) | ||
} catch (e) { | ||
return NextResponse.json({ error: 'Product not found' }, { status: 404 }) | ||
} | ||
} |
17 changes: 13 additions & 4 deletions
17
pruebas/02-bazar-universal/jfilgairacordon/src/app/api/items/route.ts
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 |
---|---|---|
@@ -1,7 +1,16 @@ | ||
import { NextResponse } from 'next/server' | ||
import { findProductsByQuery } from '../services/find' | ||
|
||
export async function GET (request: Request) { | ||
const { searchParams } = new URL(request.url) | ||
const search = searchParams.get('q') | ||
|
||
if (search) { | ||
const products = await findProductsByQuery({ query: search }) | ||
return NextResponse.json(products) | ||
} | ||
|
||
export async function GET () { | ||
return NextResponse.json({ | ||
message: 'Hello World' | ||
}) | ||
} | ||
message: 'You should use param search in order to search items' | ||
}, { status: 404 }) | ||
} |
39 changes: 39 additions & 0 deletions
39
pruebas/02-bazar-universal/jfilgairacordon/src/app/api/services/find.ts
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,39 @@ | ||
import { BaseProduct } from '@/types/product' | ||
import * as productsList from '../../../../../products.json' | ||
|
||
type FindProductByIdProps = { | ||
id: number | ||
} | ||
export async function findProductById ({ id }: FindProductByIdProps) { | ||
const product = productsList.products.find(x => x.id.toString() === id.toString()) | ||
if (!product) throw new Error('Product not found') | ||
|
||
// Simulamos un delay de 1 segundo para que se vea el loading | ||
await new Promise(resolve => setTimeout(resolve, 1000)) | ||
|
||
return product | ||
} | ||
|
||
type FindProductsByQueryProps = { | ||
query: string | ||
} | ||
export async function findProductsByQuery ({ query }: FindProductsByQueryProps) { | ||
const products = productsList.products.filter(x => | ||
x.title.toLowerCase().includes(query.toLowerCase()) || | ||
x.description.toLowerCase().includes(query.toLowerCase())) | ||
|
||
// Los mapeamos a BaseProduct | ||
const parsedProducts = products.map(x => ({ | ||
id: x.id, | ||
title: x.title, | ||
description: x.description, | ||
price: x.price, | ||
rating: x.rating, | ||
image: x.images[0] | ||
} as BaseProduct)) | ||
|
||
// Simulamos un delay de 1 segundo para que se vea el loading | ||
await new Promise(resolve => setTimeout(resolve, 1000)) | ||
|
||
return parsedProducts | ||
} |
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
17 changes: 17 additions & 0 deletions
17
pruebas/02-bazar-universal/jfilgairacordon/src/types/product.d.ts
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,17 @@ | ||
export type BaseProduct = { | ||
id: number, | ||
title: string, | ||
description: string, | ||
price: number, | ||
image: string, | ||
rating: number | ||
} | ||
|
||
export type Product = Omit<BaseProduct, 'image'> & { | ||
discountPercentage: number | ||
stock: number | ||
brand: string | ||
category: string | ||
thumbnail: string | ||
images: string[] | ||
} |