Skip to content

Commit

Permalink
+ modify collection, fix request method
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed May 5, 2024
1 parent 3032fa8 commit b7033d8
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/v2/routes/collection/create-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const responseSchema = z.object({
const openRoute = createRoute({
path: "/collection/create",
method: "post",
summary: "Create a new collection.",
summary: "Create a new collection",
description:
"Create a new collection, accent colours available for supporters",
tags: ["Collection"],
Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/collection/delete-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const responseSchema = z.object({

const openRoute = createRoute({
path: "/collection/{id}/delete",
method: "post",
summary: "Delete a collection.",
method: "delete",
summary: "Delete a collection",
description:
"Delete a collection. Only the owner of the collection can delete it.",
tags: ["Collection"],
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/collection/get-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const openRoute = createRoute({
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"],
tags: ["Collection"],
request: {
params: paramsSchema,
},
Expand Down
4 changes: 4 additions & 0 deletions src/v2/routes/collection/handler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { CreateCollectionRoute } from "./create-collection"
import { DeleteCollectionRoute } from "./delete-collection"
import { GetCollectionByIdRoute } from "./get-collection"
import { ModifyCollectionRoute } from "./modify-collection"

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

GetCollectionByIdRoute(handler)
ModifyCollectionRoute(handler)
CreateCollectionRoute(handler)
DeleteCollectionRoute(handler)

Expand Down
141 changes: 141 additions & 0 deletions src/v2/routes/collection/modify-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { getConnection } from "@/v2/db/turso"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { selectUserCollectionSchema, userCollection } 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 { AppHandler } from "../handler"
import { ColourType } from "@/v2/lib/colour"

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

const requestBodySchema = z.object({
name: z.string().min(1).max(32).optional(),
description: z.string().min(1).max(256).optional(),
isPublic: z.number().int().min(0).max(1).optional(),
accentColour: z.string().length(7).optional(),
})

const responseSchema = z.object({
success: z.literal(true),
collection: selectUserCollectionSchema,
})

const openRoute = createRoute({
path: "/{id}/modify",
method: "patch",
summary: "Modify a collection",
description:
"Modify an existing collection, you must be the owner of the collection to modify these details..",
tags: ["Collection"],
request: {
params: paramsSchema,
body: {
content: {
"application/json": {
schema: requestBodySchema,
},
},
},
},
responses: {
200: {
description: "Returns the collection's new attributes",
content: {
"application/json": {
schema: responseSchema,
},
},
},
...GenericResponses,
},
})

export const ModifyCollectionRoute = (handler: AppHandler) => {
handler.openapi(openRoute, async (ctx) => {
const { name, description, isPublic, accentColour } =
ctx.req.valid("json")

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

const authSessionManager = new AuthSessionManager(ctx)

const { user } = await authSessionManager.validateSession()

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

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

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

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

if (collection.userId !== user.id) {
return ctx.json(
{
success: false,
message:
"Unauthorized. You are not the owner of this collection.",
},
401
)
}

const [updatedCollection] = await drizzle
.update(userCollection)
.set({
name: name,
description: description,
isPublic: Boolean(isPublic),
accentColour: accentColour as ColourType,
})
.where(eq(userCollection.id, id))
.returning()

return ctx.json(
{
success: true,
collection: updatedCollection,
},
200
)
})
}

0 comments on commit b7033d8

Please sign in to comment.