Skip to content

Commit

Permalink
feat(apps/gql): add post upvote mutations (#605)
Browse files Browse the repository at this point in the history
# Description

Created create and remove mutations for post upvotes.
Also ensured that postID and userID are unique with one block attribute
(@@unique) in the psima schema.

Closes #554 
Closes #556
  • Loading branch information
rasitds authored Aug 7, 2023
1 parent 570f405 commit 588d5b3
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apps/gql/features/pano/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { createPanoCommentActions } from "~/features/pano/comment";
import { createPanoPostActions } from "~/features/pano/post";
import { type Clients } from "~/clients";
import { createPostUpvoteActions } from "./postUpvote";

export function createPanoActions(clients: Clients) {
return {
post: createPanoPostActions(clients),
comment: createPanoCommentActions(clients),
postUpvote: createPostUpvoteActions(clients),
};
}
35 changes: 35 additions & 0 deletions apps/gql/features/pano/postUpvote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { type Clients } from "~/clients";

interface CreatePostUpvoteArgs {
postID: string;
userID: string;
}

interface RemovePostUpvoteArgs {
postID: string;
userID: string;
}

export function createPostUpvoteActions({ prisma }: Clients) {
const create = (args: CreatePostUpvoteArgs) => {
return prisma.upvote.create({
data: {
post: { connect: { id: args.postID } },
owner: { connect: { id: args.userID } },
},
});
};

const remove = (args: RemovePostUpvoteArgs) => {
return prisma.upvote.delete({
where: {
postID_userID: {
postID: args.postID,
userID: args.userID,
},
},
});
};

return { create, remove };
}
10 changes: 9 additions & 1 deletion apps/gql/loaders/pano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
createPrismaLoader,
} from "@kampus/gql-utils";
import { type Connection } from "@kampus/gql-utils/connection";
import { type Comment, type Post } from "@kampus/prisma";
import { type Comment, type Post, type Upvote } from "@kampus/prisma";

import { type Clients } from "~/clients";
import {
type PanoComment,
type PanoCommentConnection,
type PanoPost,
type PanoPostConnection,
type PostUpvote,
} from "~/schema/types.generated";

export type PanoLoaders = ReturnType<typeof createPanoLoaders>;
Expand Down Expand Up @@ -142,3 +143,10 @@ export const transformPanoCommentConnection = (connection: Connection<Comment>)
totalCount: connection.totalCount,
} satisfies PanoCommentConnection;
};

export const transformPostUpvote = (upvote: Upvote) => {
return {
...upvote,
__typename: "PostUpvote",
} satisfies PostUpvote;
};
28 changes: 28 additions & 0 deletions apps/gql/schema/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
transformPanoCommentConnection,
transformPanoPost,
transformPanoPostConnection,
transformPostUpvote,
} from "~/loaders/pano";
import { transformSozlukTerm, transformSozlukTermsConnection } from "~/loaders/sozluk";
import { transformUser } from "~/loaders/user";
Expand Down Expand Up @@ -244,6 +245,9 @@ export const resolvers = {
CreatePanoCommentPayload: {}, // union
UpdatePanoCommentPayload: {}, // union
RemovePanoCommentPayload: {}, // union
CreatePostUpvotePayload: {}, // union
RemovePostUpvotePayload: {}, // union

UserError: {}, // interface

InvalidInput: errorFieldsResolver,
Expand Down Expand Up @@ -347,5 +351,29 @@ export const resolvers = {

return transformPanoComment(await actions.pano.comment.remove(id.value));
},
createPostUpvote: async (_, { input }, { actions, pasaport: { session } }) => {
if (!session?.user?.id) {
return NotAuthorized();
}

return transformPostUpvote(
await actions.pano.postUpvote.create({
postID: input.postID,
userID: session.user.id,
})
);
},
removePostUpvote: async (_, { input }, { actions, pasaport: { session } }) => {
if (!session?.user?.id) {
return NotAuthorized();
}

return transformPostUpvote(
await actions.pano.postUpvote.remove({
postID: input.postID,
userID: session.user.id,
})
);
},
},
} satisfies Resolvers;
21 changes: 21 additions & 0 deletions apps/gql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ type PanoCommentEdge {
node: PanoComment
}

type PostUpvote {
id: ID!
postID: String!
userID: String!
}

type Mutation {
createPanoPost(input: CreatePanoPostInput!): CreatePanoPostPayload
updatePanoPost(input: UpdatePanoPostInput!): UpdatePanoPostPayload
Expand All @@ -166,6 +172,9 @@ type Mutation {
createPanoComment(input: CreatePanoCommentInput!): CreatePanoCommentPayload
updatePanoComment(input: UpdatePanoCommentInput!): UpdatePanoCommentPayload
removePanoComment(input: RemovePanoCommentInput!): RemovePanoCommentPayload

createPostUpvote(input: CreatePostUpvoteInput!): CreatePostUpvotePayload
removePostUpvote(input: RemovePostUpvoteInput!): RemovePostUpvotePayload
}

input CreatePanoPostInput {
Expand Down Expand Up @@ -211,3 +220,15 @@ union RemovePanoCommentPayload = PanoComment | NotAuthorized | InvalidInput
input RemovePanoCommentInput {
id: ID!
}

input CreatePostUpvoteInput {
postID: String!
}

union CreatePostUpvotePayload = PostUpvote | NotAuthorized | InvalidInput

input RemovePostUpvoteInput {
postID: String!
}

union RemovePostUpvotePayload = PostUpvote | NotAuthorized | InvalidInput
Loading

0 comments on commit 588d5b3

Please sign in to comment.