| 17 | } |
| 18 | |
| 19 | export async function getUsers( |
| 20 | input: GetUsersInput |
| 21 | ): Promise<GetUsersResponse> { |
| 22 | try { |
| 23 | const offset = (input.page - 1) * input.totalItems; |
| 24 | const limit = input.totalItems; |
| 25 | |
| 26 | const [totalCount] = await db |
| 27 | .select({ count: count() }) |
| 28 | .from(users) |
| 29 | .where( |
| 30 | or( |
| 31 | input.search ? ilike(users.email, `%${input.search}%`) : undefined, |
| 32 | input.search ? ilike(users.username, `%${input.search}%`) : undefined |
| 33 | ) |
| 34 | ); |
| 35 | |
| 36 | const totalPages = Math.ceil(totalCount.count / limit); |
| 37 | |
| 38 | const items = await db |
| 39 | .select() |
| 40 | .from(users) |
| 41 | .where( |
| 42 | or( |
| 43 | input.search ? ilike(users.email, `%${input.search}%`) : undefined, |
| 44 | input.search ? ilike(users.username, `%${input.search}%`) : undefined |
| 45 | ) |
| 46 | ) |
| 47 | .offset(offset) |
| 48 | .limit(limit); |
| 49 | |
| 50 | return { |
| 51 | items, |
| 52 | totalPages, |
| 53 | }; |
| 54 | } catch (e) { |
| 55 | throw e; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | interface CreateUserInput { |
| 60 | username: string; |