Skip to content

Commit c99fb7b

Browse files
committed
Complete Section 13
1 parent ad6f1c5 commit c99fb7b

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed
Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { client } from "$services/redis";
22
import { itemsKey, itemsByViewsKey } from "$services/keys";
3+
import { deserialize } from "./deserialize";
34

45
export const itemsByViews = async (
56
order: "DESC" | "ASC" = "DESC",
67
offset = 0,
78
count = 10
89
) => {
9-
const results = await client.sort(itemsByViewsKey(), {
10-
GET: ["#", `${itemsKey("*")}->name`, `${itemsKey("*")}->views`],
10+
let results: any = await client.sort(itemsByViewsKey(), {
11+
GET: [
12+
"#",
13+
`${itemsKey("*")}->name`,
14+
`${itemsKey("*")}->views`,
15+
`${itemsKey("*")}->endingAt`,
16+
`${itemsKey("*")}->imageUrl`,
17+
`${itemsKey("*")}->price`,
18+
],
1119
BY: "score",
1220
DIRECTION: order,
1321
LIMIT: {
@@ -16,5 +24,44 @@ export const itemsByViews = async (
1624
},
1725
});
1826

19-
console.log(results);
27+
const items = [];
28+
29+
/*
30+
*
31+
*
32+
* Uderstanding this code:
33+
* [
34+
'91fdf6', 'Chair', '25', '1729520486331', 'https://realrealreal-redis.s3.amazonaws.com/4.jpg', '0',
35+
'bfdc60', 'coutch', '1', '1729106535253', 'https://realrealreal-redis.s3.amazonaws.com/51.jpg', '0',
36+
'967898', 'Chair', '1', '1729106525749', 'https://realrealreal-redis.s3.amazonaws.com/30.jpg', '0',
37+
'8110cf', 'table', '1', '1729106543084', 'https://realrealreal-redis.s3.amazonaws.com/8.jpg', '0'
38+
]
39+
40+
Each item (or object) you're trying to create needs 6 pieces of information. For example:
41+
42+
id = '91fdf6'
43+
name = 'Chair'
44+
views = '25'
45+
endingAt = '1729520486331'
46+
imageUrl = 'https://realrealreal-redis.s3.amazonaws.com/4.jpg'
47+
price = '0'
48+
So the loop is trying to take these 6 values and make an object out of them, and then repeat for the next 6 values.
49+
50+
*/
51+
while (results.length) {
52+
const [id, name, views, endingAt, imageUrl, price, ...rest] = results;
53+
54+
const item = deserialize(id, {
55+
name,
56+
views,
57+
endingAt,
58+
imageUrl,
59+
price,
60+
});
61+
items.push(item);
62+
63+
results = rest;
64+
}
65+
66+
return items;
2067
};

0 commit comments

Comments
 (0)