Skip to content

Commit

Permalink
convert game handler import
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 242492c commit e0d9e02
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 241 deletions.
4 changes: 2 additions & 2 deletions src/v2/routes/asset/delete-asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Handler } from "../handler"
import { type AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { eq } from "drizzle-orm"
import { asset } from "@/v2/db/schema"
Expand Down Expand Up @@ -44,7 +44,7 @@ const deleteAssetByIdRoute = createRoute({
},
})

export const DeleteAssetByIdRoute = (handler: Handler) => {
export const DeleteAssetByIdRoute = (handler: AppHandler) => {
handler.openapi(deleteAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/asset/get-asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Handler } from "../handler"
import { AppHandler } 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,7 +74,7 @@ const getAssetByIdRoute = createRoute({
},
})

export const GetAssetByIdRoute = (handler: Handler) => {
export const GetAssetByIdRoute = (handler: AppHandler) => {
handler.openapi(getAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/asset/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ GetAssetByIdRoute(handler)
ModifyAssetRoute(handler)
DeleteAssetByIdRoute(handler)

UnlikeAssetByIdRoute(handler)
LikeAssetByIdRoute(handler)
UnlikeAssetByIdRoute(handler)

GetAssetLikesRoute(handler)

Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/asset/like-asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Handler } from "../handler"
import { type AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { asset, assetLikes } from "@/v2/db/schema"
Expand Down Expand Up @@ -44,7 +44,7 @@ const likeAssetByIdRoute = createRoute({
},
})

export const LikeAssetByIdRoute = (handler: Handler) => {
export const LikeAssetByIdRoute = (handler: AppHandler) => {
handler.openapi(likeAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/asset/modify-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SplitQueryByCommas } from "@/v2/lib/helpers/split-query-by-commas"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"
import { Handler } from "../handler"
import { AppHandler } from "../handler"

const modifyAssetPathSchema = z.object({
id: z.string().openapi({
Expand Down Expand Up @@ -84,7 +84,7 @@ const modifyAssetRoute = createRoute({
},
})

export const ModifyAssetRoute = (handler: Handler) => {
export const ModifyAssetRoute = (handler: AppHandler) => {
handler.openapi(modifyAssetRoute, async (ctx) => {
const { name, tags, assetCategoryId, gameId } = ctx.req.valid("json")
const { id } = ctx.req.valid("param")
Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/asset/search-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
selectAssetTagAssetSchema,
selectAssetTagSchema,
} from "@/v2/db/schema"
import { type Handler } from "../handler"
import { type AppHandler } from "../handler"

export const assetSearchAllFilterResponseSchema = z.object({
success: z.literal(true),
Expand Down Expand Up @@ -100,7 +100,7 @@ const assetSearchAllFilterRoute = createRoute({
},
})

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

Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/asset/unlike-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { and, eq } from "drizzle-orm"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"
import type { Handler } from "../handler"
import type { AppHandler } from "../handler"

const unlikeAssetByIdSchema = z.object({
id: z.string().openapi({
Expand Down Expand Up @@ -44,7 +44,7 @@ const unlikeAssetByIdRoute = createRoute({
},
})

export const UnlikeAssetByIdRoute = (handler: Handler) => {
export const UnlikeAssetByIdRoute = (handler: AppHandler) => {
handler.openapi(unlikeAssetByIdRoute, async (ctx) => {
const assetId = ctx.req.valid("param").id

Expand Down
4 changes: 2 additions & 2 deletions src/v2/routes/asset/upload-asset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Handler } from "../handler"
import type { AppHandler } from "../handler"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { getConnection } from "@/v2/db/turso"
import { SplitQueryByCommas } from "@/v2/lib/helpers/split-query-by-commas"
Expand Down Expand Up @@ -88,7 +88,7 @@ const uploadAssetRoute = createRoute({
},
})

export const UploadAssetRoute = (handler: Handler) =>
export const UploadAssetRoute = (handler: AppHandler) =>
handler.openapi(uploadAssetRoute, async (ctx) => {
const {
asset,
Expand Down
50 changes: 24 additions & 26 deletions src/v2/routes/contributors/all-contributors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { authUser } from "@/v2/db/schema"
import { eq } from "drizzle-orm"
Expand All @@ -20,7 +20,7 @@ const contributorListSchema = z.object({
})

const contributorsRoute = createRoute({
path: "/",
path: "/all",
method: "get",
description: "Get a list of all contributors.",
tags: ["Contributors"],
Expand All @@ -39,29 +39,27 @@ const contributorsRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const AllContributorsRoute = (handler: AppHandler) => {
handler.openapi(contributorsRoute, async (ctx) => {
const { drizzle } = await getConnection(ctx.env)

handler.openapi(contributorsRoute, async (ctx) => {
const { drizzle } = await getConnection(ctx.env)
const contributors = await drizzle
.select({
id: authUser.id,
username: authUser.username,
avatarUrl: authUser.avatarUrl,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
.where(eq(authUser.isContributor, true))

const contributors = await drizzle
.select({
id: authUser.id,
username: authUser.username,
avatarUrl: authUser.avatarUrl,
plan: authUser.plan,
role: authUser.role,
})
.from(authUser)
.where(eq(authUser.isContributor, true))

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

export default handler
return ctx.json(
{
success: true,
contributors,
},
200
)
})
}
4 changes: 2 additions & 2 deletions src/v2/routes/contributors/handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import AllContributorsRoute from "./all-contributors"
import { AllContributorsRoute } from "./all-contributors"

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

handler.route("/all", AllContributorsRoute)
AllContributorsRoute(handler)

export default handler
30 changes: 14 additions & 16 deletions src/v2/routes/game/all-games.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { getConnection } from "@/v2/db/turso"
import { game } from "@/v2/db/schema"
import { GenericResponses } from "@/v2/lib/response-schemas"
Expand Down Expand Up @@ -29,20 +29,18 @@ const getAllGamesRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const AllGamesRoute = (handler: AppHandler) => {
handler.openapi(getAllGamesRoute, async (ctx) => {
const { drizzle } = await getConnection(ctx.env)

handler.openapi(getAllGamesRoute, async (ctx) => {
const { drizzle } = await getConnection(ctx.env)
const games = (await drizzle.select().from(game)) ?? []

const games = (await drizzle.select().from(game)) ?? []

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

export default handler
return ctx.json(
{
success: true,
games,
},
200
)
})
}
96 changes: 47 additions & 49 deletions src/v2/routes/game/create-game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { game } from "@/v2/db/schema"
import { eq } from "drizzle-orm"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
Expand Down Expand Up @@ -36,7 +36,7 @@ export const createGameResponse = z.object({
})

const createGameRoute = createRoute({
path: "/",
path: "/create",
method: "post",
description: "Create a new game.",
tags: ["Game"],
Expand All @@ -62,61 +62,59 @@ const createGameRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const CreateGameRoute = (handler: AppHandler) => {
handler.openapi(createGameRoute, async (ctx) => {
const authSessionManager = new AuthSessionManager(ctx)

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

const { user } = await authSessionManager.validateSession()
if (!user || user.role != "creator") {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}

if (!user || user.role != "creator") {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}
const { name, formattedName, possibleSuggestiveContent } =
ctx.req.valid("json")

const { drizzle } = getConnection(ctx.env)

const { name, formattedName, possibleSuggestiveContent } =
ctx.req.valid("json")
const [gameExists] = await drizzle
.select({ name: game.name })
.from(game)
.where(eq(game.name, name))

const { drizzle } = getConnection(ctx.env)
if (gameExists.name) {
return ctx.json(
{
success: false,
message: "Game already exists",
},
400
)
}

const [gameExists] = await drizzle
.select({ name: game.name })
.from(game)
.where(eq(game.name, name))
const [newGame] = await drizzle
.insert(game)
.values({
id: name,
name,
formattedName,
possibleSuggestiveContent: Boolean(possibleSuggestiveContent),
lastUpdated: new Date().toISOString(),
})
.returning()

if (gameExists.name) {
return ctx.json(
{
success: false,
message: "Game already exists",
success: true,
game: newGame,
},
400
200
)
}

const [newGame] = await drizzle
.insert(game)
.values({
id: name,
name,
formattedName,
possibleSuggestiveContent: Boolean(possibleSuggestiveContent),
lastUpdated: new Date().toISOString(),
})
.returning()

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

export default handler
})
}
Loading

0 comments on commit e0d9e02

Please sign in to comment.