feat: getMyDrafts api, base drafts list, add drafts to database

This commit is contained in:
MTG2000
2022-07-12 18:05:55 +03:00
parent f0962d5763
commit c37d62ad17
10 changed files with 167 additions and 23 deletions

View File

@@ -32,6 +32,7 @@ export interface NexusGenInputs {
body: string; // String!
cover_image?: string | null; // String
id?: number | null; // Int
is_published?: boolean | null; // Boolean
tags: string[]; // [String!]!
title: string; // String!
}
@@ -335,6 +336,7 @@ export interface NexusGenFieldTypes {
getDonationsStats: NexusGenRootTypes['DonationsStats']; // DonationsStats!
getFeed: NexusGenRootTypes['Post'][]; // [Post!]!
getLnurlDetailsForProject: NexusGenRootTypes['LnurlDetails']; // LnurlDetails!
getMyDrafts: NexusGenRootTypes['Post'][]; // [Post!]!
getPostById: NexusGenRootTypes['Post']; // Post!
getProject: NexusGenRootTypes['Project']; // Project!
getTrendingPosts: NexusGenRootTypes['Post'][]; // [Post!]!
@@ -533,6 +535,7 @@ export interface NexusGenFieldTypeNames {
getDonationsStats: 'DonationsStats'
getFeed: 'Post'
getLnurlDetailsForProject: 'LnurlDetails'
getMyDrafts: 'Post'
getPostById: 'Post'
getProject: 'Project'
getTrendingPosts: 'Post'
@@ -663,6 +666,9 @@ export interface NexusGenArgTypes {
getLnurlDetailsForProject: { // args
project_id: number; // Int!
}
getMyDrafts: { // args
type: NexusGenEnums['POST_TYPE']; // POST_TYPE!
}
getPostById: { // args
id: number; // Int!
type: NexusGenEnums['POST_TYPE']; // POST_TYPE!

View File

@@ -150,6 +150,7 @@ type Query {
getDonationsStats: DonationsStats!
getFeed(skip: Int = 0, sortBy: String, tag: Int = 0, take: Int = 10): [Post!]!
getLnurlDetailsForProject(project_id: Int!): LnurlDetails!
getMyDrafts(type: POST_TYPE!): [Post!]!
getPostById(id: Int!, type: POST_TYPE!): Post!
getProject(id: Int!): Project!
getTrendingPosts: [Post!]!
@@ -196,6 +197,7 @@ input StoryInputType {
body: String!
cover_image: String
id: Int
is_published: Boolean
tags: [String!]!
title: String!
}

View File

@@ -109,6 +109,7 @@ const StoryInputType = inputObjectType({
t.nonNull.string('body');
t.string('cover_image');
t.nonNull.list.nonNull.string('tags');
t.boolean('is_published')
}
})
const createStory = extendType({
@@ -118,21 +119,24 @@ const createStory = extendType({
type: 'Story',
args: { data: StoryInputType },
async resolve(_root, args, ctx) {
const { id, title, body, cover_image, tags } = args.data;
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
user_id: true,
is_published: true
}
})
was_published = oldPost.is_published;
if (user.id !== oldPost.user_id)
throw new ApolloError("Not post author")
}
@@ -160,6 +164,7 @@ const createStory = extendType({
body,
cover_image,
excerpt,
is_published: was_published || is_published,
tags: {
connectOrCreate:
tags.map(tag => {
@@ -185,6 +190,7 @@ const createStory = extendType({
body,
cover_image,
excerpt,
is_published,
tags: {
connectOrCreate:
tags.map(tag => {
@@ -371,7 +377,8 @@ const getFeed = extendType({
id: tag
}
},
})
}),
is_published: true,
},
skip,
take,
@@ -396,7 +403,8 @@ const getTrendingPosts = extendType({
where: {
createdAt: {
gte: lastWeekDate
}
},
is_published: true,
},
orderBy: { votes_count: 'desc' },
take: 5,
@@ -407,6 +415,37 @@ const getTrendingPosts = extendType({
})
const getMyDrafts = extendType({
type: "Query",
definition(t) {
t.nonNull.list.nonNull.field('getMyDrafts', {
type: "Post",
args: {
type: arg({
type: nonNull('POST_TYPE')
})
},
async resolve(parent, { type }, ctx) {
const user = await getUserByPubKey(ctx.userPubKey);
// Do some validation
if (!user)
throw new ApolloError("Not Authenticated");
if (type === 'Story')
return prisma.story.findMany({
where: {
is_published: false,
user_id: user.id
},
orderBy: { createdAt: 'desc' },
}).then(asStoryType)
return []
}
})
}
})
const getPostById = extendType({
type: "Query",
definition(t) {
@@ -453,6 +492,7 @@ module.exports = {
getFeed,
getPostById,
getTrendingPosts,
getMyDrafts,
// Mutations
createStory,