장재혁

Add post api

...@@ -5,6 +5,7 @@ import { join } from 'path'; ...@@ -5,6 +5,7 @@ import { join } from 'path';
5 import { AppController } from './app.controller'; 5 import { AppController } from './app.controller';
6 import { AppService } from './app.service'; 6 import { AppService } from './app.service';
7 import { MypageModule } from './mypage/mypage.module'; 7 import { MypageModule } from './mypage/mypage.module';
8 +import { PostModule } from './post/post.module';
8 9
9 @Module({ 10 @Module({
10 imports: [ 11 imports: [
...@@ -16,10 +17,11 @@ import { MypageModule } from './mypage/mypage.module'; ...@@ -16,10 +17,11 @@ import { MypageModule } from './mypage/mypage.module';
16 type: 'sqlite', 17 type: 'sqlite',
17 database: 'database.db', 18 database: 'database.db',
18 entities: ['dist/**/*.entity{.ts,.js}'], 19 entities: ['dist/**/*.entity{.ts,.js}'],
19 - synchronize: true, 20 + synchronize: false,
20 logging: false, 21 logging: false,
21 }), 22 }),
22 MypageModule, 23 MypageModule,
24 + PostModule,
23 ], 25 ],
24 controllers: [AppController], 26 controllers: [AppController],
25 providers: [AppService], 27 providers: [AppService],
......
1 +import { Field, InputType } from '@nestjs/graphql';
2 +import { Entity } from 'typeorm';
3 +
4 +@Entity()
5 +@InputType()
6 +export class GetPostInput {
7 + @Field()
8 + id: number;
9 +
10 + @Field({ nullable: true })
11 + author?: string;
12 +
13 + @Field({ nullable: true })
14 + category?: string;
15 +}
1 +/* eslint-disable @typescript-eslint/no-unused-vars */
2 +import { ObjectType, Field, Int } from '@nestjs/graphql';
3 +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
4 +
5 +@Entity({ name: 'post' })
6 +@ObjectType()
7 +export class Post {
8 + @PrimaryGeneratedColumn()
9 + @Field((type) => Int)
10 + id: number;
11 +
12 + @Column()
13 + @Field()
14 + author: string;
15 +
16 + @Column()
17 + @Field()
18 + created_date: string;
19 +
20 + @Column({ nullable: true })
21 + @Field({ nullable: true })
22 + updated_date?: string;
23 +
24 + @Column()
25 + @Field()
26 + title: string;
27 +
28 + @Column()
29 + @Field()
30 + content: string;
31 +
32 + @Column()
33 + @Field()
34 + category: string;
35 +}
1 +import { Module } from '@nestjs/common';
2 +import { PostService } from './post.service';
3 +import { PostResolver } from './post.resolver';
4 +import { TypeOrmModule } from '@nestjs/typeorm';
5 +import { Post } from './post.entity';
6 +
7 +@Module({
8 + imports: [TypeOrmModule.forFeature([Post])],
9 + providers: [PostService, PostResolver],
10 +})
11 +export class PostModule {}
1 +import { Test, TestingModule } from '@nestjs/testing';
2 +import { PostResolver } from './post.resolver';
3 +
4 +describe('PostResolver', () => {
5 + let resolver: PostResolver;
6 +
7 + beforeEach(async () => {
8 + const module: TestingModule = await Test.createTestingModule({
9 + providers: [PostResolver],
10 + }).compile();
11 +
12 + resolver = module.get<PostResolver>(PostResolver);
13 + });
14 +
15 + it('should be defined', () => {
16 + expect(resolver).toBeDefined();
17 + });
18 +});
1 +import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
2 +import { PostService } from './post.service';
3 +import { Post } from './post.entity';
4 +import { GetPostInput } from './dto/post.input';
5 +
6 +@Resolver((of) => Post)
7 +export class PostResolver {
8 + constructor(private postService: PostService) {}
9 +
10 + @Query((returns) => [Post])
11 + getPosts(): Promise<Post[]> {
12 + return this.postService.findAll();
13 + }
14 +
15 + @Query((returns) => Post)
16 + getPost(@Args('id') id: number): Promise<Post> {
17 + return this.postService.findOne(id);
18 + }
19 +}
1 +import { Test, TestingModule } from '@nestjs/testing';
2 +import { PostService } from './post.service';
3 +
4 +describe('PostService', () => {
5 + let service: PostService;
6 +
7 + beforeEach(async () => {
8 + const module: TestingModule = await Test.createTestingModule({
9 + providers: [PostService],
10 + }).compile();
11 +
12 + service = module.get<PostService>(PostService);
13 + });
14 +
15 + it('should be defined', () => {
16 + expect(service).toBeDefined();
17 + });
18 +});
1 +import { Injectable } from '@nestjs/common';
2 +import { InjectRepository } from '@nestjs/typeorm';
3 +import { Repository } from 'typeorm';
4 +import { GetPostInput } from './dto/post.input';
5 +import { Post } from './post.entity';
6 +
7 +@Injectable()
8 +export class PostService {
9 + constructor(
10 + @InjectRepository(Post) private postRepository: Repository<Post>,
11 + ) {}
12 +
13 + async findAll(): Promise<Post[]> {
14 + return this.postRepository.find();
15 + }
16 +
17 + async findOne(id: number): Promise<Post> {
18 + return this.postRepository.findOne(id);
19 + }
20 +}
...@@ -17,6 +17,18 @@ type MyPage { ...@@ -17,6 +17,18 @@ type MyPage {
17 type: String 17 type: String
18 } 18 }
19 19
20 +type Post {
21 + author: String!
22 + category: String!
23 + content: String!
24 + created_date: String!
25 + id: Int!
26 + title: String!
27 + updated_date: String
28 +}
29 +
20 type Query { 30 type Query {
31 + getPost(id: Float!): Post!
32 + getPosts: [Post!]!
21 myPage: [MyPage!]! 33 myPage: [MyPage!]!
22 } 34 }
......
1 +export enum CATEGORY {
2 + test = 'test_category',
3 + test2 = 'test2_category',
4 +}