-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c0571b
commit efa04d4
Showing
14 changed files
with
172 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
import { z } from "zod"; | ||
import { shurtles } from "~/db/schema"; | ||
import { limitOrThrow, rateLimits } from "~/utils/ratelimiter"; | ||
import { protectedProcedure } from "../../trpc"; | ||
|
||
export const createShurtleRouter = protectedProcedure | ||
.input( | ||
z.object({ | ||
url: z.string().url({ message: "Must be a valid URL!" }), | ||
slug: z | ||
.string({ required_error: "Cannot be empty!" }) | ||
.regex(/^[a-z0-9](-?[a-z0-9])*$/, { | ||
message: "Invalid slug!", | ||
}), | ||
}) | ||
) | ||
.mutation(async ({ input, ctx }) => { | ||
const userId = ctx.auth.userId; | ||
|
||
await limitOrThrow(rateLimits.create, userId); | ||
|
||
try { | ||
await ctx.db.insert(shurtles).values({ | ||
slug: input.slug, | ||
url: input.url, | ||
creatorId: ctx.auth.userId, | ||
}); | ||
} catch { | ||
throw new TRPCError({ | ||
code: "CONFLICT", | ||
message: `Shurtle for slug ${input.slug} already exists.`, | ||
}); | ||
} | ||
|
||
const shurtle = await ctx.db.query.shurtles.findFirst({ | ||
where: (shurtles, { eq }) => eq(shurtles.slug, input.slug), | ||
}); | ||
|
||
return shurtle; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
import { and, eq } from "drizzle-orm"; | ||
import { z } from "zod"; | ||
import { shurtles } from "~/db/schema"; | ||
import { limitOrThrow, rateLimits } from "~/utils/ratelimiter"; | ||
import { createTRPCRouter, protectedProcedure } from "../../trpc"; | ||
|
||
export const deleteShurtleRouter = createTRPCRouter({ | ||
bySlug: protectedProcedure | ||
.input( | ||
z.object({ | ||
slug: z | ||
.string({ required_error: "Cannot be empty!" }) | ||
.regex(/^[a-z0-9](-?[a-z0-9])*$/, { | ||
message: "Invalid slug!", | ||
}), | ||
}) | ||
) | ||
.mutation(async ({ input, ctx }) => { | ||
const userId = ctx.auth.userId; | ||
|
||
await limitOrThrow(rateLimits.private, userId); | ||
|
||
const result = await ctx.db | ||
.delete(shurtles) | ||
.where( | ||
and(eq(shurtles.slug, input.slug), eq(shurtles.creatorId, userId)) | ||
); | ||
|
||
if (result.rowsAffected < 1) { | ||
throw new TRPCError({ | ||
code: "NOT_FOUND", | ||
message: `Shurtle for slug ${input.slug} and creator id ${userId} does not exist.`, | ||
}); | ||
} | ||
}), | ||
}); |
Oops, something went wrong.