-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update
/users
routes to use new DB
- Loading branch information
Showing
7 changed files
with
140 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
export interface User { | ||
id: number; | ||
displayName: string; | ||
name: string; | ||
avatarUrl: string; | ||
discordId: number; | ||
bio: string; | ||
assetsUploaded: number; | ||
roles: string; | ||
id: string; | ||
avatar_url: string | null; | ||
banner_url: string | null; | ||
username: string; | ||
username_colour: string; | ||
bio: string | null; | ||
pronouns: string | null; | ||
verified: number; | ||
date_joined: string; | ||
role: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { responseHeaders } from "@/lib/responseHeaders"; | ||
import type { User } from "@/lib/types/user"; | ||
import type { Asset } from "@/lib/types/asset"; | ||
import { getConnection } from "@/lib/planetscale"; | ||
|
||
export const getUserByUsername = async ( | ||
request: Request, | ||
env: Env | ||
): Promise<Response> => { | ||
const url = new URL(request.url); | ||
const name = url.pathname.split("/")[2]; | ||
|
||
if (!name) { | ||
throw new Error("No Name provided"); | ||
} | ||
|
||
const cacheKey = new Request(url.toString(), request); | ||
|
||
const cache = caches.default; | ||
|
||
let response = await cache.match(cacheKey); | ||
|
||
if (response) { | ||
return response; | ||
} | ||
|
||
const db = await getConnection(env); | ||
|
||
const row = await db | ||
.execute("SELECT * FROM auth_user WHERE username = ?", [name]) | ||
.then((row) => row.rows[0] as User | undefined); | ||
|
||
const user = { | ||
id: row.id, | ||
username: row.username, | ||
avatar_url: row.avatar_url || null, | ||
banner_url: row.banner_url || null, | ||
bio: row.bio || null, | ||
pronouns: row.pronouns || null, | ||
verified: row.verified, | ||
date_joined: row.date_joined, | ||
roles: row.role, | ||
}; | ||
|
||
const uploadedAssets = await db | ||
.execute( | ||
"SELECT * FROM assets WHERE uploaded_by = ? ORDER BY uploaded_date DESC LIMIT 5", | ||
[user.id] | ||
) | ||
.then((row) => row.rows as Asset[] | undefined) | ||
.then((row) => | ||
row?.map((asset) => { | ||
return { | ||
id: asset.id, | ||
name: asset.name, | ||
game: asset.game, | ||
assetCategory: asset.asset_category, | ||
url: asset.url, | ||
tags: asset.tags, | ||
status: asset.status, | ||
uploadedBy: asset.uploaded_by, | ||
uploadedDate: asset.uploaded_date, | ||
fileSize: asset.file_size, | ||
}; | ||
}) | ||
); | ||
|
||
if (!row) { | ||
return new Response( | ||
JSON.stringify({ | ||
success: false, | ||
status: "error", | ||
error: "404 Not Found", | ||
}), | ||
{ | ||
headers: responseHeaders, | ||
} | ||
); | ||
} | ||
|
||
response = new Response( | ||
JSON.stringify({ | ||
success: true, | ||
status: "ok", | ||
user, | ||
uploadedAssets, | ||
}), | ||
{ | ||
headers: responseHeaders, | ||
} | ||
); | ||
|
||
response.headers.set("Cache-Control", "s-maxage=60"); | ||
await cache.put(cacheKey, response.clone()); | ||
|
||
return response; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters