Skip to content

Commit

Permalink
tag routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 57667af commit d7f12db
Show file tree
Hide file tree
Showing 15 changed files with 463 additions and 0 deletions.
File renamed without changes.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions src/v2/routes/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OpenAPIHono } from "@hono/zod-openapi"
import UserRoute from "@/v2/routes/user/handler"
import GameRoute from "@/v2/routes/game/handler"
import AssetRoute from "@/v2/routes/asset/handler"
import TagRoute from "@/v2/routes/tags/handler"
import ContributorRoute from "@/v2/routes/contributors/handler"
import AuthRoute from "@/v2/routes/auth/handler"
import RequestFormRoute from "@/v2/routes/requests/handler"
Expand All @@ -10,6 +11,7 @@ const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()

handler.route("/game", GameRoute)
handler.route("/asset", AssetRoute)
handler.route("/tags", TagRoute)
handler.route("/user", UserRoute)
handler.route("/contributors", ContributorRoute)
handler.route("/auth", AuthRoute)
Expand Down
47 changes: 47 additions & 0 deletions src/v2/routes/tags/all-tags.ts
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
)
})
}
108 changes: 108 additions & 0 deletions src/v2/routes/tags/create-tag.ts
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
)
})
}
91 changes: 91 additions & 0 deletions src/v2/routes/tags/delete-tag.ts
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
)
})
}
78 changes: 78 additions & 0 deletions src/v2/routes/tags/get-tag.ts
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
)
})
}
16 changes: 16 additions & 0 deletions src/v2/routes/tags/handler.ts
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
Loading

0 comments on commit d7f12db

Please sign in to comment.