Skip to content

Commit

Permalink
products are now retrieved from dynamodb
Browse files Browse the repository at this point in the history
  • Loading branch information
Facundo De Lorenzo committed May 19, 2024
1 parent 3134d39 commit d522ae0
Show file tree
Hide file tree
Showing 5 changed files with 1,072 additions and 28 deletions.
4 changes: 4 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"@types/aws-lambda": "^8.10.137",
"@types/node": "^20.12.11",
"typescript": "^5.4.5"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.577.0",
"@aws-sdk/lib-dynamodb": "^3.577.0"
}
}
74 changes: 52 additions & 22 deletions packages/api/src/products.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { APIGatewayEvent, Callback, Context } from 'aws-lambda';
import { CreateOrderRequest, Order, Product } from './types';
import {
BatchWriteItemCommand,
DynamoDBClient,
WriteRequest,
} from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, QueryCommand } from '@aws-sdk/lib-dynamodb';

const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);

const responseHeaders = {
'Access-Control-Allow-Origin': '*',
Expand All @@ -14,45 +23,66 @@ exports.get = async (
) => {
try {
const req = event.queryStringParameters;
if (!req?.restaurantId)
const restaurantId = parseInt(req.restaurantId);
if (!restaurantId)
return {
isBase64Encoded: false,
statusCode: 400,
headers: responseHeaders,
body: 'RestaurantId must be provided',
};

const lastKey = req.lastKey ? JSON.parse(req.lastKey) : undefined;

const limit = req.limit ? Number(req.limit) : 5;
const offset = req.offset ? Number(req.offset) : 0;

const productsDB: Product[] = [
{ id: 1, name: 'Kuva Roast Rib Eye', price: 418 },
{ id: 2, name: 'Guadalupe Half Rack', price: 298 },
{ id: 3, name: 'Tohono Chicken', price: 308 },
{ id: 4, name: 'Milanesa napolitana', price: 500 },
{ id: 5, name: 'Asado', price: 800 },
{ id: 6, name: 'Choripan', price: 230 },
{ id: 7, name: 'Asado x4', price: 2700 },
{ id: 8, name: 'Kuva Roast Rib Eye', price: 418 },
{ id: 9, name: 'Guadalupe Half Rack', price: 298 },
{ id: 10, name: 'Tohono Chicken', price: 308 },
{ id: 11, name: 'Milanesa napolitana', price: 500 },
{ id: 12, name: 'Asado', price: 800 },
{ id: 13, name: 'Choripan', price: 230 },
{ id: 14, name: 'Asado x45', price: 2700 },
{ id: 15, name: 'Asado', price: 800 },
{ id: 16, name: 'Choripan', price: 230 },
{ id: 17, name: 'Asado x455', price: 2700 },
];
const command = new QueryCommand({
ExpressionAttributeValues: {
':id': restaurantId,
},
KeyConditionExpression: 'restaurantId = :id',
TableName: 'wakeup-challenge-products',
Limit: limit,
ExclusiveStartKey: lastKey,
});
const response = await docClient.send(command);
console.log('response: ', response);

const productsDB: Product[] = response.Items.map((x) => ({
id: x.productId,
name: x.name,
price: x.price,
}));
const newLastKey = response.LastEvaluatedKey;

// const productsDB: Product[] = [
// { id: 1, name: 'Kuva Roast Rib Eye', price: 418 },
// { id: 2, name: 'Guadalupe Half Rack', price: 298 },
// { id: 3, name: 'Tohono Chicken', price: 308 },
// { id: 4, name: 'Milanesa napolitana', price: 500 },
// { id: 5, name: 'Asado', price: 800 },
// { id: 6, name: 'Choripan', price: 230 },
// { id: 7, name: 'Asado x4', price: 2700 },
// { id: 8, name: 'Kuva Roast Rib Eye', price: 418 },
// { id: 9, name: 'Guadalupe Half Rack', price: 298 },
// { id: 10, name: 'Tohono Chicken', price: 308 },
// { id: 11, name: 'Milanesa napolitana', price: 500 },
// { id: 12, name: 'Asado', price: 800 },
// { id: 13, name: 'Choripan', price: 230 },
// { id: 14, name: 'Asado x45', price: 2700 },
// { id: 15, name: 'Asado', price: 800 },
// { id: 16, name: 'Choripan', price: 230 },
// { id: 17, name: 'Asado x455', price: 2700 },
// ];

const products = productsDB.slice(offset, offset + limit);
const hasMore = productsDB.length > offset + limit;

return {
isBase64Encoded: false,
statusCode: 200,
headers: responseHeaders,
body: JSON.stringify({ products, hasMore }),
body: JSON.stringify({ products, lastKey: newLastKey }),
};
} catch (error) {
return {
Expand Down
Empty file.
12 changes: 6 additions & 6 deletions packages/front/src/components/app/hooks/useGetProducts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ const useGetProducts = ({ restaurantId, limit }: IUseGetProducts) => {
const abortController = new AbortController();
const signal = abortController.signal;
const [products, setProducts] = useState<Product[]>([]);
const [offset, setOffset] = useState(0);
const [hasMore, setHasMore] = useState(false);
const [lastKey, setLastKey] = useState<any>();
const [isFetching, setIsFetching] = useState(false);

const callAxios = () => {
if (isFetching) return;
setIsFetching(true);
let strParams = `limit=${limit}&restaurantId=${restaurantId}`;
if (lastKey) strParams += `&lastKey=${JSON.stringify(lastKey)}`;
axios
.get(
`${process.env.REACT_APP_API_ENDPOINT}/products?limit=${limit}&offset=${offset}&restaurantId=${restaurantId}`,
`${process.env.REACT_APP_API_ENDPOINT}/products?${strParams}`,
{ signal }
)
.then((resp) => {
if (!signal.aborted)
if (resp.data) {
setOffset((value) => value + limit);
setHasMore(resp.data.hasMore);
setLastKey(resp.data.lastKey);
setProducts((value) => [...value, ...resp.data.products]);
}
})
Expand All @@ -56,7 +56,7 @@ const useGetProducts = ({ restaurantId, limit }: IUseGetProducts) => {
};
}, []);

return { products, hasMore, fetchNextPage, isFetching };
return { products, hasMore: !!lastKey, fetchNextPage, isFetching };
};

export default useGetProducts;
Loading

0 comments on commit d522ae0

Please sign in to comment.