Skip to content

Commit

Permalink
request route impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 4ec3163 commit 9a3320b
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 114 deletions.
51 changes: 31 additions & 20 deletions src/v2/routes/requests/all-requests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { RequestFormManager } from "@/v2/lib/managers/request-form/request-form-manager"
import { AppHandler } from "../handler"
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 { selectRequestFormSchema } from "@/v2/db/schema"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"

const viewAllRequestsSchema = z
.object({
Expand Down Expand Up @@ -32,7 +32,7 @@ const viewAllRequestsResponseSchema = z.object({
})

const getAllRequestsRoute = createRoute({
path: "/",
path: "/all",
method: "get",
description:
"Get all requests. This will also return all associated upvotes count.",
Expand All @@ -53,26 +53,37 @@ const getAllRequestsRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const AllRequestsRoute = (handler: AppHandler) => {
handler.openapi(getAllRequestsRoute, async (ctx) => {
const { offset } = ctx.req.valid("query") ?? { offset: "0" }

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

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

const requestFormManager = new RequestFormManager(drizzle)
if (!user || user.role != "creator" || user.plan == "supporter") {
return ctx.json(
{
success: false,
message: "Unauthorized. Only supporters can view requests.",
},
401
)
}

const allRequests = await requestFormManager.getRequestFormEntries(
parseInt(offset)
)
const { drizzle } = await getConnection(ctx.env)

return ctx.json(
{
success: true,
requests: allRequests,
},
200
)
})
const allRequests = await drizzle.query.requestForm.findMany({
offset: parseInt(offset),
limit: 50,
})

export default handler
return ctx.json(
{
success: true,
requests: allRequests,
},
200
)
})
}
72 changes: 35 additions & 37 deletions src/v2/routes/requests/create-request.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { RequestFormManager } from "@/v2/lib/managers/request-form/request-form-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 { selectRequestFormSchema } from "@/v2/db/schema"
import { requestForm, selectRequestFormSchema } from "@/v2/db/schema"
import type { requestArea } from "@/v2/db/schema"

const createRequestFormEntrySchema = z.object({
Expand Down Expand Up @@ -35,7 +34,7 @@ const createRequestFormEntryResponse = z.object({
})

const createRequestFormEntryRoute = createRoute({
path: "/",
path: "/create",
method: "post",
description: "Create a new entry into the request form.",
tags: ["Requests"],
Expand All @@ -61,44 +60,43 @@ const createRequestFormEntryRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const CreateRequestFormEntryRoute = (handler: AppHandler) => {
handler.openapi(createRequestFormEntryRoute, async (ctx) => {
const { area, title, description } = ctx.req.valid("json")

handler.openapi(createRequestFormEntryRoute, async (ctx) => {
const { area, title, description } = ctx.req.valid("json")
const authSessionManager = new AuthSessionManager(ctx)

const authSessionManager = new AuthSessionManager(ctx)
const { user } = await authSessionManager.validateSession()

const { user } = await authSessionManager.validateSession()
if (!user || user.role != "creator" || user.plan == "supporter") {
return ctx.json(
{
success: false,
message:
"Unauthorized. Only supporters can create request entries.",
},
401
)
}

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

const [newRequestEntry] = await drizzle
.insert(requestForm)
.values({
userId: user.id,
title: title,
area: area,
description: description,
})
.returning()

if (!user || user.role != "creator" || user.plan == "supporter") {
return ctx.json(
{
success: false,
message:
"Unauthorized. Only supporters can create request entries.",
success: true,
response: newRequestEntry,
},
401
200
)
}

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

const requestFormManager = new RequestFormManager(drizzle)

const [newRequestEntry] = await requestFormManager.createRequestFormEntry(
user.id,
title,
area,
description
)

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

export default handler
})
}
104 changes: 53 additions & 51 deletions src/v2/routes/requests/delete-request.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import { AppHandler } from "../handler"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
import { RequestFormManager } from "@/v2/lib/managers/request-form/request-form-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 { requestForm } from "@/v2/db/schema"
import { eq } from "drizzle-orm"

export const deleteRequestByIdSchema = z.object({
id: z.string().openapi({
Expand All @@ -23,7 +24,7 @@ export const deleteRequestByIdResponseSchema = z.object({
})

const deleteRequestByIdRoute = createRoute({
path: "/{id}",
path: "/{id}/delete",
method: "delete",
description:
"Delete a request by its ID. This will also delete all associated upvotes.",
Expand All @@ -44,60 +45,61 @@ const deleteRequestByIdRoute = createRoute({
},
})

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()
export const DeleteRequestByIdRoute = (handler: AppHandler) => {
handler.openapi(deleteRequestByIdRoute, async (ctx) => {
const requestId = ctx.req.valid("param").id

handler.openapi(deleteRequestByIdRoute, async (ctx) => {
const requestId = ctx.req.valid("param").id
const authSessionManager = new AuthSessionManager(ctx)

const authSessionManager = new AuthSessionManager(ctx)
const { user } = await authSessionManager.validateSession()

const { user } = await authSessionManager.validateSession()

if (!user || user.role != "creator" || user.plan == "supporter") {
return ctx.json(
{
success: false,
message: "Unauthorized. Only supporters can delete requests.",
},
401
)
}

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

const requestFormManager = new RequestFormManager(drizzle)

const request =
await requestFormManager.doesRequestFormEntryExist(requestId)
if (!user || user.role != "creator" || user.plan == "supporter") {
return ctx.json(
{
success: false,
message:
"Unauthorized. Only supporters can delete requests.",
},
401
)
}

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

const [request] = await drizzle
.select({ id: requestForm.id, userId: requestForm.userId })
.from(requestForm)
.where(eq(requestForm.id, requestId))
.limit(1)

if (!request) {
return ctx.json(
{
success: false,
message: "Request by ID not found",
},
404
)
}

if (request.userId != user.id) {
return ctx.json(
{
success: false,
message:
"Unauthorized. You can only delete your own requests.",
},
401
)
}

if (!request) {
return ctx.json(
{
success: false,
message: "Request by ID not found",
},
404
)
}
await drizzle.delete(requestForm).where(eq(requestForm.id, requestId))

if (request.userId != user.id) {
return ctx.json(
{
success: false,
message: "Unauthorized. You can only delete your own requests.",
success: true,
},
401
200
)
}

await requestFormManager.deleteRequestFormEntry(requestId)

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

export default handler
})
}
20 changes: 14 additions & 6 deletions src/v2/routes/requests/handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { OpenAPIHono } from "@hono/zod-openapi"
import ViewAllRequestsRoute from "./all-requests"
import CreateRequestRoute from "./create-request"
import DeleteRequestRoute from "./delete-request"
import { AllRequestsRoute } from "./all-requests"
import { DeleteRequestByIdRoute } from "./delete-request"
import { CreateRequestFormEntryRoute } from "./create-request"
import { UpvoteRequestRoute } from "./upvote-request"
import { ViewRequestRoute } from "./view-request"
import { RemoveRequestUpvoteRoute } from "./remove-request-upvote"

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

handler.route("/view/all", ViewAllRequestsRoute)
handler.route("/create", CreateRequestRoute)
handler.route("/delete", DeleteRequestRoute)
AllRequestsRoute(handler)
ViewRequestRoute(handler)

UpvoteRequestRoute(handler)
RemoveRequestUpvoteRoute(handler)

CreateRequestFormEntryRoute(handler)
DeleteRequestByIdRoute(handler)

export default handler
Loading

0 comments on commit 9a3320b

Please sign in to comment.