장재혁

Add Post CR

1 -import { Field, InputType, PartialType, PickType } from '@nestjs/graphql'; 1 +import { Field, InputType, PartialType, PickType } from '@nestjs/graphql'
2 -import { IsNumber } from 'class-validator'; 2 +import { IsNumber } from 'class-validator'
3 -import { Post } from '../model/post.entity'; 3 +import { Post } from '../model/post.entity'
4 4
5 @InputType() 5 @InputType()
6 export class GetPostInput { 6 export class GetPostInput {
7 - @Field() 7 + @Field({ nullable: true })
8 - id: number; 8 + id?: number
9 9
10 @Field({ nullable: true }) 10 @Field({ nullable: true })
11 - author?: string; 11 + author?: string
12 12
13 @Field({ nullable: true }) 13 @Field({ nullable: true })
14 - category?: string; 14 + category?: string
15 } 15 }
16 16
17 @InputType() 17 @InputType()
...@@ -25,5 +25,5 @@ export class CreatePostInput extends PickType( ...@@ -25,5 +25,5 @@ export class CreatePostInput extends PickType(
25 export class UpdatePostInput extends PartialType(CreatePostInput) { 25 export class UpdatePostInput extends PartialType(CreatePostInput) {
26 @Field() 26 @Field()
27 @IsNumber() 27 @IsNumber()
28 - id: number; 28 + id: number
29 } 29 }
......
1 /* eslint-disable @typescript-eslint/no-unused-vars */ 1 /* eslint-disable @typescript-eslint/no-unused-vars */
2 -import { ObjectType, Field, Int } from '@nestjs/graphql'; 2 +import { ObjectType, Field, Int } from '@nestjs/graphql'
3 -import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; 3 +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
4 4
5 @Entity({ name: 'post' }) 5 @Entity({ name: 'post' })
6 @ObjectType() 6 @ObjectType()
7 export class Post { 7 export class Post {
8 @PrimaryGeneratedColumn() 8 @PrimaryGeneratedColumn()
9 @Field((type) => Int) 9 @Field((type) => Int)
10 - id: number; 10 + id: number
11 11
12 @Column() 12 @Column()
13 @Field() 13 @Field()
14 - author: string; 14 + author: string
15 15
16 @Column() 16 @Column()
17 @Field() 17 @Field()
18 - created_date: string; 18 + created_date: string
19 19
20 @Column({ nullable: true }) 20 @Column({ nullable: true })
21 @Field({ nullable: true }) 21 @Field({ nullable: true })
22 - updated_date?: string; 22 + updated_date?: string
23 23
24 @Column() 24 @Column()
25 @Field() 25 @Field()
26 - title: string; 26 + title: string
27 27
28 @Column() 28 @Column()
29 @Field() 29 @Field()
30 - content: string; 30 + content: string
31 31
32 @Column() 32 @Column()
33 @Field() 33 @Field()
34 - category: string; 34 + category: string
35 } 35 }
......
1 import { Args, Mutation, Query, Resolver } from '@nestjs/graphql' 1 import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
2 import { PostService } from './post.service' 2 import { PostService } from './post.service'
3 import { Post } from './model/post.entity' 3 import { Post } from './model/post.entity'
4 -import { GetPostInput } from './dto/post.input' 4 +import { CreatePostInput, GetPostInput } from './dto/post.input'
5 5
6 @Resolver((of) => Post) 6 @Resolver((of) => Post)
7 export class PostResolver { 7 export class PostResolver {
8 constructor(private postService: PostService) {} 8 constructor(private postService: PostService) {}
9 9
10 @Query((returns) => [Post]) 10 @Query((returns) => [Post])
11 - getPosts(): Promise<Post[]> { 11 + getAllPosts(): Promise<Post[]> {
12 return this.postService.findAll() 12 return this.postService.findAll()
13 } 13 }
14 14
15 + @Query((returns) => [Post])
16 + getSomePosts(@Args('input') input: GetPostInput): Promise<Post[]> {
17 + return this.postService.findSome(input)
18 + }
19 +
15 @Query((returns) => Post) 20 @Query((returns) => Post)
16 getPost(@Args('id') id: number): Promise<Post> { 21 getPost(@Args('id') id: number): Promise<Post> {
17 return this.postService.findOne(id) 22 return this.postService.findOne(id)
18 } 23 }
24 +
25 + @Mutation(() => Post, { name: 'createPost' })
26 + createPost(@Args('input') input: CreatePostInput): Promise<Post> {
27 + return this.postService.createOne(input)
28 + }
19 } 29 }
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
7 UpdatePostInput, 7 UpdatePostInput,
8 } from './dto/post.input' 8 } from './dto/post.input'
9 import { Post } from './model/post.entity' 9 import { Post } from './model/post.entity'
10 +import { getCurrentDate } from '../shared/utils'
10 11
11 @Injectable() 12 @Injectable()
12 export class PostService { 13 export class PostService {
...@@ -26,7 +27,9 @@ export class PostService { ...@@ -26,7 +27,9 @@ export class PostService {
26 async findSome(input: Partial<GetPostInput>): Promise<Post[]> { 27 async findSome(input: Partial<GetPostInput>): Promise<Post[]> {
27 return this.postRepository 28 return this.postRepository
28 .createQueryBuilder('post') 29 .createQueryBuilder('post')
29 - .where('post.author like :author', { author: input.author }) 30 + .where('post.id like :id', { id: input.id })
31 + .orWhere('post.author like :author', { author: input.author })
32 + .orWhere('post.category like :category', { category: input.category })
30 .getMany() 33 .getMany()
31 } 34 }
32 35
...@@ -37,28 +40,14 @@ export class PostService { ...@@ -37,28 +40,14 @@ export class PostService {
37 await queryRunner.startTransaction() 40 await queryRunner.startTransaction()
38 41
39 try { 42 try {
40 - result = await queryRunner.manager.save(this.postRepository.create(input)) 43 + const newInput = {
41 - await queryRunner.commitTransaction() 44 + ...input,
42 - } catch (err) { 45 + created_date: getCurrentDate(),
43 - await queryRunner.rollbackTransaction() 46 + author: 'test',
44 - throw err 47 + }
45 - } finally { 48 + result = await queryRunner.manager.save(
46 - await queryRunner.release() 49 + this.postRepository.create(newInput),
47 - } 50 + )
48 -
49 - return result
50 - }
51 -
52 - async updateOne(input: UpdatePostInput): Promise<Post> {
53 - let result
54 - const queryRunner: QueryRunner = this.connection.createQueryRunner()
55 - await queryRunner.connect()
56 - await queryRunner.startTransaction()
57 -
58 - try {
59 - const before = await this.postRepository.findOne(input.id)
60 -
61 - result = await queryRunner.manager.save(this.postRepository.create(input))
62 await queryRunner.commitTransaction() 51 await queryRunner.commitTransaction()
63 } catch (err) { 52 } catch (err) {
64 await queryRunner.rollbackTransaction() 53 await queryRunner.rollbackTransaction()
......
...@@ -7,8 +7,21 @@ input CreateMyInput { ...@@ -7,8 +7,21 @@ input CreateMyInput {
7 type: String 7 type: String
8 } 8 }
9 9
10 +input CreatePostInput {
11 + category: String!
12 + content: String!
13 + title: String!
14 +}
15 +
16 +input GetPostInput {
17 + author: String
18 + category: String
19 + id: Float
20 +}
21 +
10 type Mutation { 22 type Mutation {
11 createMyPage(createMyInput: CreateMyInput!): MyPage! 23 createMyPage(createMyInput: CreateMyInput!): MyPage!
24 + createPost(input: CreatePostInput!): Post!
12 } 25 }
13 26
14 type MyPage { 27 type MyPage {
...@@ -28,7 +41,8 @@ type Post { ...@@ -28,7 +41,8 @@ type Post {
28 } 41 }
29 42
30 type Query { 43 type Query {
44 + getAllPosts: [Post!]!
31 getPost(id: Float!): Post! 45 getPost(id: Float!): Post!
32 - getPosts: [Post!]! 46 + getSomePosts(input: GetPostInput!): [Post!]!
33 myPage: [MyPage!]! 47 myPage: [MyPage!]!
34 } 48 }
......
1 +export function getCurrentDate() {
2 + const now = new Date()
3 + const MM = now.getMonth() + 1
4 + const DD = now.getDate()
5 +
6 + const yyyymmdd = [
7 + now.getFullYear(),
8 + (MM > 9 ? '' : '0') + MM,
9 + (DD > 9 ? '' : '0') + DD,
10 + ].join('-')
11 +
12 + const hh = now.getHours()
13 + const mm = now.getMinutes()
14 + const ss = now.getSeconds()
15 +
16 + const hhmmss = [
17 + (hh > 9 ? '' : '0') + hh,
18 + (mm > 9 ? '' : '0') + mm,
19 + (ss > 9 ? '' : '0') + ss,
20 + ].join(':')
21 +
22 + return `${yyyymmdd} ${hhmmss}`
23 +}