Skip to content

Commit

Permalink
dl route, add more data for /contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 18f0647 commit f870e28
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/v2/routes/asset/download-asset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { asset } from "@/v2/db/schema"
import { eq, sql } from "drizzle-orm"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"

const downloadAssetByIdSchema = z.object({
id: z.string().openapi({
param: {
name: "id",
in: "path",
description: "The ID of the asset to retrieve.",
required: true,
},
}),
})

const downloadAssetByIdResponseSchema = z.object({
success: z.literal(true),
downloadUrl: z.string(),
})

const downloadAssetByIdRoute = createRoute({
path: "/{id}/download",
method: "get",
description: "Download an asset by their ID.",
tags: ["Asset"],
request: {
params: downloadAssetByIdSchema,
},
responses: {
200: {
description: "Asset downloaded successfully.",
response: {
content: {
"application/json": {
schema: downloadAssetByIdResponseSchema,
},
},
},
},
...GenericResponses,
},
})

export const DownloadAssetRoute = (handler: AppHandler) => {
handler.openapi(downloadAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

const { drizzle } = await getConnection(ctx.env)

const [foundAsset] = await drizzle
.select({
url: asset.url,
})
.from(asset)
.where(eq(asset.id, assetId))
.limit(1)

if (!foundAsset) {
return ctx.json(
{
success: false,
message: "Asset not found",
},
404
)
}

await drizzle
.update(asset)
.set({
viewCount: sql`${asset.downloadCount} + 1`,
})
.where(eq(asset.id, assetId))

return ctx.json({
success: true,
downloadUrl: foundAsset.url,
})
})
}
2 changes: 2 additions & 0 deletions src/v2/routes/asset/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GetAssetLikesRoute } from "./get-users-asset-likes"
import { ModifyAssetRoute } from "./modify-asset"
import { UploadAssetRoute } from "./upload-asset"
import { DeleteAssetByIdRoute } from "./delete-asset"
import { DownloadAssetRoute } from "./download-asset"

import { ViewAssetCommentsRoute } from "./get-asset-comments"

Expand All @@ -17,6 +18,7 @@ AssetSearchAllFilterRoute(handler)
UploadAssetRoute(handler)

GetAssetByIdRoute(handler)
DownloadAssetRoute(handler)
ModifyAssetRoute(handler)
DeleteAssetByIdRoute(handler)

Expand Down
4 changes: 4 additions & 0 deletions src/v2/routes/contributors/all-contributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const contributorListSchema = z.object({
id: true,
username: true,
avatarUrl: true,
displayName: true,
usernameColour: true,
plan: true,
role: true,
})
Expand Down Expand Up @@ -48,6 +50,8 @@ export const AllContributorsRoute = (handler: AppHandler) => {
id: authUser.id,
username: authUser.username,
avatarUrl: authUser.avatarUrl,
displayName: authUser.displayName,
usernameColour: authUser.usernameColour,
plan: authUser.plan,
role: authUser.role,
})
Expand Down

0 comments on commit f870e28

Please sign in to comment.