mirror of
https://github.com/aljazceru/landscape-template.git
synced 2026-01-03 22:44:24 +01:00
feat: built stories component in profile
This commit is contained in:
@@ -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'
|
||||
}
|
||||
|
||||
@@ -245,6 +245,7 @@ type User {
|
||||
location: String
|
||||
name: String!
|
||||
role: String
|
||||
stories: [Story!]!
|
||||
twitter: String
|
||||
website: String
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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" } });
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user