From cdfa09cd537f379d0a501e83ea84cf4c56668107 Mon Sep 17 00:00:00 2001 From: Ben Awad Date: Sun, 16 Aug 2020 11:06:18 -0500 Subject: [PATCH] delete-post --- server/src/entities/Updoot.ts | 4 +- server/src/resolvers/post.ts | 20 +++++++- web/src/components/NavBar.tsx | 23 ++++++--- web/src/generated/graphql.tsx | 21 +++++++- web/src/graphql/mutations/deletePost.graphql | 3 ++ web/src/pages/index.tsx | 53 ++++++++++++-------- web/src/utils/createUrqlClient.ts | 7 +++ 7 files changed, 97 insertions(+), 34 deletions(-) create mode 100644 web/src/graphql/mutations/deletePost.graphql diff --git a/server/src/entities/Updoot.ts b/server/src/entities/Updoot.ts index e108dc93..dd23efe8 100644 --- a/server/src/entities/Updoot.ts +++ b/server/src/entities/Updoot.ts @@ -23,6 +23,8 @@ export class Updoot extends BaseEntity { @PrimaryColumn() postId: number; - @ManyToOne(() => Post, (post) => post.updoots) + @ManyToOne(() => Post, (post) => post.updoots, { + onDelete: "CASCADE", + }) post: Post; } diff --git a/server/src/resolvers/post.ts b/server/src/resolvers/post.ts index 1c6f2440..06e725cd 100644 --- a/server/src/resolvers/post.ts +++ b/server/src/resolvers/post.ts @@ -203,8 +203,24 @@ export class PostResolver { } @Mutation(() => Boolean) - async deletePost(@Arg("id") id: number): Promise { - await Post.delete(id); + @UseMiddleware(isAuth) + async deletePost( + @Arg("id", () => Int) id: number, + @Ctx() { req }: MyContext + ): Promise { + // not cascade way + // const post = await Post.findOne(id); + // if (!post) { + // return false; + // } + // if (post.creatorId !== req.session.userId) { + // throw new Error("not authorized"); + // } + + // await Updoot.delete({ postId: id }); + // await Post.delete({ id }); + + await Post.delete({ id, creatorId: req.session.userId }); return true; } } diff --git a/web/src/components/NavBar.tsx b/web/src/components/NavBar.tsx index 4b6e36f0..fcf2db4a 100644 --- a/web/src/components/NavBar.tsx +++ b/web/src/components/NavBar.tsx @@ -31,7 +31,12 @@ export const NavBar: React.FC = ({}) => { // user is logged in } else { body = ( - + + + + {data.me.username}