-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,4 @@ app.onError((err, ctx) => { | |
) | ||
}) | ||
|
||
export default app | ||
export default app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters