Skip to content

Commit

Permalink
rewrite route structure
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent f0e4114 commit f8e48c1
Show file tree
Hide file tree
Showing 142 changed files with 1,848 additions and 2,148 deletions.
81 changes: 81 additions & 0 deletions src/v2/routes/asset/delete-asset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { getConnection } from "@/v2/db/turso"
import { eq } from "drizzle-orm"
import { asset } from "@/v2/db/schema"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"

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

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

const deleteAssetByIdRoute = createRoute({
path: "/{id}",
method: "delete",
description:
"Delete an asset from their ID. Must be the owner of the asset or an admin.",
tags: ["Asset"],
request: {
params: deleteAssetByIdSchema,
},
responses: {
200: {
description: "True if the asset was deleted.",
content: {
"application/json": {
schema: deleteAssetByIdResponseSchema,
},
},
},
...GenericResponses,
},
})

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

handler.openapi(deleteAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

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

const [existingAsset] = await drizzle
.select({ id: asset.id })
.from(asset)
.where(eq(asset.id, parseInt(assetId)))
.limit(1)

if (!existingAsset) {
return ctx.json(
{
success: true,
message: "Asset not found",
},
400
)
}

await drizzle.delete(asset).where(eq(asset.id, parseInt(assetId)))
// await ctx.env.FILES_BUCKET.delete(asset.url)

return ctx.json(
{
success: true,
},
200
)
})

export default handler
25 changes: 0 additions & 25 deletions src/v2/routes/asset/delete/id/[id]/openapi.ts

This file was deleted.

41 changes: 0 additions & 41 deletions src/v2/routes/asset/delete/id/[id]/route.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/v2/routes/asset/delete/id/[id]/schema.ts

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { allAssetLikesRoute } from "./openapi"
import { getConnection } from "@/v2/db/turso"
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 {
selectAssetSchema,
selectAssetTagAssetSchema,
selectAssetTagSchema,
selectAssetLikesSchema,
} from "@/v2/db/schema"

const allAssetLikesSchema = z.object({
success: z.literal(true),
likes: z.array(
selectAssetLikesSchema.extend({
asset: selectAssetSchema.extend({
assetTagAsset: z.array(
selectAssetTagAssetSchema.extend({
assetTag: selectAssetTagSchema,
})
),
}),
})
),
})

const allAssetLikesRoute = createRoute({
path: "/",
method: "get",
description: "All your liked assets.",
tags: ["Asset"],
responses: {
200: {
description: "Array of your liked assets.",
content: {
"application/json": {
schema: allAssetLikesSchema,
},
},
},
...GenericResponses,
},
})

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

Expand Down
138 changes: 138 additions & 0 deletions src/v2/routes/asset/get-asset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { getConnection } from "@/v2/db/turso"
import { asset } from "@/v2/db/schema"
import { eq, sql } from "drizzle-orm"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"
import {
selectAssetCategorySchema,
selectGameSchema,
selectAssetSchema,
selectAssetTagAssetSchema,
selectAssetTagSchema,
selectUserSchema,
} from "@/v2/db/schema"

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

const getAssetByIdResponseSchema = z.object({
success: z.literal(true),
// mmm nested schemas
asset: selectAssetSchema.extend({
assetTagAsset: z.array(
selectAssetTagAssetSchema.extend({
assetTag: selectAssetTagSchema,
})
),
}),
authUser: selectUserSchema.pick({
id: true,
avatarUrl: true,
displayName: true,
username: true,
usernameColour: true,
pronouns: true,
verified: true,
bio: true,
dateJoined: true,
plan: true,
role: true,
}),
game: selectGameSchema,
assetCategory: selectAssetCategorySchema,
// similarAssets: selectAssetSchema.array(),
})

const getAssetByIdRoute = createRoute({
path: "/{id}",
method: "get",
description: "Get an asset by their ID.",
tags: ["Asset"],
request: {
params: getAssetByIdSchema,
},
responses: {
200: {
description: "The found asset & similar assets are returned.",
content: {
"application/json": {
schema: getAssetByIdResponseSchema,
},
},
},
...GenericResponses,
},
})

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

handler.openapi(getAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

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

const foundAsset = await drizzle.query.asset.findFirst({
where: (asset, { eq }) => eq(asset.id, parseInt(assetId)),
with: {
assetTagAsset: {
with: {
assetTag: true,
},
},
authUser: {
columns: {
id: true,
avatarUrl: true,
displayName: true,
username: true,
usernameColour: true,
pronouns: true,
verified: true,
bio: true,
dateJoined: true,
plan: true,
role: true,
},
},
game: true,
assetCategory: true,
},
})

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

await drizzle
.update(asset)
.set({
viewCount: sql`${asset.viewCount} + 1`,
})
.where(eq(asset.id, parseInt(assetId)))

return ctx.json(
{
success: true,
asset: foundAsset,
},
200
)
})

export default handler
8 changes: 0 additions & 8 deletions src/v2/routes/asset/get/handler.ts

This file was deleted.

24 changes: 0 additions & 24 deletions src/v2/routes/asset/get/id/[id]/openapi.ts

This file was deleted.

Loading

0 comments on commit f8e48c1

Please sign in to comment.