Skip to content

Commit

Permalink
shdjgfsfkdhjkldfj;fdgdgkfjbh
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed May 5, 2024
1 parent ac016bf commit 3032fa8
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ app.onError((err, ctx) => {
)
})

export default app
export default app
2 changes: 1 addition & 1 deletion src/v2/db/schema/collections/user-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const selectUserCollectionAssetSchema =
export const collectionRelations = relations(
userCollection,
({ one, many }) => ({
user: one(authUser, {
authUser: one(authUser, {
fields: [userCollection.userId],
references: [authUser.id],
relationName: "collection_auth_user",
Expand Down
12 changes: 8 additions & 4 deletions src/v2/routes/collection/create-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const openRoute = createRoute({
path: "/collection/create",
method: "post",
summary: "Create a new collection.",
description: "Create a new collection, accent colours available for supporters",
description:
"Create a new collection, accent colours available for supporters",
tags: ["Collection"],
request: {
body: {
Expand All @@ -36,7 +37,8 @@ const openRoute = createRoute({
},
responses: {
200: {
description: "Returns the collection + true if the collection was made.",
description:
"Returns the collection + true if the collection was made.",
content: {
"application/json": {
schema: responseSchema,
Expand Down Expand Up @@ -68,14 +70,16 @@ export const CreateCollectionRoute = (handler: AppHandler) => {

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

const [newCollection] = await drizzle.insert(userCollection)
const [newCollection] = await drizzle
.insert(userCollection)
.values({
name: name,
userId: user.id,
description: description,
isPublic: Boolean(isPublic),
accentColour: accentColour as ColourType,
}).returning()
})
.returning()

return ctx.json(
{
Expand Down
9 changes: 4 additions & 5 deletions src/v2/routes/collection/delete-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ export const DeleteCollectionRoute = (handler: AppHandler) => {
401
)
}

const { id } = ctx.req.valid("param")

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

const [existingCollection] = await drizzle
.select().from(userCollection)

const [existingCollection] = await drizzle.select().from(userCollection)

if (!existingCollection) {
return ctx.json(
{
Expand All @@ -90,7 +89,7 @@ export const DeleteCollectionRoute = (handler: AppHandler) => {
}

await drizzle.delete(userCollection).where(eq(userCollection.id, id))

return ctx.json(
{
success: true,
Expand Down
Empty file.
165 changes: 165 additions & 0 deletions src/v2/routes/collection/get-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import {
userCollection,
userCollectionCollaborators,
selectUserCollectionSchema,
selectUserSchema,
} 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 responseSchema = z.object({
success: z.literal(true),
collection: selectUserCollectionSchema
.pick({
id: true,
name: true,
accentColour: true,
isPublic: true,
userId: true,
})
.extend({
authUser: selectUserSchema.pick({
id: true,
avatarUrl: true,
displayName: true,
username: true,
usernameColour: true,
plan: true,
role: true,
}),
}),
})

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

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

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 collectionInfo = await drizzle.query.userCollection.findFirst({
columns: {
id: true,
name: true,
accentColour: true,
isPublic: true,
userId: true,
},
where: (collection, { eq }) => eq(collection.id, id),
with: {
authUser: {
columns: {
id: true,
avatarUrl: true,
displayName: true,
username: true,
usernameColour: true,
plan: true,
role: true,
},
},
},
})

return ctx.json(
{
success: true,
collection: collectionInfo,
},
200
)
})
}
14 changes: 7 additions & 7 deletions src/v2/routes/collection/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import { CreateCollectionRoute } from "./create-collection";
import { DeleteCollectionRoute } from "./delete-collection";
import { OpenAPIHono } from "@hono/zod-openapi"
import { CreateCollectionRoute } from "./create-collection"
import { DeleteCollectionRoute } from "./delete-collection"

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

CreateCollectionRoute(handler);
DeleteCollectionRoute(handler);
CreateCollectionRoute(handler)
DeleteCollectionRoute(handler)

export default handler;
export default handler

0 comments on commit 3032fa8

Please sign in to comment.