Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apps/gql): create PanoComment mutations #603

Merged
merged 4 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/gql/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createPanoActions } from "~/features/pano";
import { type Clients } from "./clients";
import { createPanoActions } from "./features/pano/post";

export type DataActions = ReturnType<typeof createActions>;

Expand Down
50 changes: 50 additions & 0 deletions apps/gql/features/pano/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { type Clients } from "~/clients";

interface CreatePanoCommentArgs {
userID: string;
content: string;
postID: string;
parentID: string | null;
}

interface UpdatePanoCommentArgs {
content: string;
}

export function createPanoCommentActions({ prisma }: Clients) {
const create = (args: CreatePanoCommentArgs) => {
return prisma.comment.create({
data: {
owner: { connect: { id: args.userID } },
content: args.content,
post: {
connect: { id: args.postID },
},
parent: args.parentID
? {
connect: { id: args.parentID },
}
: undefined,
},
});
};

const update = (id: string, args: UpdatePanoCommentArgs) => {
return prisma.comment.update({
where: { id },
data: {
content: args.content,
},
});
};

const remove = (id: string) => {
return prisma.comment.delete({ where: { id } });
};

return {
create,
update,
remove,
};
}
10 changes: 10 additions & 0 deletions apps/gql/features/pano/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createPanoCommentActions } from "~/features/pano/comment";
import { createPanoPostActions } from "~/features/pano/post";
import { type Clients } from "~/clients";

export function createPanoActions(clients: Clients) {
return {
post: createPanoPostActions(clients),
comment: createPanoCommentActions(clients),
};
}
8 changes: 1 addition & 7 deletions apps/gql/features/pano/post.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { type Clients } from "~/clients";
import { getSitename } from "./utils/get-sitename";

export function createPanoActions(clients: Clients) {
return {
post: createPanoPostActions(clients),
};
}

interface CreatePanoPostArgs {
title: string;
userID: string;
Expand All @@ -20,7 +14,7 @@ interface UpdatePanoPostArgs {
content: string | null;
}

function createPanoPostActions({ prisma }: Clients) {
export function createPanoPostActions({ prisma }: Clients) {
const create = (args: CreatePanoPostArgs) => {
return prisma.post.create({
data: {
Expand Down
54 changes: 53 additions & 1 deletion apps/gql/schema/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ export const resolvers = {
CreatePanoPostPayload: {}, // union
UpdatePanoPostPayload: {}, // union
RemovePanoPostPayload: {}, // union
CreatePanoCommentPayload: {}, // union
UpdatePanoCommentPayload: {}, // union
RemovePanoCommentPayload: {}, // union
UserError: {}, // interface

InvalidInput: errorFieldsResolver,
Expand All @@ -253,7 +256,6 @@ export const resolvers = {
const created = await actions.pano.post.create({ ...input, userID: session.user.id });
return transformPanoPost(await loaders.pano.post.byID.load(created.id));
},

updatePanoPost: async (_, { input }, { actions, loaders, pasaport: { session } }) => {
if (!session?.user?.id) {
return NotAuthorized();
Expand Down Expand Up @@ -289,5 +291,55 @@ export const resolvers = {

return transformPanoPost(await actions.pano.post.remove(id.value));
},
createPanoComment: async (_, { input }, { loaders, actions, pasaport: { session } }) => {
if (!session?.user?.id) {
return NotAuthorized();
}

if (!input.postID && !input.content) {
return InvalidInput("Either post or content is required");
}

const created = await actions.pano.comment.create({ ...input, userID: session.user.id });

return transformPanoComment(await loaders.pano.comment.byID.load(created.id));
},
updatePanoComment: async (_, { input }, { actions, loaders, pasaport: { session } }) => {
if (!session?.user?.id) {
return NotAuthorized();
}

const id = parse(input.id);
if (id.type !== "PanoComment") {
return InvalidInput("wrong id");
}

const comment = await loaders.pano.comment.byID.load(id.value);
if (comment.userID !== session.user.id) {
return NotAuthorized();
}

const updated = await actions.pano.comment.update(id.value, input);
return transformPanoComment(
await loaders.pano.comment.byID.clear(updated.id).load(updated.id)
);
},
removePanoComment: async (_, { input }, { actions, loaders, pasaport: { session } }) => {
if (!session?.user?.id) {
return NotAuthorized();
}

const id = parse(input.id);
if (id.type !== "PanoComment") {
return InvalidInput("wrong id");
}

const comment = await loaders.pano.comment.byID.load(id.value);
if (comment.userID !== session.user.id) {
return NotAuthorized();
}

return transformPanoComment(await actions.pano.comment.remove(id.value));
},
},
} satisfies Resolvers;
25 changes: 25 additions & 0 deletions apps/gql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ type Mutation {
createPanoPost(input: CreatePanoPostInput!): CreatePanoPostPayload
updatePanoPost(input: UpdatePanoPostInput!): UpdatePanoPostPayload
removePanoPost(input: RemovePanoPostInput!): RemovePanoPostPayload

createPanoComment(input: CreatePanoCommentInput!): CreatePanoCommentPayload
updatePanoComment(input: UpdatePanoCommentInput!): UpdatePanoCommentPayload
removePanoComment(input: RemovePanoCommentInput!): RemovePanoCommentPayload
}

input CreatePanoPostInput {
Expand All @@ -184,3 +188,24 @@ input RemovePanoPostInput {
}

union RemovePanoPostPayload = PanoPost | NotAuthorized | InvalidInput

union CreatePanoCommentPayload = PanoComment | NotAuthorized | InvalidInput

input CreatePanoCommentInput {
content: String!
postID: String!
parentID: String
}

union UpdatePanoCommentPayload = PanoComment | NotAuthorized | InvalidInput

input UpdatePanoCommentInput {
id: ID!
content: String!
}

union RemovePanoCommentPayload = PanoComment | NotAuthorized | InvalidInput

input RemovePanoCommentInput {
id: ID!
}
Loading