Skip to content

Commit

Permalink
fix get single asset schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 1e9dca1 commit 8c808b5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 32 deletions.
114 changes: 83 additions & 31 deletions src/v2/routes/asset/get-asset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { asset } from "@/v2/db/schema"
import { asset, assetLikes } from "@/v2/db/schema"
import { eq, sql } from "drizzle-orm"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
Expand Down Expand Up @@ -28,29 +28,46 @@ const getAssetByIdSchema = z.object({
const getAssetByIdResponseSchema = z.object({
success: z.literal(true),
// mmm nested schemas
asset: selectAssetSchema.extend({
assetTagAsset: z.array(
selectAssetTagAssetSchema.extend({
assetTag: selectAssetTagSchema,
})
),
}),
authUser: selectUserSchema.pick({
id: true,
avatarUrl: true,
displayName: true,
username: true,
usernameColour: true,
pronouns: true,
verified: true,
bio: true,
dateJoined: true,
plan: true,
role: true,
}),
game: selectGameSchema,
assetCategory: selectAssetCategorySchema,
// similarAssets: selectAssetSchema.array(),
asset: selectAssetSchema
.pick({
id: true,
name: true,
extension: true,
url: true,
viewCount: true,
downloadCount: true,
fileSize: true,
width: true,
height: true,
})
.extend({
assetTagAsset: z.array(
selectAssetTagAssetSchema.pick({}).extend({
assetTag: selectAssetTagSchema.pick({
id: true,
formattedName: true,
}),
})
),
authUser: selectUserSchema.pick({
id: true,
avatarUrl: true,
displayName: true,
username: true,
usernameColour: true,
plan: true,
role: true,
}),
game: selectGameSchema.pick({
id: true,
formattedName: true,
}),
assetCategory: selectAssetCategorySchema.pick({
id: true,
formattedName: true,
}),
}),
assetLikes: z.number(),
})

const getAssetByIdRoute = createRoute({
Expand Down Expand Up @@ -81,11 +98,31 @@ export const GetAssetByIdRoute = (handler: AppHandler) => {
const { drizzle } = await getConnection(ctx.env)

const foundAsset = await drizzle.query.asset.findFirst({
columns: {
id: true,
name: true,
extension: true,
url: true,
viewCount: true,
downloadCount: true,
fileSize: true,
width: true,
height: true,
},
where: (asset, { eq }) => eq(asset.id, assetId),
with: {
assetTagAsset: {
columns: {
assetTagId: false,
assetId: false,
},
with: {
assetTag: true,
assetTag: {
columns: {
id: true,
formattedName: true,
},
},
},
},
authUser: {
Expand All @@ -95,19 +132,33 @@ export const GetAssetByIdRoute = (handler: AppHandler) => {
displayName: true,
username: true,
usernameColour: true,
pronouns: true,
verified: true,
bio: true,
dateJoined: true,
plan: true,
role: true,
},
},
game: true,
assetCategory: true,
game: {
columns: {
id: true,
formattedName: true,
},
},
assetCategory: {
columns: {
id: true,
formattedName: true,
},
},
},
})

const [totalAssetLikes] = await drizzle
.select({
likeCount: sql<number>`COUNT(${assetLikes.assetId})`,
})
.from(asset)
.where(eq(asset.id, assetId))
.limit(1)

if (!foundAsset) {
return ctx.json(
{
Expand All @@ -129,6 +180,7 @@ export const GetAssetByIdRoute = (handler: AppHandler) => {
{
success: true,
asset: foundAsset,
assetLikes: totalAssetLikes.likeCount,
},
200
)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/v2/routes/asset/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GetAssetByIdRoute } from "./get-asset"
import { LikeAssetByIdRoute } from "./like-asset"
import { UnlikeAssetByIdRoute } from "./unlike-asset"
import { AssetSearchAllFilterRoute } from "./search-assets"
import { GetAssetLikesRoute } from "./get-asset-likes"
import { GetAssetLikesRoute } from "./get-users-asset-likes"
import { ModifyAssetRoute } from "./modify-asset"
import { UploadAssetRoute } from "./upload-asset"
import { DeleteAssetByIdRoute } from "./delete-asset"
Expand Down

0 comments on commit 8c808b5

Please sign in to comment.