From 28e31dd154f518c4928b0a54938a0b4cb6d90e62 Mon Sep 17 00:00:00 2001 From: Yancey Leo Date: Wed, 15 Apr 2020 09:04:26 +0800 Subject: [PATCH] feat: update getAllTags --- schema.gql | 6 +++++ src/posts/models/post.model.ts | 40 ++++++++++++++++++++++++++++++ src/posts/models/posts.model.ts | 43 ++------------------------------- src/posts/models/tags.model.ts | 8 ++++++ src/posts/posts.resolver.ts | 14 ++++++++++- src/posts/posts.service.ts | 19 ++++++++++++++- 6 files changed, 87 insertions(+), 43 deletions(-) create mode 100644 src/posts/models/post.model.ts create mode 100644 src/posts/models/tags.model.ts diff --git a/schema.gql b/schema.gql index 9245fc34..917fcfcf 100644 --- a/schema.gql +++ b/schema.gql @@ -387,6 +387,8 @@ type Query { getPosts(input: PaginationInput!): PostModel! getPostById(id: ID!): PostItemModel! getTopPVPosts(limit: Int!): [PostItemModel!]! + getTopLikePosts(limit: Int!): [PostItemModel!]! + getAllTags: TagsModel! getMottos: [MottoModel!]! getMottoById(id: ID!): MottoModel! getCovers: [CoverModel!]! @@ -457,6 +459,10 @@ type ServiceInfoModel { veid: Float! } +type TagsModel { + tags: [String!]! +} + type TOTPModel { qrcode: String! key: String! diff --git a/src/posts/models/post.model.ts b/src/posts/models/post.model.ts new file mode 100644 index 00000000..15724890 --- /dev/null +++ b/src/posts/models/post.model.ts @@ -0,0 +1,40 @@ +import { Field, ID, ObjectType } from '@nestjs/graphql' + +@ObjectType() +export class PostItemModel { + @Field(() => ID) + public readonly _id: string + + @Field() + public readonly posterUrl: string + + @Field() + public readonly title: string + + @Field() + public readonly summary: string + + @Field() + public readonly content: string + + @Field(() => [String]) + public readonly tags: string[] + + @Field() + public readonly lastModifiedDate: Date + + @Field() + public readonly like: number + + @Field() + public readonly pv: number + + @Field() + public readonly isPublic: boolean + + @Field() + public readonly createdAt: Date + + @Field() + public readonly updatedAt: Date +} diff --git a/src/posts/models/posts.model.ts b/src/posts/models/posts.model.ts index 3c59d5bb..33dbe8ae 100644 --- a/src/posts/models/posts.model.ts +++ b/src/posts/models/posts.model.ts @@ -1,44 +1,5 @@ -/* eslint-disable max-classes-per-file */ -import { Field, ID, ObjectType } from '@nestjs/graphql' - -@ObjectType() -export class PostItemModel { - @Field(() => ID) - public readonly _id: string - - @Field() - public readonly posterUrl: string - - @Field() - public readonly title: string - - @Field() - public readonly summary: string - - @Field() - public readonly content: string - - @Field(() => [String]) - public readonly tags: string[] - - @Field() - public readonly lastModifiedDate: Date - - @Field() - public readonly like: number - - @Field() - public readonly pv: number - - @Field() - public readonly isPublic: boolean - - @Field() - public readonly createdAt: Date - - @Field() - public readonly updatedAt: Date -} +import { Field, ObjectType } from '@nestjs/graphql' +import { PostItemModel } from './post.model' @ObjectType() export class PostModel { diff --git a/src/posts/models/tags.model.ts b/src/posts/models/tags.model.ts new file mode 100644 index 00000000..c131f553 --- /dev/null +++ b/src/posts/models/tags.model.ts @@ -0,0 +1,8 @@ +import { Field, ObjectType } from '@nestjs/graphql' +import { PostItemModel } from './post.model' + +@ObjectType() +export class TagsModel { + @Field(() => [String]) + public readonly tags: string[] +} diff --git a/src/posts/posts.resolver.ts b/src/posts/posts.resolver.ts index 38e5f1e9..afa064ce 100644 --- a/src/posts/posts.resolver.ts +++ b/src/posts/posts.resolver.ts @@ -1,7 +1,9 @@ import { UseGuards } from '@nestjs/common' import { Args, Query, Resolver, Mutation, ID, Int } from '@nestjs/graphql' import { PostsService } from './posts.service' -import { PostModel, PostItemModel } from './models/posts.model' +import { PostModel } from './models/posts.model' +import { PostItemModel } from './models/post.model' +import { TagsModel } from './models/tags.model' import { BatchDeleteModel } from '../database/models/batch-delete.model' import { CreatePostInput } from './dtos/create-post.input' import { UpdatePostInput } from './dtos/update-post.input' @@ -64,4 +66,14 @@ export class PostsResolver { public async getTopPVPosts(@Args({ name: 'limit', type: () => Int }) limit: number) { return this.postsService.getTopPVPosts(limit) } + + @Query(() => [PostItemModel]) + public async getTopLikePosts(@Args({ name: 'limit', type: () => Int }) limit: number) { + return this.postsService.getTopLikePosts(limit) + } + + @Query(() => TagsModel) + public async getAllTags() { + return this.postsService.getAllTags() + } } diff --git a/src/posts/posts.service.ts b/src/posts/posts.service.ts index 138c16a1..979889d3 100644 --- a/src/posts/posts.service.ts +++ b/src/posts/posts.service.ts @@ -4,7 +4,9 @@ import { Model } from 'mongoose' import { CreatePostInput } from './dtos/create-post.input' import { UpdatePostInput } from './dtos/update-post.input' import { PaginationInput } from './dtos/pagination.input' -import { PostModel, PostItemModel } from './models/posts.model' +import { PostModel } from './models/posts.model' +import { PostItemModel } from './models/post.model' +import { TagsModel } from './models/tags.model' import { Post } from './interfaces/posts.interface' import { BatchDeleteModel } from '../database/models/batch-delete.model' @@ -83,4 +85,19 @@ export class PostsService { .sort({ pv: -1, _id: -1 }) .limit(limit) } + + public async getTopLikePosts(limit: number): Promise { + return this.postModel + .find({ isPublic: { $ne: false } }) + .sort({ like: -1, _id: -1 }) + .limit(limit) + } + + public async getAllTags(): Promise { + const posts = await this.postModel.find({}, { isPublic: { $ne: false }, tags: 1 }) + + return { + tags: [], + } + } }