@@ -3,7 +3,25 @@ import { client } from "$services/redis";
3
3
import type { CreateUserAttrs } from "$services/types" ;
4
4
import { genId } from "$services/utils" ;
5
5
6
- export const getUserByUsername = async ( username : string ) => { } ;
6
+ export const getUserByUsername = async ( username : string ) => {
7
+ // use username variable
8
+ // look up if we can find the decimal id of the user
9
+ const decimalId = await client . zScore ( usernamesKey ( ) , username ) ;
10
+
11
+ // check if there is an id
12
+ if ( ! decimalId ) {
13
+ throw new Error ( "User does not exist!" ) ;
14
+ }
15
+
16
+ // convert the decimal to base16
17
+ const id = decimalId . toString ( 16 ) ;
18
+
19
+ // find the user
20
+ const user = await client . hGetAll ( usersKey ( id ) ) ;
21
+
22
+ // deserialize the user and return
23
+ return deserialize ( id , user ) ;
24
+ } ;
7
25
8
26
export const getUserById = async ( id : string ) => {
9
27
const user = await client . hGetAll ( usersKey ( id ) ) ;
@@ -19,7 +37,7 @@ export const createUser = async (attrs: CreateUserAttrs) => {
19
37
20
38
await client . hSet ( usersKey ( id ) , serialize ( attrs ) ) ;
21
39
await client . sAdd ( usernamesUniqueKey ( ) , attrs . username ) ;
22
- await client . zAdd ( usernamesUniqueKey ( ) , {
40
+ await client . zAdd ( usernamesKey ( ) , {
23
41
value : attrs . username ,
24
42
score : parseInt ( id , 16 ) , // we converted a base 16 to a base 10 by implementing the parInt function
25
43
} ) ;
0 commit comments