Skip to content

Commit

Permalink
get collection assets (&inc offset)
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed May 5, 2024
1 parent b7033d8 commit 2ba9959
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
188 changes: 188 additions & 0 deletions src/v2/routes/collection/get-collection-assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import {
userCollection,
userCollectionCollaborators,
selectUserCollectionAssetSchema,
selectAssetSchema,
} from "@/v2/db/schema"
import { and, eq } from "drizzle-orm"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"

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

const querySchema = z.object({
offset: z
.string()
.optional()
.openapi({
param: {
name: "offset",
in: "query",
description: "The offset to start from.",
required: false,
},
}),
})

const responseSchema = z.object({
success: z.literal(true),
assets: z.array(
selectUserCollectionAssetSchema
.pick({ order: true, dateAdded: true })
.extend({
asset: selectAssetSchema.pick({
id: true,
name: true,
extension: true,
url: true,
viewCount: true,
downloadCount: true,
uploadedDate: true,
fileSize: true,
width: true,
height: true,
}),
})
),
})

const openRoute = createRoute({
path: "/{id}/assets",
method: "get",
summary: "Get a collection's assets",
description:
"Get a collection's assets by its ID. Returns 50 per request. If you do not have access to the collection (it is private/you do not have edit permission), it will not be returned.",
tags: ["Collection"],
request: {
params: paramsSchema,
query: querySchema,
},
responses: {
200: {
description: "Asset information",
content: {
"application/json": {
schema: responseSchema,
},
},
},
...GenericResponses,
},
})

export const GetCollectionAssetsByIdRoute = (handler: AppHandler) => {
handler.openapi(openRoute, async (ctx) => {
const { id } = ctx.req.valid("param")
const { offset } = ctx.req.valid("query")

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

const [validCollection] = await drizzle
.select({
id: userCollection.id,
userId: userCollection.userId,
isPublic: userCollection.isPublic,
})
.from(userCollection)
.where(eq(userCollection.id, id))

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

const authSessionManager = new AuthSessionManager(ctx)

const { user } = await authSessionManager.validateSession()

if (!validCollection.isPublic) {
if (!user) {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}

const [collaborator] = await drizzle
.select()
.from(userCollectionCollaborators)
.where(
and(
eq(userCollectionCollaborators.collectionId, id),
eq(userCollectionCollaborators.collaboratorId, user.id)
)
)

if (!collaborator && validCollection.userId != user.id) {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}
}

const collectionAssets =
await drizzle.query.userCollectionAsset.findMany({
columns: {
collectionId: true,
order: true,
dateAdded: true,
},
where: (userCollectionAsset) =>
eq(userCollectionAsset.collectionId, id),
with: {
asset: {
columns: {
id: true,
name: true,
extension: true,
url: true,
viewCount: true,
downloadCount: true,
uploadedDate: true,
fileSize: true,
width: true,
height: true,
},
},
},
offset: parseInt(offset) || 0,
limit: 50,
orderBy: (userCollectionAsset, { asc }) => [
asc(userCollectionAsset.order),
],
})

return ctx.json(
{
success: true,
assets: collectionAssets,
},
200
)
})
}
2 changes: 2 additions & 0 deletions src/v2/routes/collection/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { CreateCollectionRoute } from "./create-collection"
import { DeleteCollectionRoute } from "./delete-collection"
import { GetCollectionByIdRoute } from "./get-collection"
import { ModifyCollectionRoute } from "./modify-collection"
import { GetCollectionAssetsByIdRoute } from "./get-collection-assets"

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()

GetCollectionByIdRoute(handler)
GetCollectionAssetsByIdRoute(handler)
ModifyCollectionRoute(handler)
CreateCollectionRoute(handler)
DeleteCollectionRoute(handler)
Expand Down

0 comments on commit 2ba9959

Please sign in to comment.