From b83902e3476a71b86126eaeaacc8dbc885ef80a0 Mon Sep 17 00:00:00 2001 From: Penumarthi Navaneeth Date: Fri, 18 Oct 2024 09:39:55 +0530 Subject: [PATCH] Code Improvements: Feature/ Add series to link related articles --- server/api/router/series.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/server/api/router/series.ts b/server/api/router/series.ts index e4da3bef..0df3ac37 100644 --- a/server/api/router/series.ts +++ b/server/api/router/series.ts @@ -2,7 +2,7 @@ import { TRPCError } from "@trpc/server"; import { createTRPCRouter, protectedProcedure } from "../trpc"; import { series, post } from "@/server/db/schema"; import { UpdateSeriesSchema } from "@/schema/series"; -import {eq} from "drizzle-orm"; +import {eq, and} from "drizzle-orm"; export const seriesRouter = createTRPCRouter({ update: protectedProcedure .input(UpdateSeriesSchema) @@ -47,7 +47,10 @@ export const seriesRouter = createTRPCRouter({ columns: { id: true }, - where: (series, { eq }) => eq(series.title, seriesTitle), + where: (series, { eq, and }) => and( + eq(series.title, seriesTitle), + eq(series.userId, ctx.session.user.id) + ), }) if(!currSeries){ @@ -80,13 +83,18 @@ export const seriesRouter = createTRPCRouter({ where: (post, { eq, and, ne }) => and ( ne(post.id, currentPost.id), - eq(post.seriesId, currentPost.seriesId!) + eq(post.seriesId, seriesId) ) }) // if another post with the same seriesId is present, then do nothing // else remove the series from the series table if(!anotherPostInThisSeries){ - await tx.delete(series).where(eq(series.id, seriesId)); + await tx.delete(series).where( + and( + eq(series.id, seriesId), + eq(series.userId, ctx.session.user.id) + ) + ); } // update that series id in the current post await tx