Merge branch 'dev'

This commit is contained in:
Atridad Lahiji 2023-08-20 16:53:17 -06:00
commit 91aa3909ea
No known key found for this signature in database
3 changed files with 14 additions and 19 deletions

View file

@ -20,7 +20,7 @@ export const roomRouter = createTRPCRouter({
const room = await ctx.db const room = await ctx.db
.insert(rooms) .insert(rooms)
.values({ .values({
id: createId(), id: `room_${createId()}`,
userId: ctx.auth.userId, userId: ctx.auth.userId,
roomName: input.name, roomName: input.name,
storyName: "First Story!", storyName: "First Story!",
@ -119,7 +119,7 @@ export const roomRouter = createTRPCRouter({
oldRoom && oldRoom &&
(await ctx.db.insert(logs).values({ (await ctx.db.insert(logs).values({
id: createId(), id: `log_${createId()}`,
userId: ctx.auth.userId, userId: ctx.auth.userId,
roomId: input.roomId, roomId: input.roomId,
scale: oldRoom.scale, scale: oldRoom.scale,

View file

@ -37,31 +37,24 @@ export const voteRouter = createTRPCRouter({
set: protectedProcedure set: protectedProcedure
.input(z.object({ value: z.string(), roomId: z.string() })) .input(z.object({ value: z.string(), roomId: z.string() }))
.mutation(async ({ ctx, input }) => { .mutation(async ({ ctx, input }) => {
const updateResult = await ctx.db const upsertResult = await ctx.db
.update(votes) .insert(votes)
.set({ .values({
id: `vote_${createId()}`,
value: input.value, value: input.value,
userId: ctx.auth.userId, userId: ctx.auth.userId,
roomId: input.roomId, roomId: input.roomId,
}) })
.where(eq(votes.userId, ctx.auth.userId)) .onConflictDoUpdate({
.returning(); target: [votes.userId, votes.roomId],
set: {
let success = updateResult.length > 0;
if (!success) {
const vote = await ctx.db
.insert(votes)
.values({
id: createId(),
value: input.value, value: input.value,
userId: ctx.auth.userId, userId: ctx.auth.userId,
roomId: input.roomId, roomId: input.roomId,
}) },
.returning(); });
success = vote.length > 0; let success = upsertResult.rowCount > 0;
}
if (success) { if (success) {
await invalidateCache(`kv_votecount`); await invalidateCache(`kv_votecount`);

View file

@ -5,6 +5,7 @@ import {
boolean, boolean,
json, json,
index, index,
unique,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm"; import { relations } from "drizzle-orm";
@ -37,6 +38,7 @@ export const votes = pgTable(
(table) => { (table) => {
return { return {
userIdx: index("user_idx").on(table.userId), userIdx: index("user_idx").on(table.userId),
unq: unique().on(table.userId, table.roomId),
}; };
} }
); );