Skip to content

Commit

Permalink
holy fuck
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent f8e48c1 commit 242492c
Show file tree
Hide file tree
Showing 10 changed files with 500 additions and 508 deletions.
54 changes: 26 additions & 28 deletions src/v2/routes/asset/delete-asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { type Handler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { eq } from "drizzle-orm"
import { asset } from "@/v2/db/schema"
Expand All @@ -23,7 +23,7 @@ const deleteAssetByIdResponseSchema = z.object({
})

const deleteAssetByIdRoute = createRoute({
path: "/{id}",
path: "/{id}/delete",
method: "delete",
description:
"Delete an asset from their ID. Must be the owner of the asset or an admin.",
Expand All @@ -44,38 +44,36 @@ const deleteAssetByIdRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const DeleteAssetByIdRoute = (handler: Handler) => {
handler.openapi(deleteAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

handler.openapi(deleteAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id
const { drizzle } = await getConnection(ctx.env)

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

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)

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

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
})
}
74 changes: 36 additions & 38 deletions src/v2/routes/asset/get-asset-likes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { getConnection } from "@/v2/db/turso"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { createRoute } from "@hono/zod-openapi"
Expand All @@ -10,6 +9,7 @@ import {
selectAssetTagSchema,
selectAssetLikesSchema,
} from "@/v2/db/schema"
import { Handler } from "../handler"

const allAssetLikesSchema = z.object({
success: z.literal(true),
Expand All @@ -27,7 +27,7 @@ const allAssetLikesSchema = z.object({
})

const allAssetLikesRoute = createRoute({
path: "/",
path: "/likes",
method: "get",
description: "All your liked assets.",
tags: ["Asset"],
Expand All @@ -44,47 +44,45 @@ const allAssetLikesRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const GetAssetLikesRoute = (handler: Handler) => {
handler.openapi(allAssetLikesRoute, async (ctx) => {
const authSessionManager = new AuthSessionManager(ctx)
const { user } = await authSessionManager.validateSession()

handler.openapi(allAssetLikesRoute, async (ctx) => {
const authSessionManager = new AuthSessionManager(ctx)
const { user } = await authSessionManager.validateSession()

if (!user) {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}
if (!user) {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}

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

const likes = await drizzle.query.assetLikes.findMany({
where: (assetLikes, { eq }) => eq(assetLikes.likedById, user.id),
with: {
asset: {
with: {
assetTagAsset: {
with: {
assetTag: true,
const likes = await drizzle.query.assetLikes.findMany({
where: (assetLikes, { eq }) => eq(assetLikes.likedById, user.id),
with: {
asset: {
with: {
assetTagAsset: {
with: {
assetTag: true,
},
},
},
},
},
},
offset: 0,
})

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

export default handler
return ctx.json(
{
success: true,
likes,
},
200
)
})
}
104 changes: 51 additions & 53 deletions src/v2/routes/asset/get-asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { Handler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { asset } from "@/v2/db/schema"
import { eq, sql } from "drizzle-orm"
Expand Down Expand Up @@ -74,65 +74,63 @@ const getAssetByIdRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const GetAssetByIdRoute = (handler: Handler) => {
handler.openapi(getAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

handler.openapi(getAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id
const { drizzle } = await getConnection(ctx.env)

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,
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,
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,
},
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)))

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

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
})
}
35 changes: 16 additions & 19 deletions src/v2/routes/asset/handler.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import { OpenAPIHono } from "@hono/zod-openapi"

import SearchAssetRoute from "@/v2/routes/asset/search-assets"
import GetAssetRoute from "@/v2/routes/asset/get-asset"

import UploadAssetRoute from "@/v2/routes/asset/upload-asset"
import ModifyAssetRoute from "@/v2/routes/asset/modify-asset"
import DeleteAssetRoute from "@/v2/routes/asset/delete-asset"

import LikeAssetByIdRoute from "@/v2/routes/asset/like-asset"
import UnlikeAssetByIdRoute from "@/v2/routes/asset/unlike-asset"

import GetAssetLikesRoute from "@/v2/routes/asset/get-asset-likes"
import { GetAssetByIdRoute } from "./get-asset"
import { LikeAssetByIdRoute } from "./like-asset"
import { UnlikeAssetByIdRoute } from "./unlike-asset"
import { AssetSearchAllFilterRoute } from "./search-assets"
import { GetAssetLikesRoute } from "./get-asset-likes"
import { ModifyAssetRoute } from "./modify-asset"
import { UploadAssetRoute } from "./upload-asset"
import { DeleteAssetByIdRoute } from "./delete-asset"

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

handler.route("/search", SearchAssetRoute)
handler.route("/get", GetAssetRoute)
AssetSearchAllFilterRoute(handler)
UploadAssetRoute(handler)

handler.route("/upload", UploadAssetRoute)
handler.route("/modify", ModifyAssetRoute)
handler.route("/delete", DeleteAssetRoute)
GetAssetByIdRoute(handler)
ModifyAssetRoute(handler)
DeleteAssetByIdRoute(handler)

handler.route("/like", LikeAssetByIdRoute)
handler.route("/unlike", UnlikeAssetByIdRoute)
UnlikeAssetByIdRoute(handler)
LikeAssetByIdRoute(handler)

handler.route("/likes", GetAssetLikesRoute)
GetAssetLikesRoute(handler)

export default handler
Loading

0 comments on commit 242492c

Please sign in to comment.