1
1
import { client } from "$services/redis" ;
2
2
import { itemsKey , itemsByViewsKey } from "$services/keys" ;
3
+ import { deserialize } from "./deserialize" ;
3
4
4
5
export const itemsByViews = async (
5
6
order : "DESC" | "ASC" = "DESC" ,
6
7
offset = 0 ,
7
8
count = 10
8
9
) => {
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
+ ] ,
11
19
BY : "score" ,
12
20
DIRECTION : order ,
13
21
LIMIT : {
@@ -16,5 +24,44 @@ export const itemsByViews = async (
16
24
} ,
17
25
} ) ;
18
26
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 ;
20
67
} ;
0 commit comments