-
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
15 changed files
with
463 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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
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,47 @@ | ||
import { AppHandler } from "../handler" | ||
import { getConnection } from "@/v2/db/turso" | ||
import { assetTag } from "@/v2/db/schema" | ||
import { GenericResponses } from "@/v2/lib/response-schemas" | ||
import { createRoute } from "@hono/zod-openapi" | ||
import { z } from "@hono/zod-openapi" | ||
import { selectAssetTagSchema } from "@/v2/db/schema" | ||
|
||
export const getAllTagsResponse = z.object({ | ||
success: z.literal(true), | ||
tags: selectAssetTagSchema.array(), | ||
}) | ||
|
||
const getAllTagsRoute = createRoute({ | ||
path: "/all", | ||
method: "get", | ||
summary: "Get all tags", | ||
description: "Get all tags.", | ||
tags: ["Tags"], | ||
responses: { | ||
200: { | ||
description: "All games.", | ||
content: { | ||
"application/json": { | ||
schema: getAllTagsResponse, | ||
}, | ||
}, | ||
}, | ||
...GenericResponses, | ||
}, | ||
}) | ||
|
||
export const AllTagsRoute = (handler: AppHandler) => { | ||
handler.openapi(getAllTagsRoute, async (ctx) => { | ||
const { drizzle } = await getConnection(ctx.env) | ||
|
||
const tags = (await drizzle.select().from(assetTag)) ?? [] | ||
|
||
return ctx.json( | ||
{ | ||
success: true, | ||
tags, | ||
}, | ||
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,108 @@ | ||
import { AppHandler } from "../handler" | ||
import { assetTag, game } from "@/v2/db/schema" | ||
import { eq } from "drizzle-orm" | ||
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager" | ||
import { getConnection } from "@/v2/db/turso" | ||
import { createRoute } from "@hono/zod-openapi" | ||
import { GenericResponses } from "@/v2/lib/response-schemas" | ||
import { z } from "@hono/zod-openapi" | ||
import { selectAssetTagSchema } from "@/v2/db/schema" | ||
|
||
export const createTagSchema = z.object({ | ||
name: z.string().min(3).max(32).openapi({ | ||
description: "The name of the tag.", | ||
example: "official", | ||
}), | ||
formattedName: z.string().min(3).max(64).openapi({ | ||
description: "The formatted name of the tag.", | ||
example: "Official", | ||
}), | ||
}) | ||
|
||
export const createTagResponse = z.object({ | ||
success: z.literal(true), | ||
tag: selectAssetTagSchema, | ||
}) | ||
|
||
const createTagRoute = createRoute({ | ||
path: "/create", | ||
method: "post", | ||
summary: "Create a tag", | ||
description: "Create a new tag.", | ||
tags: ["Tags"], | ||
request: { | ||
body: { | ||
content: { | ||
"application/json": { | ||
schema: createTagSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
responses: { | ||
200: { | ||
description: "Returns the new tag.", | ||
content: { | ||
"application/json": { | ||
schema: createTagResponse, | ||
}, | ||
}, | ||
}, | ||
...GenericResponses, | ||
}, | ||
}) | ||
|
||
export const CreateTagRoute = (handler: AppHandler) => { | ||
handler.openapi(createTagRoute, async (ctx) => { | ||
const authSessionManager = new AuthSessionManager(ctx) | ||
|
||
const { user } = await authSessionManager.validateSession() | ||
|
||
if (!user || user.role != "creator") { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Unauthorized", | ||
}, | ||
401 | ||
) | ||
} | ||
|
||
const { name, formattedName } = ctx.req.valid("json") | ||
|
||
const { drizzle } = getConnection(ctx.env) | ||
|
||
const [tagExists] = await drizzle | ||
.select({ name: assetTag.name }) | ||
.from(assetTag) | ||
.where(eq(assetTag.name, name)) | ||
|
||
if (tagExists) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Tag already exists", | ||
}, | ||
400 | ||
) | ||
} | ||
|
||
const [newTag] = await drizzle | ||
.insert(game) | ||
.values({ | ||
id: name, | ||
name, | ||
formattedName, | ||
lastUpdated: new Date().toISOString(), | ||
}) | ||
.returning() | ||
|
||
return ctx.json( | ||
{ | ||
success: true, | ||
tag: newTag, | ||
}, | ||
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,91 @@ | ||
import { AppHandler } from "../handler" | ||
import { getConnection } from "@/v2/db/turso" | ||
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager" | ||
import { assetTag } from "@/v2/db/schema" | ||
import { eq } from "drizzle-orm" | ||
import { createRoute } from "@hono/zod-openapi" | ||
import { GenericResponses } from "@/v2/lib/response-schemas" | ||
import { z } from "@hono/zod-openapi" | ||
|
||
export const deleteTagSchema = z.object({ | ||
id: z.string().openapi({ | ||
param: { | ||
name: "id", | ||
in: "path", | ||
description: "The ID of the tag to delete.", | ||
example: "official", | ||
required: true, | ||
}, | ||
}), | ||
}) | ||
|
||
export const deleteTagResponseSchema = z.object({ | ||
success: z.literal(true), | ||
}) | ||
|
||
const deleteTagRoute = createRoute({ | ||
path: "/{id}/delete", | ||
method: "delete", | ||
summary: "Delete a tag", | ||
description: "Delete a tag.", | ||
tags: ["Tags"], | ||
request: { | ||
params: deleteTagSchema, | ||
}, | ||
responses: { | ||
200: { | ||
description: "Returns boolean indicating success.", | ||
content: { | ||
"application/json": { | ||
schema: deleteTagResponseSchema, | ||
}, | ||
}, | ||
}, | ||
...GenericResponses, | ||
}, | ||
}) | ||
|
||
export const DeleteTagRoute = (handler: AppHandler) => { | ||
handler.openapi(deleteTagRoute, async (ctx) => { | ||
const id = ctx.req.valid("param").id | ||
|
||
const { drizzle } = await getConnection(ctx.env) | ||
|
||
const [foundTag] = await drizzle | ||
.select({ id: assetTag.id }) | ||
.from(assetTag) | ||
.where(eq(assetTag.id, id)) | ||
|
||
if (!foundTag) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Game not found", | ||
}, | ||
400 | ||
) | ||
} | ||
|
||
const authSessionManager = new AuthSessionManager(ctx) | ||
const { user } = await authSessionManager.validateSession() | ||
|
||
if (!user || user.role != "creator") { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Unauthorized", | ||
}, | ||
401 | ||
) | ||
} | ||
|
||
await drizzle.delete(assetTag).where(eq(assetTag.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,78 @@ | ||
import { AppHandler } from "../handler" | ||
import { createRoute } from "@hono/zod-openapi" | ||
import { assetTag } from "@/v2/db/schema" | ||
import { getConnection } from "@/v2/db/turso" | ||
import { eq } from "drizzle-orm" | ||
import { z } from "@hono/zod-openapi" | ||
import { selectAssetTagSchema } from "@/v2/db/schema" | ||
import { GenericResponses } from "@/v2/lib/response-schemas" | ||
|
||
const getTagByIdSchema = z.object({ | ||
id: z.string().openapi({ | ||
param: { | ||
name: "id", | ||
in: "path", | ||
description: "The ID of the tag to retrieve.", | ||
example: "official", | ||
required: true, | ||
}, | ||
}), | ||
}) | ||
|
||
const getTagByIDResponseSchema = z.object({ | ||
success: z.literal(true), | ||
tag: selectAssetTagSchema, | ||
}) | ||
|
||
const getTagByIdRoute = createRoute({ | ||
path: "/{id}", | ||
method: "get", | ||
summary: "Get a tag", | ||
description: "Get tag game by their ID.", | ||
tags: ["Tags"], | ||
request: { | ||
params: getTagByIdSchema, | ||
}, | ||
responses: { | ||
200: { | ||
description: "Tag was found.", | ||
content: { | ||
"application/json": { | ||
schema: getTagByIDResponseSchema, | ||
}, | ||
}, | ||
}, | ||
...GenericResponses, | ||
}, | ||
}) | ||
|
||
export const GetTagByIdRoute = (handler: AppHandler) => { | ||
handler.openapi(getTagByIdRoute, async (ctx) => { | ||
const id = ctx.req.valid("param").id | ||
|
||
const { drizzle } = await getConnection(ctx.env) | ||
|
||
const [foundTag] = await drizzle | ||
.select() | ||
.from(assetTag) | ||
.where(eq(assetTag.id, id)) | ||
|
||
if (!foundTag) { | ||
return ctx.json( | ||
{ | ||
success: false, | ||
message: "Tag not found", | ||
}, | ||
400 | ||
) | ||
} | ||
|
||
return ctx.json( | ||
{ | ||
success: true, | ||
tag: foundTag, | ||
}, | ||
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,16 @@ | ||
import { OpenAPIHono } from "@hono/zod-openapi" | ||
import { AllTagsRoute } from "./all-tags" | ||
import { CreateTagRoute } from "./create-tag" | ||
import { GetTagByIdRoute } from "./get-tag" | ||
import { DeleteTagRoute } from "./delete-tag" | ||
import { ModifyTagRoute } from "./modify-tag" | ||
|
||
const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>() | ||
|
||
AllTagsRoute(handler) | ||
GetTagByIdRoute(handler) | ||
CreateTagRoute(handler) | ||
ModifyTagRoute(handler) | ||
DeleteTagRoute(handler) | ||
|
||
export default handler |
Oops, something went wrong.