diff --git a/apps/gql/features/pano/post.ts b/apps/gql/features/pano/post.ts index b48adac1..bc315ed8 100644 --- a/apps/gql/features/pano/post.ts +++ b/apps/gql/features/pano/post.ts @@ -33,9 +33,9 @@ function createPanoPostActions({ prisma }: Clients) { }); }; - const update = (userID: string, args: UpdatePanoPostArgs) => { + const update = (id: string, args: UpdatePanoPostArgs) => { return prisma.post.update({ - where: { id: userID }, + where: { id }, data: { title: args.title, url: args.url, @@ -45,8 +45,13 @@ function createPanoPostActions({ prisma }: Clients) { }); }; + const remove = (id: string) => { + return prisma.post.delete({ where: { id } }); + }; + return { create, update, + remove, }; } diff --git a/apps/gql/schema/resolvers/index.ts b/apps/gql/schema/resolvers/index.ts index dd909b73..a38aa437 100644 --- a/apps/gql/schema/resolvers/index.ts +++ b/apps/gql/schema/resolvers/index.ts @@ -234,6 +234,7 @@ export const resolvers = { CreatePanoPostPayload: {}, // union UpdatePanoPostPayload: {}, // union + RemovePanoPostPayload: {}, // union UserError: {}, // interface InvalidInput: errorFieldsResolver, @@ -271,5 +272,22 @@ export const resolvers = { const updated = await actions.pano.post.update(id.value, input); return transformPanoPost(await loaders.pano.post.byID.clear(updated.id).load(updated.id)); }, + removePanoPost: async (_, { input }, { actions, loaders, pasaport: { session } }) => { + if (!session?.user?.id) { + return NotAuthorized(); + } + + const id = parse(input.id); + if (id.type !== "PanoPost") { + return InvalidInput("wrong id"); + } + + const post = await loaders.pano.post.byID.load(id.value); + if (post.userID !== session.user.id) { + return NotAuthorized(); + } + + return transformPanoPost(await actions.pano.post.remove(id.value)); + }, }, } satisfies Resolvers; diff --git a/apps/gql/schema/schema.graphql b/apps/gql/schema/schema.graphql index d30a950a..6488f312 100644 --- a/apps/gql/schema/schema.graphql +++ b/apps/gql/schema/schema.graphql @@ -159,6 +159,7 @@ type PanoCommentEdge { type Mutation { createPanoPost(input: CreatePanoPostInput!): CreatePanoPostPayload updatePanoPost(input: UpdatePanoPostInput!): UpdatePanoPostPayload + removePanoPost(input: RemovePanoPostInput!): RemovePanoPostPayload } input CreatePanoPostInput { @@ -177,3 +178,9 @@ input UpdatePanoPostInput { } union UpdatePanoPostPayload = PanoPost | NotAuthorized | InvalidInput + +input RemovePanoPostInput { + id: ID! +} + +union RemovePanoPostPayload = PanoPost | NotAuthorized | InvalidInput diff --git a/apps/gql/schema/types.generated.ts b/apps/gql/schema/types.generated.ts index a7277d1d..f32a9ab4 100644 --- a/apps/gql/schema/types.generated.ts +++ b/apps/gql/schema/types.generated.ts @@ -46,6 +46,7 @@ export type InvalidInput = UserError & { export type Mutation = { __typename?: "Mutation"; createPanoPost: Maybe; + removePanoPost: Maybe; updatePanoPost: Maybe; }; @@ -53,6 +54,10 @@ export type MutationCreatePanoPostArgs = { input: CreatePanoPostInput; }; +export type MutationRemovePanoPostArgs = { + input: RemovePanoPostInput; +}; + export type MutationUpdatePanoPostArgs = { input: UpdatePanoPostInput; }; @@ -184,6 +189,12 @@ export type QueryUserArgs = { username: InputMaybe; }; +export type RemovePanoPostInput = { + id: Scalars["ID"]["input"]; +}; + +export type RemovePanoPostPayload = InvalidInput | NotAuthorized | PanoPost; + export type SozlukQuery = { __typename?: "SozlukQuery"; term: Maybe; @@ -355,6 +366,10 @@ export type ResolversUnionTypes> = Resol | (InvalidInput & { __typename: "InvalidInput" }) | (NotAuthorized & { __typename: "NotAuthorized" }) | (PanoPost & { __typename: "PanoPost" }); + RemovePanoPostPayload: + | (InvalidInput & { __typename: "InvalidInput" }) + | (NotAuthorized & { __typename: "NotAuthorized" }) + | (PanoPost & { __typename: "PanoPost" }); UpdatePanoPostPayload: | (InvalidInput & { __typename: "InvalidInput" }) | (NotAuthorized & { __typename: "NotAuthorized" }) @@ -400,6 +415,10 @@ export type ResolversTypes = ResolversObject<{ PanoPostFilter: PanoPostFilter; PanoQuery: ResolverTypeWrapper; Query: ResolverTypeWrapper<{}>; + RemovePanoPostInput: RemovePanoPostInput; + RemovePanoPostPayload: ResolverTypeWrapper< + ResolversUnionTypes["RemovePanoPostPayload"] + >; SozlukQuery: ResolverTypeWrapper; SozlukTerm: ResolverTypeWrapper; SozlukTermBody: ResolverTypeWrapper; @@ -438,6 +457,8 @@ export type ResolversParentTypes = ResolversObject<{ PanoPostEdge: PanoPostEdge; PanoQuery: PanoQuery; Query: {}; + RemovePanoPostInput: RemovePanoPostInput; + RemovePanoPostPayload: ResolversUnionTypes["RemovePanoPostPayload"]; SozlukQuery: SozlukQuery; SozlukTerm: SozlukTerm; SozlukTermBody: SozlukTermBody; @@ -496,6 +517,12 @@ export type MutationResolvers< ContextType, RequireFields >; + removePanoPost: Resolver< + Maybe, + ParentType, + ContextType, + RequireFields + >; updatePanoPost: Resolver< Maybe, ParentType, @@ -656,6 +683,17 @@ export type QueryResolvers< viewer: Resolver, ParentType, ContextType>; }>; +export type RemovePanoPostPayloadResolvers< + ContextType = KampusGQLContext, + ParentType extends ResolversParentTypes["RemovePanoPostPayload"] = ResolversParentTypes["RemovePanoPostPayload"] +> = ResolversObject<{ + __resolveType?: TypeResolveFn< + "InvalidInput" | "NotAuthorized" | "PanoPost", + ParentType, + ContextType + >; +}>; + export type SozlukQueryResolvers< ContextType = KampusGQLContext, ParentType extends ResolversParentTypes["SozlukQuery"] = ResolversParentTypes["SozlukQuery"] @@ -775,6 +813,7 @@ export type Resolvers = ResolversObject<{ PanoPostEdge: PanoPostEdgeResolvers; PanoQuery: PanoQueryResolvers; Query: QueryResolvers; + RemovePanoPostPayload: RemovePanoPostPayloadResolvers; SozlukQuery: SozlukQueryResolvers; SozlukTerm: SozlukTermResolvers; SozlukTermBody: SozlukTermBodyResolvers;