Skip to content

Commit

Permalink
category routes impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 5809f87 commit 63f191a
Show file tree
Hide file tree
Showing 8 changed files with 463 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/v2/routes/category/all-categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { assetCategory } from "@/v2/db/schema"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { createRoute } from "@hono/zod-openapi"
import { z } from "@hono/zod-openapi"
import { selectAssetCategorySchema } from "@/v2/db/schema"

export const getAllCategoriesResponseSchema = z.object({
success: z.literal(true),
categories: selectAssetCategorySchema.array(),
})

const getAllCategoriesRoute = createRoute({
path: "/all",
method: "get",
summary: "Get all categories",
description: "Get all categories.",
tags: ["Category"],
responses: {
200: {
description: "All categories.",
content: {
"application/json": {
schema: getAllCategoriesResponseSchema,
},
},
},
...GenericResponses,
},
})

export const AllCategoriesRoute = (handler: AppHandler) => {
handler.openapi(getAllCategoriesRoute, async (ctx) => {
const { drizzle } = await getConnection(ctx.env)

const assetCategories =
(await drizzle.select().from(assetCategory)) ?? []

return ctx.json(
{
success: true,
categories: assetCategories,
},
200
)
})
}
108 changes: 108 additions & 0 deletions src/v2/routes/category/create-category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { AppHandler } from "../handler"
import { assetCategory } 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 { selectAssetCategorySchema } from "@/v2/db/schema"

export const createAssetCategorySchema = z.object({
name: z.string().min(3).max(32).openapi({
description: "The name of the asset category.",
example: "splash-art",
}),
formattedName: z.string().min(3).max(64).openapi({
description: "The formatted name of the category.",
example: "Splash Art",
}),
})

export const createAssetCategoryResponseSchema = z.object({
success: z.literal(true),
assetCategory: selectAssetCategorySchema,
})

const createAssetCategoryRoute = createRoute({
path: "/create",
method: "post",
summary: "Create a category",
description: "Create a new category.",
tags: ["Category"],
request: {
body: {
content: {
"application/json": {
schema: createAssetCategorySchema,
},
},
},
},
responses: {
200: {
description: "Returns the new category.",
content: {
"application/json": {
schema: createAssetCategoryResponseSchema,
},
},
},
...GenericResponses,
},
})

export const CreateCategoryRoute = (handler: AppHandler) => {
handler.openapi(createAssetCategoryRoute, 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 [categoryExists] = await drizzle
.select({ name: assetCategory.name })
.from(assetCategory)
.where(eq(assetCategory.name, name))

if (categoryExists) {
return ctx.json(
{
success: false,
message: "Category already exists",
},
400
)
}

const [newCategory] = await drizzle
.insert(assetCategory)
.values({
id: name,
name,
formattedName,
lastUpdated: new Date().toISOString(),
})
.returning()

return ctx.json(
{
success: true,
assetCategory: newCategory,
},
200
)
})
}
91 changes: 91 additions & 0 deletions src/v2/routes/category/delete-category.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 { assetCategory } 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 deleteAssetCategorySchema = z.object({
id: z.string().openapi({
param: {
name: "id",
in: "path",
description: "The ID of the category to delete.",
example: "splash-art",
required: true,
},
}),
})

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

const deleteCategoryRoute = createRoute({
path: "/{id}/delete",
method: "delete",
summary: "Delete a category",
description: "Delete a category.",
tags: ["Category"],
request: {
params: deleteAssetCategorySchema,
},
responses: {
200: {
description: "Returns boolean indicating success.",
content: {
"application/json": {
schema: deleteAssetCategoryResponseSchema,
},
},
},
...GenericResponses,
},
})

export const DeleteAssetCategoryRoute = (handler: AppHandler) => {
handler.openapi(deleteCategoryRoute, async (ctx) => {
const id = ctx.req.valid("param").id

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

const [foundCategory] = await drizzle
.select({ id: assetCategory.id })
.from(assetCategory)
.where(eq(assetCategory.id, id))

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

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(assetCategory).where(eq(assetCategory.id, id))

return ctx.json(
{
success: true,
},
200
)
})
}
78 changes: 78 additions & 0 deletions src/v2/routes/category/get-category.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 { assetCategory } from "@/v2/db/schema"
import { getConnection } from "@/v2/db/turso"
import { eq } from "drizzle-orm"
import { z } from "@hono/zod-openapi"
import { selectAssetCategorySchema } from "@/v2/db/schema"
import { GenericResponses } from "@/v2/lib/response-schemas"

const getAssetCategoryByIdSchema = z.object({
id: z.string().openapi({
param: {
name: "id",
in: "path",
description: "The ID of the category to retrieve.",
example: "splash-art",
required: true,
},
}),
})

const getAssetCategoryByIdResponseSchema = z.object({
success: z.literal(true),
category: selectAssetCategorySchema,
})

const getAssetCategoryByIdRoute = createRoute({
path: "/{id}",
method: "get",
summary: "Get a category",
description: "Get a category by their ID.",
tags: ["Category"],
request: {
params: getAssetCategoryByIdSchema,
},
responses: {
200: {
description: "Category was found.",
content: {
"application/json": {
schema: getAssetCategoryByIdResponseSchema,
},
},
},
...GenericResponses,
},
})

export const GetCategoryByIdRoute = (handler: AppHandler) => {
handler.openapi(getAssetCategoryByIdRoute, async (ctx) => {
const id = ctx.req.valid("param").id

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

const [foundCategory] = await drizzle
.select()
.from(assetCategory)
.where(eq(assetCategory.id, id))

if (!foundCategory) {
return ctx.json(
{
success: false,
message: "Category not found",
},
400
)
}

return ctx.json(
{
success: true,
category: foundCategory,
},
200
)
})
}
16 changes: 16 additions & 0 deletions src/v2/routes/category/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AllCategoriesRoute } from "./all-categories"
import { CreateCategoryRoute } from "./create-category"
import { GetCategoryByIdRoute } from "./get-category"
import { ModifyAssetCategoryRoute } from "./modify-category"
import { DeleteAssetCategoryRoute } from "./delete-category"

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

AllCategoriesRoute(handler)
GetCategoryByIdRoute(handler)
CreateCategoryRoute(handler)
ModifyAssetCategoryRoute(handler)
DeleteAssetCategoryRoute(handler)

export default handler
Loading

0 comments on commit 63f191a

Please sign in to comment.