Showing
28 changed files
with
203 additions
and
129 deletions
... | @@ -22,5 +22,12 @@ module.exports = { | ... | @@ -22,5 +22,12 @@ module.exports = { |
22 | '@typescript-eslint/explicit-module-boundary-types': 'off', | 22 | '@typescript-eslint/explicit-module-boundary-types': 'off', |
23 | '@typescript-eslint/no-explicit-any': 'off', | 23 | '@typescript-eslint/no-explicit-any': 'off', |
24 | '@typescript-eslint/no-unused-vars': 'off', | 24 | '@typescript-eslint/no-unused-vars': 'off', |
25 | + semi: [2, 'never'], | ||
26 | + 'prettier/prettier': [ | ||
27 | + 'error', | ||
28 | + { | ||
29 | + semi: false, | ||
30 | + }, | ||
31 | + ], | ||
25 | }, | 32 | }, |
26 | -}; | 33 | +} | ... | ... |
No preview for this file type
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { AppController } from './app.controller'; | 2 | +import { AppController } from './app.controller' |
3 | -import { AppService } from './app.service'; | 3 | +import { AppService } from './app.service' |
4 | 4 | ||
5 | describe('AppController', () => { | 5 | describe('AppController', () => { |
6 | - let appController: AppController; | 6 | + let appController: AppController |
7 | 7 | ||
8 | beforeEach(async () => { | 8 | beforeEach(async () => { |
9 | const app: TestingModule = await Test.createTestingModule({ | 9 | const app: TestingModule = await Test.createTestingModule({ |
10 | controllers: [AppController], | 10 | controllers: [AppController], |
11 | providers: [AppService], | 11 | providers: [AppService], |
12 | - }).compile(); | 12 | + }).compile() |
13 | 13 | ||
14 | - appController = app.get<AppController>(AppController); | 14 | + appController = app.get<AppController>(AppController) |
15 | - }); | 15 | + }) |
16 | 16 | ||
17 | describe('root', () => { | 17 | describe('root', () => { |
18 | it('should return "Hello World!"', () => { | 18 | it('should return "Hello World!"', () => { |
19 | - expect(appController.getHello()).toBe('Hello World!'); | 19 | + expect(appController.getHello()).toBe('Hello World!') |
20 | - }); | 20 | + }) |
21 | - }); | 21 | + }) |
22 | -}); | 22 | +}) | ... | ... |
1 | -import { Controller, Get } from '@nestjs/common'; | 1 | +import { Controller, Get } from '@nestjs/common' |
2 | -import { AppService } from './app.service'; | 2 | +import { AppService } from './app.service' |
3 | 3 | ||
4 | @Controller() | 4 | @Controller() |
5 | export class AppController { | 5 | export class AppController { |
... | @@ -7,6 +7,6 @@ export class AppController { | ... | @@ -7,6 +7,6 @@ export class AppController { |
7 | 7 | ||
8 | @Get() | 8 | @Get() |
9 | getHello(): string { | 9 | getHello(): string { |
10 | - return this.appService.getHello(); | 10 | + return this.appService.getHello() |
11 | } | 11 | } |
12 | } | 12 | } | ... | ... |
1 | -import { Module } from '@nestjs/common'; | 1 | +import { Module } from '@nestjs/common' |
2 | -import { GraphQLModule } from '@nestjs/graphql'; | 2 | +import { GraphQLModule } from '@nestjs/graphql' |
3 | -import { TypeOrmModule } from '@nestjs/typeorm'; | 3 | +import { TypeOrmModule } from '@nestjs/typeorm' |
4 | -import { join } from 'path'; | 4 | +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 | +import { PostModule } from './post/post.module' |
9 | -import { CommentModule } from './comment/comment.module'; | 9 | +import { CommentModule } from './comment/comment.module' |
10 | -import { UserModule } from './user/user.module'; | 10 | +import { UserModule } from './user/user.module' |
11 | -import { LikeableService } from './likeable/likeable.service'; | 11 | +import { LikeableService } from './likeable/likeable.service' |
12 | -import { LikeableModule } from './likeable/likeable.module'; | 12 | +import { LikeableModule } from './likeable/likeable.module' |
13 | 13 | ||
14 | @Module({ | 14 | @Module({ |
15 | imports: [ | 15 | imports: [ | ... | ... |
1 | -import { Injectable } from '@nestjs/common'; | 1 | +import { Injectable } from '@nestjs/common' |
2 | 2 | ||
3 | @Injectable() | 3 | @Injectable() |
4 | export class AppService { | 4 | export class AppService { |
5 | getHello(): string { | 5 | getHello(): string { |
6 | - return 'Hello World!'; | 6 | + return 'Hello World!' |
7 | } | 7 | } |
8 | } | 8 | } | ... | ... |
1 | -import { Module } from '@nestjs/common'; | 1 | +import { Module } from '@nestjs/common' |
2 | -import { CommentService } from './comment.service'; | 2 | +import { CommentService } from './comment.service' |
3 | -import { CommentResolver } from './comment.resolver'; | 3 | +import { CommentResolver } from './comment.resolver' |
4 | 4 | ||
5 | @Module({ | 5 | @Module({ |
6 | providers: [CommentService, CommentResolver], | 6 | providers: [CommentService, CommentResolver], | ... | ... |
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { CommentResolver } from './comment.resolver'; | 2 | +import { CommentResolver } from './comment.resolver' |
3 | 3 | ||
4 | describe('CommentResolver', () => { | 4 | describe('CommentResolver', () => { |
5 | - let resolver: CommentResolver; | 5 | + let resolver: CommentResolver |
6 | 6 | ||
7 | beforeEach(async () => { | 7 | beforeEach(async () => { |
8 | const module: TestingModule = await Test.createTestingModule({ | 8 | const module: TestingModule = await Test.createTestingModule({ |
9 | providers: [CommentResolver], | 9 | providers: [CommentResolver], |
10 | - }).compile(); | 10 | + }).compile() |
11 | 11 | ||
12 | - resolver = module.get<CommentResolver>(CommentResolver); | 12 | + resolver = module.get<CommentResolver>(CommentResolver) |
13 | - }); | 13 | + }) |
14 | 14 | ||
15 | it('should be defined', () => { | 15 | it('should be defined', () => { |
16 | - expect(resolver).toBeDefined(); | 16 | + expect(resolver).toBeDefined() |
17 | - }); | 17 | + }) |
18 | -}); | 18 | +}) | ... | ... |
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { CommentService } from './comment.service'; | 2 | +import { CommentService } from './comment.service' |
3 | 3 | ||
4 | describe('CommentService', () => { | 4 | describe('CommentService', () => { |
5 | - let service: CommentService; | 5 | + let service: CommentService |
6 | 6 | ||
7 | beforeEach(async () => { | 7 | beforeEach(async () => { |
8 | const module: TestingModule = await Test.createTestingModule({ | 8 | const module: TestingModule = await Test.createTestingModule({ |
9 | providers: [CommentService], | 9 | providers: [CommentService], |
10 | - }).compile(); | 10 | + }).compile() |
11 | 11 | ||
12 | - service = module.get<CommentService>(CommentService); | 12 | + service = module.get<CommentService>(CommentService) |
13 | - }); | 13 | + }) |
14 | 14 | ||
15 | it('should be defined', () => { | 15 | it('should be defined', () => { |
16 | - expect(service).toBeDefined(); | 16 | + expect(service).toBeDefined() |
17 | - }); | 17 | + }) |
18 | -}); | 18 | +}) | ... | ... |
1 | -import { Module } from '@nestjs/common'; | 1 | +import { Module } from '@nestjs/common' |
2 | -import { LikeableResolver } from './likeable.resolver'; | 2 | +import { LikeableResolver } from './likeable.resolver' |
3 | 3 | ||
4 | @Module({ | 4 | @Module({ |
5 | providers: [LikeableResolver], | 5 | providers: [LikeableResolver], | ... | ... |
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { LikeableResolver } from './likeable.resolver'; | 2 | +import { LikeableResolver } from './likeable.resolver' |
3 | 3 | ||
4 | describe('LikeableResolver', () => { | 4 | describe('LikeableResolver', () => { |
5 | - let resolver: LikeableResolver; | 5 | + let resolver: LikeableResolver |
6 | 6 | ||
7 | beforeEach(async () => { | 7 | beforeEach(async () => { |
8 | const module: TestingModule = await Test.createTestingModule({ | 8 | const module: TestingModule = await Test.createTestingModule({ |
9 | providers: [LikeableResolver], | 9 | providers: [LikeableResolver], |
10 | - }).compile(); | 10 | + }).compile() |
11 | 11 | ||
12 | - resolver = module.get<LikeableResolver>(LikeableResolver); | 12 | + resolver = module.get<LikeableResolver>(LikeableResolver) |
13 | - }); | 13 | + }) |
14 | 14 | ||
15 | it('should be defined', () => { | 15 | it('should be defined', () => { |
16 | - expect(resolver).toBeDefined(); | 16 | + expect(resolver).toBeDefined() |
17 | - }); | 17 | + }) |
18 | -}); | 18 | +}) | ... | ... |
1 | -import { Field, InputType } from '@nestjs/graphql'; | 1 | +import { Field, InputType, PartialType, PickType } from '@nestjs/graphql'; |
2 | -import { Entity } from 'typeorm'; | 2 | +import { IsNumber } from 'class-validator'; |
3 | +import { Post } from '../model/post.entity'; | ||
3 | 4 | ||
4 | -@Entity() | ||
5 | @InputType() | 5 | @InputType() |
6 | export class GetPostInput { | 6 | export class GetPostInput { |
7 | @Field() | 7 | @Field() |
... | @@ -13,3 +13,17 @@ export class GetPostInput { | ... | @@ -13,3 +13,17 @@ export class GetPostInput { |
13 | @Field({ nullable: true }) | 13 | @Field({ nullable: true }) |
14 | category?: string; | 14 | category?: string; |
15 | } | 15 | } |
16 | + | ||
17 | +@InputType() | ||
18 | +export class CreatePostInput extends PickType( | ||
19 | + Post, | ||
20 | + ['category', 'content', 'title'], | ||
21 | + InputType, | ||
22 | +) {} | ||
23 | + | ||
24 | +@InputType() | ||
25 | +export class UpdatePostInput extends PartialType(CreatePostInput) { | ||
26 | + @Field() | ||
27 | + @IsNumber() | ||
28 | + id: number; | ||
29 | +} | ... | ... |
File moved
1 | -import { Module } from '@nestjs/common'; | 1 | +import { Module } from '@nestjs/common' |
2 | -import { PostService } from './post.service'; | 2 | +import { PostService } from './post.service' |
3 | -import { PostResolver } from './post.resolver'; | 3 | +import { PostResolver } from './post.resolver' |
4 | -import { TypeOrmModule } from '@nestjs/typeorm'; | 4 | +import { TypeOrmModule } from '@nestjs/typeorm' |
5 | -import { Post } from './post.entity'; | 5 | +import { Post } from './model/post.entity' |
6 | 6 | ||
7 | @Module({ | 7 | @Module({ |
8 | imports: [TypeOrmModule.forFeature([Post])], | 8 | imports: [TypeOrmModule.forFeature([Post])], | ... | ... |
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { PostResolver } from './post.resolver'; | 2 | +import { PostResolver } from './post.resolver' |
3 | 3 | ||
4 | describe('PostResolver', () => { | 4 | describe('PostResolver', () => { |
5 | - let resolver: PostResolver; | 5 | + let resolver: PostResolver |
6 | 6 | ||
7 | beforeEach(async () => { | 7 | beforeEach(async () => { |
8 | const module: TestingModule = await Test.createTestingModule({ | 8 | const module: TestingModule = await Test.createTestingModule({ |
9 | providers: [PostResolver], | 9 | providers: [PostResolver], |
10 | - }).compile(); | 10 | + }).compile() |
11 | 11 | ||
12 | - resolver = module.get<PostResolver>(PostResolver); | 12 | + resolver = module.get<PostResolver>(PostResolver) |
13 | - }); | 13 | + }) |
14 | 14 | ||
15 | it('should be defined', () => { | 15 | it('should be defined', () => { |
16 | - expect(resolver).toBeDefined(); | 16 | + expect(resolver).toBeDefined() |
17 | - }); | 17 | + }) |
18 | -}); | 18 | +}) | ... | ... |
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 './post.entity'; | 3 | +import { Post } from './model/post.entity' |
4 | -import { GetPostInput } from './dto/post.input'; | 4 | +import { GetPostInput } from './dto/post.input' |
5 | 5 | ||
6 | @Resolver((of) => Post) | 6 | @Resolver((of) => Post) |
7 | export class PostResolver { | 7 | export class PostResolver { |
... | @@ -9,11 +9,11 @@ export class PostResolver { | ... | @@ -9,11 +9,11 @@ export class PostResolver { |
9 | 9 | ||
10 | @Query((returns) => [Post]) | 10 | @Query((returns) => [Post]) |
11 | getPosts(): Promise<Post[]> { | 11 | getPosts(): Promise<Post[]> { |
12 | - return this.postService.findAll(); | 12 | + return this.postService.findAll() |
13 | } | 13 | } |
14 | 14 | ||
15 | @Query((returns) => Post) | 15 | @Query((returns) => Post) |
16 | getPost(@Args('id') id: number): Promise<Post> { | 16 | getPost(@Args('id') id: number): Promise<Post> { |
17 | - return this.postService.findOne(id); | 17 | + return this.postService.findOne(id) |
18 | } | 18 | } |
19 | } | 19 | } | ... | ... |
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { PostService } from './post.service'; | 2 | +import { PostService } from './post.service' |
3 | 3 | ||
4 | describe('PostService', () => { | 4 | describe('PostService', () => { |
5 | - let service: PostService; | 5 | + let service: PostService |
6 | 6 | ||
7 | beforeEach(async () => { | 7 | beforeEach(async () => { |
8 | const module: TestingModule = await Test.createTestingModule({ | 8 | const module: TestingModule = await Test.createTestingModule({ |
9 | providers: [PostService], | 9 | providers: [PostService], |
10 | - }).compile(); | 10 | + }).compile() |
11 | 11 | ||
12 | - service = module.get<PostService>(PostService); | 12 | + service = module.get<PostService>(PostService) |
13 | - }); | 13 | + }) |
14 | 14 | ||
15 | it('should be defined', () => { | 15 | it('should be defined', () => { |
16 | - expect(service).toBeDefined(); | 16 | + expect(service).toBeDefined() |
17 | - }); | 17 | + }) |
18 | -}); | 18 | +}) | ... | ... |
1 | -import { Injectable } from '@nestjs/common'; | 1 | +import { Injectable } from '@nestjs/common' |
2 | -import { InjectRepository } from '@nestjs/typeorm'; | 2 | +import { InjectRepository } from '@nestjs/typeorm' |
3 | -import { Repository } from 'typeorm'; | 3 | +import { QueryRunner, Repository, getConnection } from 'typeorm' |
4 | -import { GetPostInput } from './dto/post.input'; | 4 | +import { |
5 | -import { Post } from './post.entity'; | 5 | + CreatePostInput, |
6 | + GetPostInput, | ||
7 | + UpdatePostInput, | ||
8 | +} from './dto/post.input' | ||
9 | +import { Post } from './model/post.entity' | ||
6 | 10 | ||
7 | @Injectable() | 11 | @Injectable() |
8 | export class PostService { | 12 | export class PostService { |
9 | constructor( | 13 | constructor( |
10 | @InjectRepository(Post) private postRepository: Repository<Post>, | 14 | @InjectRepository(Post) private postRepository: Repository<Post>, |
11 | ) {} | 15 | ) {} |
16 | + private connection = getConnection() | ||
12 | 17 | ||
13 | async findAll(): Promise<Post[]> { | 18 | async findAll(): Promise<Post[]> { |
14 | - return this.postRepository.find(); | 19 | + return this.postRepository.find() |
15 | } | 20 | } |
16 | 21 | ||
17 | async findOne(id: number): Promise<Post> { | 22 | async findOne(id: number): Promise<Post> { |
18 | - return this.postRepository.findOne(id); | 23 | + return this.postRepository.findOne(id) |
24 | + } | ||
25 | + | ||
26 | + async findSome(input: Partial<GetPostInput>): Promise<Post[]> { | ||
27 | + return this.postRepository | ||
28 | + .createQueryBuilder('post') | ||
29 | + .where('post.author like :author', { author: input.author }) | ||
30 | + .getMany() | ||
31 | + } | ||
32 | + | ||
33 | + async createOne(input: CreatePostInput): Promise<Post> { | ||
34 | + let result | ||
35 | + const queryRunner: QueryRunner = this.connection.createQueryRunner() | ||
36 | + await queryRunner.connect() | ||
37 | + await queryRunner.startTransaction() | ||
38 | + | ||
39 | + try { | ||
40 | + result = await queryRunner.manager.save(this.postRepository.create(input)) | ||
41 | + await queryRunner.commitTransaction() | ||
42 | + } catch (err) { | ||
43 | + await queryRunner.rollbackTransaction() | ||
44 | + throw err | ||
45 | + } finally { | ||
46 | + await queryRunner.release() | ||
47 | + } | ||
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() | ||
63 | + } catch (err) { | ||
64 | + await queryRunner.rollbackTransaction() | ||
65 | + throw err | ||
66 | + } finally { | ||
67 | + await queryRunner.release() | ||
68 | + } | ||
69 | + | ||
70 | + return result | ||
19 | } | 71 | } |
20 | } | 72 | } | ... | ... |
1 | -import { Module } from '@nestjs/common'; | 1 | +import { Module } from '@nestjs/common' |
2 | -import { UserResolver } from './user.resolver'; | 2 | +import { UserResolver } from './user.resolver' |
3 | -import { UserService } from './user.service'; | 3 | +import { UserService } from './user.service' |
4 | 4 | ||
5 | @Module({ | 5 | @Module({ |
6 | - providers: [UserResolver, UserService] | 6 | + providers: [UserResolver, UserService], |
7 | }) | 7 | }) |
8 | export class UserModule {} | 8 | export class UserModule {} | ... | ... |
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { UserResolver } from './user.resolver'; | 2 | +import { UserResolver } from './user.resolver' |
3 | 3 | ||
4 | describe('UserResolver', () => { | 4 | describe('UserResolver', () => { |
5 | - let resolver: UserResolver; | 5 | + let resolver: UserResolver |
6 | 6 | ||
7 | beforeEach(async () => { | 7 | beforeEach(async () => { |
8 | const module: TestingModule = await Test.createTestingModule({ | 8 | const module: TestingModule = await Test.createTestingModule({ |
9 | providers: [UserResolver], | 9 | providers: [UserResolver], |
10 | - }).compile(); | 10 | + }).compile() |
11 | 11 | ||
12 | - resolver = module.get<UserResolver>(UserResolver); | 12 | + resolver = module.get<UserResolver>(UserResolver) |
13 | - }); | 13 | + }) |
14 | 14 | ||
15 | it('should be defined', () => { | 15 | it('should be defined', () => { |
16 | - expect(resolver).toBeDefined(); | 16 | + expect(resolver).toBeDefined() |
17 | - }); | 17 | + }) |
18 | -}); | 18 | +}) | ... | ... |
1 | -import { Test, TestingModule } from '@nestjs/testing'; | 1 | +import { Test, TestingModule } from '@nestjs/testing' |
2 | -import { UserService } from './user.service'; | 2 | +import { UserService } from './user.service' |
3 | 3 | ||
4 | describe('UserService', () => { | 4 | describe('UserService', () => { |
5 | - let service: UserService; | 5 | + let service: UserService |
6 | 6 | ||
7 | beforeEach(async () => { | 7 | beforeEach(async () => { |
8 | const module: TestingModule = await Test.createTestingModule({ | 8 | const module: TestingModule = await Test.createTestingModule({ |
9 | providers: [UserService], | 9 | providers: [UserService], |
10 | - }).compile(); | 10 | + }).compile() |
11 | 11 | ||
12 | - service = module.get<UserService>(UserService); | 12 | + service = module.get<UserService>(UserService) |
13 | - }); | 13 | + }) |
14 | 14 | ||
15 | it('should be defined', () => { | 15 | it('should be defined', () => { |
16 | - expect(service).toBeDefined(); | 16 | + expect(service).toBeDefined() |
17 | - }); | 17 | + }) |
18 | -}); | 18 | +}) | ... | ... |
-
Please register or login to post a comment