장재혁

Add Post CR

import { Field, InputType, PartialType, PickType } from '@nestjs/graphql';
import { IsNumber } from 'class-validator';
import { Post } from '../model/post.entity';
import { Field, InputType, PartialType, PickType } from '@nestjs/graphql'
import { IsNumber } from 'class-validator'
import { Post } from '../model/post.entity'
@InputType()
export class GetPostInput {
@Field()
id: number;
@Field({ nullable: true })
id?: number
@Field({ nullable: true })
author?: string;
author?: string
@Field({ nullable: true })
category?: string;
category?: string
}
@InputType()
......@@ -25,5 +25,5 @@ export class CreatePostInput extends PickType(
export class UpdatePostInput extends PartialType(CreatePostInput) {
@Field()
@IsNumber()
id: number;
id: number
}
......
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { ObjectType, Field, Int } from '@nestjs/graphql'
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
@Entity({ name: 'post' })
@ObjectType()
export class Post {
@PrimaryGeneratedColumn()
@Field((type) => Int)
id: number;
id: number
@Column()
@Field()
author: string;
author: string
@Column()
@Field()
created_date: string;
created_date: string
@Column({ nullable: true })
@Field({ nullable: true })
updated_date?: string;
updated_date?: string
@Column()
@Field()
title: string;
title: string
@Column()
@Field()
content: string;
content: string
@Column()
@Field()
category: string;
category: string
}
......
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
import { PostService } from './post.service'
import { Post } from './model/post.entity'
import { GetPostInput } from './dto/post.input'
import { CreatePostInput, GetPostInput } from './dto/post.input'
@Resolver((of) => Post)
export class PostResolver {
constructor(private postService: PostService) {}
@Query((returns) => [Post])
getPosts(): Promise<Post[]> {
getAllPosts(): Promise<Post[]> {
return this.postService.findAll()
}
@Query((returns) => [Post])
getSomePosts(@Args('input') input: GetPostInput): Promise<Post[]> {
return this.postService.findSome(input)
}
@Query((returns) => Post)
getPost(@Args('id') id: number): Promise<Post> {
return this.postService.findOne(id)
}
@Mutation(() => Post, { name: 'createPost' })
createPost(@Args('input') input: CreatePostInput): Promise<Post> {
return this.postService.createOne(input)
}
}
......
......@@ -7,6 +7,7 @@ import {
UpdatePostInput,
} from './dto/post.input'
import { Post } from './model/post.entity'
import { getCurrentDate } from '../shared/utils'
@Injectable()
export class PostService {
......@@ -26,7 +27,9 @@ export class PostService {
async findSome(input: Partial<GetPostInput>): Promise<Post[]> {
return this.postRepository
.createQueryBuilder('post')
.where('post.author like :author', { author: input.author })
.where('post.id like :id', { id: input.id })
.orWhere('post.author like :author', { author: input.author })
.orWhere('post.category like :category', { category: input.category })
.getMany()
}
......@@ -37,28 +40,14 @@ export class PostService {
await queryRunner.startTransaction()
try {
result = await queryRunner.manager.save(this.postRepository.create(input))
await queryRunner.commitTransaction()
} catch (err) {
await queryRunner.rollbackTransaction()
throw err
} finally {
await queryRunner.release()
}
return result
const newInput = {
...input,
created_date: getCurrentDate(),
author: 'test',
}
async updateOne(input: UpdatePostInput): Promise<Post> {
let result
const queryRunner: QueryRunner = this.connection.createQueryRunner()
await queryRunner.connect()
await queryRunner.startTransaction()
try {
const before = await this.postRepository.findOne(input.id)
result = await queryRunner.manager.save(this.postRepository.create(input))
result = await queryRunner.manager.save(
this.postRepository.create(newInput),
)
await queryRunner.commitTransaction()
} catch (err) {
await queryRunner.rollbackTransaction()
......
......@@ -7,8 +7,21 @@ input CreateMyInput {
type: String
}
input CreatePostInput {
category: String!
content: String!
title: String!
}
input GetPostInput {
author: String
category: String
id: Float
}
type Mutation {
createMyPage(createMyInput: CreateMyInput!): MyPage!
createPost(input: CreatePostInput!): Post!
}
type MyPage {
......@@ -28,7 +41,8 @@ type Post {
}
type Query {
getAllPosts: [Post!]!
getPost(id: Float!): Post!
getPosts: [Post!]!
getSomePosts(input: GetPostInput!): [Post!]!
myPage: [MyPage!]!
}
......
export function getCurrentDate() {
const now = new Date()
const MM = now.getMonth() + 1
const DD = now.getDate()
const yyyymmdd = [
now.getFullYear(),
(MM > 9 ? '' : '0') + MM,
(DD > 9 ? '' : '0') + DD,
].join('-')
const hh = now.getHours()
const mm = now.getMinutes()
const ss = now.getSeconds()
const hhmmss = [
(hh > 9 ? '' : '0') + hh,
(mm > 9 ? '' : '0') + mm,
(ss > 9 ? '' : '0') + ss,
].join(':')
return `${yyyymmdd} ${hhmmss}`
}