Skip to content

Commit

Permalink
request form seeding & view all route
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Feb 11, 2024
1 parent f8dc2ad commit 816cee5
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 10 deletions.
46 changes: 46 additions & 0 deletions src/scripts/seed/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
userFavorite,
userFavoriteAsset,
userFollowing,
requestFormUpvotes,
requestForm,
} from "@/v2/db/schema"
import { Scrypt } from "lucia"
import "dotenv/config"
Expand Down Expand Up @@ -53,6 +55,7 @@ async function main() {
bio: "test bio",
role: "creator",
isContributor: true,
isSupporter: true,
},
{
username: "testuser2",
Expand All @@ -70,6 +73,7 @@ async function main() {
bio: "test bio 3",
role: "uploader",
isContributor: false,
isSupporter: true,
},
])
.returning()
Expand Down Expand Up @@ -115,6 +119,48 @@ async function main() {
`[SEED] [userFollowing] inserted ${newuserFollowing.length} rows\n`
)

console.log("[SEED] [requestForm] Seeding request forms...")
const newRequestForms = await db
.insert(requestForm)
.values([
{
userId: newUsers[0].id,
title: "test request",
area: "game",
description: "test description",
},
{
userId: newUsers[1].id,
title: "test request 2",
area: "game",
description: "test description 2",
},
])
.returning()

console.log(
`[SEED] [requestForm] inserted ${newRequestForms.length} rows\n`
)

console.log("[SEED] [requestFormUpvotes] Seeding request form upvotes...")
const newRequestFormUpvotes = await db
.insert(requestFormUpvotes)
.values([
{
requestFormId: newRequestForms[0].id,
userId: newUsers[1].id,
},
{
requestFormId: newRequestForms[1].id,
userId: newUsers[0].id,
},
])
.returning()

console.log(
`[SEED] [requestFormUpvotes] inserted ${newRequestFormUpvotes.length} rows\n`
)

console.log("[SEED] [assetTag] Seeding asset tags...")
const newAssetTags = await db
.insert(assetTag)
Expand Down
24 changes: 14 additions & 10 deletions src/v2/lib/managers/request-form/request-form-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DrizzleInstance } from "@/v2/db/turso"
import { and, eq, sql } from "drizzle-orm"
import { and, eq } from "drizzle-orm"
import { requestArea, requestForm, requestFormUpvotes } from "@/v2/db/schema"

export class RequestFormManager {
Expand All @@ -10,15 +10,19 @@ export class RequestFormManager {
return await this.drizzle.query.requestForm.findMany({
offset: offset,
limit: limit,
with: {
requestFormUpvotes,
},
extras: {
upvoteCount:
sql`SELECT COUNT(*) FROM ${requestFormUpvotes} WHERE ${requestForm.id} = ${requestFormUpvotes.requestFormId}`.as(
"upvoteCount"
),
},
// with: {
// requestFormUpvotes: {
// columns: {
// userId: true,
// },
// },
// },
// extras: {
// upvoteCount:
// sql`SELECT COUNT(*) FROM ${requestFormUpvotes} WHERE ${requestFormUpvotes.requestFormId} = ${requestForm.id}`.as(
// "upvoteCount"
// ),
// },
})
} catch (e) {
console.error(`Error getting request form entries`, e)
Expand Down
25 changes: 25 additions & 0 deletions src/v2/routes/requests/form/view/all/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createRoute } from "@hono/zod-openapi"
import { viewAllRequestsResponseSchema, viewAllRequestsSchema } from "./schema"
import { GenericResponses } from "@/v2/lib/response-schemas"

export const getAllRequestsRoute = createRoute({
path: "/",
method: "get",
description:
"Get all requests. This will also return all associated upvotes count.",
tags: ["Requests"],
request: {
query: viewAllRequestsSchema,
},
responses: {
200: {
description: "List of all submitted requests.",
content: {
"application/json": {
schema: viewAllRequestsResponseSchema,
},
},
},
...GenericResponses,
},
})
28 changes: 28 additions & 0 deletions src/v2/routes/requests/form/view/all/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { getAllRequestsRoute } from "./openapi"
import { RequestFormManager } from "@/v2/lib/managers/request-form/request-form-manager"
import { getConnection } from "@/v2/db/turso"

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

handler.openapi(getAllRequestsRoute, async (ctx) => {
const { offset } = ctx.req.valid("query") ?? { offset: "0" }

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

const requestFormManager = new RequestFormManager(drizzle)

const allRequests = await requestFormManager.getRequestFormEntries(
parseInt(offset)
)

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

export default handler
27 changes: 27 additions & 0 deletions src/v2/routes/requests/form/view/all/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { z } from "@hono/zod-openapi"
import { selectRequestFormSchema } from "@/v2/db/schema"

export const viewAllRequestsSchema = z
.object({
offset: z.string().openapi({
param: {
description:
"The offset of requests to return. This is used for pagination.",
name: "offset",
example: "0",
in: "query",
required: false,
},
}),
})
.partial()

const requestFormSchema = z.object({
...selectRequestFormSchema.shape,
upvotesCount: z.number().optional(),
})

export const viewAllRequestsResponseSchema = z.object({
success: z.literal(true),
requests: z.array(requestFormSchema),
})
2 changes: 2 additions & 0 deletions src/v2/routes/requests/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import RequestFormCreateRoute from "@/v2/routes/requests/form/create/route"
import RequestFormDeleteRoute from "@/v2/routes/requests/form/delete/[id]/route"
import ViewAllRequestsRoute from "@/v2/routes/requests/form/view/all/route"

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

handler.route("/create/form", RequestFormCreateRoute)
handler.route("/delete/form", RequestFormDeleteRoute)
handler.route("/view/all", ViewAllRequestsRoute)

export default handler

0 comments on commit 816cee5

Please sign in to comment.