Skip to content

Commit

Permalink
fallback to crud for asset querying
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Oct 15, 2023
1 parent 3f37973 commit cacbbd4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
5 changes: 3 additions & 2 deletions src/v2/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,9 @@ export const savedOcGenerators = sqliteTable(
game: text("game").notNull(),
dateCreated: integer("date_created").notNull(),
isPublic: integer("is_public").default(0).notNull(),
content: text("content").notNull(), // this is stored as json, which is then parsed on the frontend
savedColorPalette: text("saved_color_palette"), // array of 5 hex values, completely optional
content: text("content").notNull(),
savedColorPalette: text("saved_color_palette"), // array of 5 hex values, completely optional for the user to save
sakuraUrl: text("sakura_url"),
},
(savedOcGenerators) => {
return {
Expand Down
16 changes: 8 additions & 8 deletions src/v2/routes/auth/authRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ authRoute.post("/login", async (c) => {
return login(c)
})

authRoute.get("/validate", async (c) => {
return validate(c)
})

authRoute.post("/logout", async (c) => {
return logout(c)
})

authRoute.post("/update/attributes", async (c) => {
return updateUserAttributes(c)
})
Expand Down Expand Up @@ -79,12 +87,4 @@ authRoute.post("/oc-generator/delete", async (c) => {
return deleteOCGeneratorResponse(c)
})

authRoute.get("/validate", async (c) => {
return validate(c)
})

authRoute.post("/logout", async (c) => {
return logout(c)
})

export default authRoute
4 changes: 2 additions & 2 deletions src/v2/routes/auth/games/createGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export async function createGame(c: APIContext): Promise<Response> {
}

// check if game.name exists
const gameExists = await drizzle.query.assetCategories.findFirst({
where: (assetCategories, { eq }) => eq(assetCategories.name, game.name),
const gameExists = await drizzle.query.games.findFirst({
where: (games, { eq }) => eq(games.name, game.name),
})

if (gameExists) {
Expand Down
94 changes: 48 additions & 46 deletions src/v2/routes/search/asset/searchAssets.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { responseHeaders } from "@/v2/lib/responseHeaders"
import { getConnection } from "@/v2/db/turso"
import { like } from "drizzle-orm"

import { assets } from "@/v2/db/schema"
import { desc } from "drizzle-orm"
import { assetTagsAssets, assets, assetTags, users } from "@/v2/db/schema"
import { desc, like, sql, eq, and, or } from "drizzle-orm"
import { SplitQueryByCommas } from "@/v2/lib/helpers/splitQueryByCommas"

export async function searchForAssets(c: APIContext): Promise<Response> {
Expand All @@ -12,7 +11,8 @@ export async function searchForAssets(c: APIContext): Promise<Response> {
let response = await cache.match(cacheKey)
if (response) return response

const { query, game, assetCategory, assetTags } = c.req.query()
const { query, gameQuery, assetCategoryQuery, assetTagsQuery } =
c.req.query()

// search parameters can include optional search params: query, game, assetCategory, assetTags
// query?: string => ?query=keqing
Expand All @@ -22,60 +22,62 @@ export async function searchForAssets(c: APIContext): Promise<Response> {

const drizzle = getConnection(c.env).drizzle

// check if certian search parameters are present, if not, set them to null
const searchQuery = query ?? null
const gameList = game ? SplitQueryByCommas(game) : null
const assetCategoryList = assetCategory
? SplitQueryByCommas(assetCategory)
const gameList = gameQuery
? SplitQueryByCommas(gameQuery.toLowerCase())
: null
const assetTagsList = assetTags ? SplitQueryByCommas(assetTags) : null
const assetCategoryList = assetCategoryQuery
? SplitQueryByCommas(assetCategoryQuery.toLowerCase())
: null
const assetTagsList = assetTagsQuery
? SplitQueryByCommas(assetTagsQuery.toLowerCase())
: // TODO(dromzeh): allow for no tags to be specified, this is just temporary as it creates unnecessary complexity
["official", "fanmade"]

console.log(searchQuery, gameList, assetCategoryList, assetTagsList)
const assetTagResponse = drizzle.$with("sq").as(
drizzle
.select({
assetId: assetTagsAssets.assetId,
tags: sql<string[] | null>`array_agg(${assetTags})`.as("tags"),
})
.from(assetTagsAssets)
.leftJoin(assetTags, eq(assetTags.id, assetTagsAssets.assetTagId))
.where(or(...assetTagsList.map((tag) => eq(assetTags.name, tag))))
.groupBy(assetTagsAssets.assetId)
)

// query the database for assets that match the search parameters
const assetResponse = await drizzle.query.assets.findMany({
where: (assets, { and, or, eq }) => {
return and(
searchQuery ? like(assets.name, `%${searchQuery}%`) : null,
gameList
? or(...gameList.map((game) => eq(assets.game, game)))
: null,
assetCategoryList
? or(
...assetCategoryList.map((assetCategory) =>
eq(assets.assetCategory, assetCategory)
)
)
: null,
const result = await drizzle
.with(assetTagResponse)
.select()
.from(assets)
.innerJoin(assetTagResponse, eq(assetTagResponse.assetId, assets.id))
.where(
and(
searchQuery && like(assets.name, `%${searchQuery}%`),
gameList &&
or(...gameList.map((game) => eq(assets.game, game))),
assetCategoryList &&
or(
...assetCategoryList.map((category) =>
eq(assets.assetCategory, category)
)
),
eq(assets.status, 1)
)
},
with: {
assetTagsAssets: {
with: {
assetTags: true,
},
},
users: {
columns: {
email: false,
emailVerified: false,
},
},
},
orderBy: desc(assets.id),
limit: 500,
})
)
.leftJoin(users, eq(users.id, assets.uploadedById))
.orderBy(desc(assets.uploadedDate))
.limit(500)

response = c.json(
{
success: true,
status: "ok",
query,
game,
assetCategory,
assetTags,
results: assetResponse ? assetResponse : [],
gameQuery,
assetCategoryQuery,
assetTagsQuery,
results: result ?? [],
},
200,
responseHeaders
Expand Down

0 comments on commit cacbbd4

Please sign in to comment.