Skip to content

Commit

Permalink
we are so locked in (trust me)
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed May 5, 2024
1 parent 431c54d commit ac016bf
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 1 deletion.
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
88 changes: 88 additions & 0 deletions src/v2/routes/collection/create-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { AppHandler } from "../handler"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"
import { getConnection } from "@/v2/db/turso"
import { selectUserCollectionSchema, userCollection } from "@/v2/db/schema"
import { ColourType } from "@/v2/lib/colour"

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

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

const openRoute = createRoute({
path: "/collection/create",
method: "post",
summary: "Create a new collection.",
description: "Create a new collection, accent colours available for supporters",
tags: ["Collection"],
request: {
body: {
content: {
"application/json": {
schema: requestBodySchema,
},
},
},
},
responses: {
200: {
description: "Returns the collection + true if the collection was made.",
content: {
"application/json": {
schema: responseSchema,
},
},
},
...GenericResponses,
},
})

export const CreateCollectionRoute = (handler: AppHandler) => {
handler.openapi(openRoute, async (ctx) => {
const authSessionManager = new AuthSessionManager(ctx)

const { user } = await authSessionManager.validateSession()

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

const { name, description, isPublic, accentColour } =
ctx.req.valid("json")

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

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

return ctx.json(
{
success: true,
collection: newCollection,
},
200
)
})
}
101 changes: 101 additions & 0 deletions src/v2/routes/collection/delete-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { AppHandler } from "../handler"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"
import { getConnection } from "@/v2/db/turso"
import { userCollection } from "@/v2/db/schema"
import { eq } from "drizzle-orm"

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

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

const openRoute = createRoute({
path: "/collection/{id}/delete",
method: "post",
summary: "Delete a collection.",
description:
"Delete a collection. Only the owner of the collection can delete it.",
tags: ["Collection"],
request: {
params: pathSchema,
},
responses: {
200: {
description:
"Returns true if the collection was deleted successfully.",
content: {
"application/json": {
schema: responseSchema,
},
},
},
...GenericResponses,
},
})

export const DeleteCollectionRoute = (handler: AppHandler) => {
handler.openapi(openRoute, async (ctx) => {
const authSessionManager = new AuthSessionManager(ctx)

const { user } = await authSessionManager.validateSession()

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

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

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

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

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

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

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

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

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

CreateCollectionRoute(handler);
DeleteCollectionRoute(handler);

export default handler;
2 changes: 2 additions & 0 deletions src/v2/routes/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ContributorRoute from "@/v2/routes/contributors/handler"
import AuthRoute from "@/v2/routes/auth/handler"
import RequestFormRoute from "@/v2/routes/requests/handler"
import CategoriesRoute from "@/v2/routes/category/handler"
import CollectionsRoute from "@/v2/routes/collection/handler"

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

Expand All @@ -18,6 +19,7 @@ handler.route("/user", UserRoute)
handler.route("/contributors", ContributorRoute)
handler.route("/auth", AuthRoute)
handler.route("/request", RequestFormRoute)
handler.route("/collection", CollectionsRoute)

export default handler

Expand Down

0 comments on commit ac016bf

Please sign in to comment.