feat: built stories component in profile

This commit is contained in:
MTG2000
2022-07-14 11:39:59 +03:00
parent adb11c3af7
commit 7cd6fc749c
12 changed files with 337 additions and 145 deletions

View File

@@ -408,6 +408,7 @@ export interface NexusGenFieldTypes {
location: string | null; // String
name: string; // String!
role: string | null; // String
stories: NexusGenRootTypes['Story'][]; // [Story!]!
twitter: string | null; // String
website: string | null; // String
}
@@ -615,6 +616,7 @@ export interface NexusGenFieldTypeNames {
location: 'String'
name: 'String'
role: 'String'
stories: 'Story'
twitter: 'String'
website: 'String'
}

View File

@@ -245,6 +245,7 @@ type User {
location: String
name: String!
role: String
stories: [Story!]!
twitter: String
website: String
}

View File

@@ -114,143 +114,7 @@ const StoryInputType = inputObjectType({
t.boolean('is_published')
}
})
const createStory = extendType({
type: 'Mutation',
definition(t) {
t.field('createStory', {
type: 'Story',
args: { data: StoryInputType },
async resolve(_root, args, ctx) {
const { id, title, body, cover_image, tags, is_published } = args.data;
const user = await getUserByPubKey(ctx.userPubKey);
// Do some validation
if (!user)
throw new ApolloError("Not Authenticated");
let was_published = false;
if (id) {
const oldPost = await prisma.story.findFirst({
where: { id },
select: {
user_id: true,
is_published: true
}
})
was_published = oldPost.is_published;
if (user.id !== oldPost.user_id)
throw new ApolloError("Not post author")
}
// TODO: validate post data
// Preprocess & insert
const htmlBody = marked.parse(body);
const excerpt = htmlBody.replace(/<[^>]+>/g, '').slice(0, 120);
if (id) {
await prisma.story.update({
where: { id },
data: {
tags: {
set: []
},
}
});
return prisma.story.update({
where: { id },
data: {
title,
body,
cover_image,
excerpt,
is_published: was_published || is_published,
tags: {
connectOrCreate:
tags.map(tag => {
tag = tag.toLowerCase().trim();
return {
where: {
title: tag,
},
create: {
title: tag
}
}
})
},
}
})
}
return prisma.story.create({
data: {
title,
body,
cover_image,
excerpt,
is_published,
tags: {
connectOrCreate:
tags.map(tag => {
tag = tag.toLowerCase().trim();
return {
where: {
title: tag,
},
create: {
title: tag
}
}
})
},
user: {
connect: {
id: user.id,
}
}
}
})
}
})
},
})
const deleteStory = extendType({
type: 'Mutation',
definition(t) {
t.field('deleteStory', {
type: 'Story',
args: { id: nonNull(intArg()) },
async resolve(_root, args, ctx) {
const { id } = args;
const user = await getUserByPubKey(ctx.userPubKey);
// Do some validation
if (!user)
throw new ApolloError("Not Authenticated");
const oldPost = await prisma.story.findFirst({
where: { id },
select: {
user_id: true
}
})
if (user.id !== oldPost.user_id)
throw new ApolloError("Not post author")
return prisma.story.delete({
where: {
id
}
})
}
})
},
})
const BountyApplication = objectType({
name: 'BountyApplication',
@@ -417,6 +281,7 @@ const getTrendingPosts = extendType({
})
const getMyDrafts = extendType({
type: "Query",
definition(t) {
@@ -475,6 +340,144 @@ const getPostById = extendType({
}
})
const createStory = extendType({
type: 'Mutation',
definition(t) {
t.field('createStory', {
type: 'Story',
args: { data: StoryInputType },
async resolve(_root, args, ctx) {
const { id, title, body, cover_image, tags, is_published } = args.data;
const user = await getUserByPubKey(ctx.userPubKey);
// Do some validation
if (!user)
throw new ApolloError("Not Authenticated");
let was_published = false;
if (id) {
const oldPost = await prisma.story.findFirst({
where: { id },
select: {
user_id: true,
is_published: true
}
})
was_published = oldPost.is_published;
if (user.id !== oldPost.user_id)
throw new ApolloError("Not post author")
}
// TODO: validate post data
// Preprocess & insert
const htmlBody = marked.parse(body);
const excerpt = htmlBody.replace(/<[^>]+>/g, '').slice(0, 120);
if (id) {
await prisma.story.update({
where: { id },
data: {
tags: {
set: []
},
}
});
return prisma.story.update({
where: { id },
data: {
title,
body,
cover_image,
excerpt,
is_published: was_published || is_published,
tags: {
connectOrCreate:
tags.map(tag => {
tag = tag.toLowerCase().trim();
return {
where: {
title: tag,
},
create: {
title: tag
}
}
})
},
}
})
}
return prisma.story.create({
data: {
title,
body,
cover_image,
excerpt,
is_published,
tags: {
connectOrCreate:
tags.map(tag => {
tag = tag.toLowerCase().trim();
return {
where: {
title: tag,
},
create: {
title: tag
}
}
})
},
user: {
connect: {
id: user.id,
}
}
}
})
}
})
},
})
const deleteStory = extendType({
type: 'Mutation',
definition(t) {
t.field('deleteStory', {
type: 'Story',
args: { id: nonNull(intArg()) },
async resolve(_root, args, ctx) {
const { id } = args;
const user = await getUserByPubKey(ctx.userPubKey);
// Do some validation
if (!user)
throw new ApolloError("Not Authenticated");
const oldPost = await prisma.story.findFirst({
where: { id },
select: {
user_id: true
}
})
if (user.id !== oldPost.user_id)
throw new ApolloError("Not post author")
return prisma.story.delete({
where: {
id
}
})
}
})
},
})

View File

@@ -23,6 +23,13 @@ const User = objectType({
t.string('linkedin')
t.string('bio')
t.string('location')
t.nonNull.list.nonNull.field('stories', {
type: "Story",
resolve: (parent) => {
return prisma.story.findMany({ where: { user_id: parent.id, is_published: true }, orderBy: { createdAt: "desc" } });
}
});
}
})