From 2d18d23946da118360dd03787ef662e35e21ff40 Mon Sep 17 00:00:00 2001 From: YR8002 <120006167+YR8002@users.noreply.github.com> Date: Wed, 15 Mar 2023 18:34:48 +0900 Subject: [PATCH 1/3] -h --- src/apis/shops/dto/create-shop.input.ts | 19 ++++++++ src/apis/shops/entities/shops.entity.ts | 47 +++++++++++++++++++ .../interfaces/shops-service.interface.ts | 5 ++ src/apis/shops/shops.module.ts | 18 +++++++ src/apis/shops/shops.resolver.ts | 24 ++++++++++ src/apis/shops/shops.service.ts | 24 ++++++++++ src/app.controller.spec.ts | 26 +++++----- src/app.controller.ts | 10 ++-- src/app.module.ts | 31 ++++++++++-- 9 files changed, 183 insertions(+), 21 deletions(-) create mode 100644 src/apis/shops/dto/create-shop.input.ts create mode 100644 src/apis/shops/entities/shops.entity.ts create mode 100644 src/apis/shops/interfaces/shops-service.interface.ts create mode 100644 src/apis/shops/shops.module.ts create mode 100644 src/apis/shops/shops.resolver.ts create mode 100644 src/apis/shops/shops.service.ts diff --git a/src/apis/shops/dto/create-shop.input.ts b/src/apis/shops/dto/create-shop.input.ts new file mode 100644 index 0000000..aef6451 --- /dev/null +++ b/src/apis/shops/dto/create-shop.input.ts @@ -0,0 +1,19 @@ +import { Field, InputType } from '@nestjs/graphql'; + +@InputType() +export class CreateShopInput { + @Field(() => String) + name: string; + + @Field(() => String) + phone: string; + + @Field(() => String) + openHour: string; + + @Field(() => String) + closeHour: string; + + @Field(() => String) + address: string; +} diff --git a/src/apis/shops/entities/shops.entity.ts b/src/apis/shops/entities/shops.entity.ts new file mode 100644 index 0000000..71deb91 --- /dev/null +++ b/src/apis/shops/entities/shops.entity.ts @@ -0,0 +1,47 @@ +import { Field, ObjectType } from '@nestjs/graphql'; +import { + Column, + DeleteDateColumn, + Entity, + OneToMany, + PrimaryGeneratedColumn, +} from 'typeorm'; + +@Entity() +@ObjectType() +export class Shop { + @PrimaryGeneratedColumn('uuid') + @Field(() => String) + id: string; + + @Column({ length: 10 }) + @Field(() => String) + name: string; + + @Column({ length: 13 }) + @Field(() => String) + phone: string; + + @Column() + @Field(() => String) + openHour: string; + + @Column() + @Field(() => String) + closeHour: string; + + @Column({ length: 100 }) + @Field(() => String) + address: string; + + @DeleteDateColumn({ nullable: true }) + @Field(() => Date) + deleteAt?: Date; + + // // 가게(own):예약 = 1:N + // @OneToMany(() => Reservation, (reservation) => reservation.id, { + // nullable: true, + // }) + // @Field(() => [Reservation]) + // reservation?: Reservation[]; +} diff --git a/src/apis/shops/interfaces/shops-service.interface.ts b/src/apis/shops/interfaces/shops-service.interface.ts new file mode 100644 index 0000000..0f6d6e8 --- /dev/null +++ b/src/apis/shops/interfaces/shops-service.interface.ts @@ -0,0 +1,5 @@ +import { CreateShopInput } from '../dto/create-shop.input'; + +export interface IShopsServiceCreate { + createShopInput: CreateShopInput; +} diff --git a/src/apis/shops/shops.module.ts b/src/apis/shops/shops.module.ts new file mode 100644 index 0000000..aafa25d --- /dev/null +++ b/src/apis/shops/shops.module.ts @@ -0,0 +1,18 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Shop } from './entities/shops.entity'; +import { ShopsResolver } from './shops.resolver'; +import { ShopsService } from './shops.service'; + +@Module({ + imports: [ + TypeOrmModule.forFeature([ + Shop, // + ]), + ], + providers: [ + ShopsResolver, // + ShopsService, + ], +}) +export class ShopsModule {} diff --git a/src/apis/shops/shops.resolver.ts b/src/apis/shops/shops.resolver.ts new file mode 100644 index 0000000..545eefd --- /dev/null +++ b/src/apis/shops/shops.resolver.ts @@ -0,0 +1,24 @@ +import { Args, Mutation, Resolver, Query } from '@nestjs/graphql'; +import { query } from 'express'; +import { CreateShopInput } from './dto/create-shop.input'; +import { Shop } from './entities/shops.entity'; +import { ShopsService } from './shops.service'; + +@Resolver() +export class ShopsResolver { + constructor( + private readonly shopsService: ShopsService, // + ) {} + + @Query(() => String) + fetchshop() { + return 'graphQL 동작을 위한 임시쿼리문입니다!!'; + } + + @Mutation(() => Shop) + createShop( + @Args('createShopInput') createShopInput: CreateShopInput, + ): Promise { + return this.shopsService.create({ createShopInput }); + } +} diff --git a/src/apis/shops/shops.service.ts b/src/apis/shops/shops.service.ts new file mode 100644 index 0000000..03fb2dc --- /dev/null +++ b/src/apis/shops/shops.service.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Shop } from './entities/shops.entity'; + +@Injectable() +export class ShopsService { + constructor( + @InjectRepository(Shop) + private readonly shopsRepository: Repository, // + ) {} + + async create({ createShopInput }): Promise { + return await this.shopsRepository.save({ ...createShopInput }); + } + + findAll() {} + + // findOne() {} + + // update() {} + + // delete() {} +} diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts index d22f389..e2c0215 100644 --- a/src/app.controller.spec.ts +++ b/src/app.controller.spec.ts @@ -3,20 +3,20 @@ import { AppController } from './app.controller'; import { AppService } from './app.service'; describe('AppController', () => { - let appController: AppController; + let appController: AppController; - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); + beforeEach(async () => { + const app: TestingModule = await Test.createTestingModule({ + controllers: [AppController], + providers: [AppService], + }).compile(); - appController = app.get(AppController); - }); + appController = app.get(AppController); + }); - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); + describe('root', () => { + it('should return "Hello World!"', () => { + expect(appController.getHello()).toBe('Hello World!'); + }); + }); }); diff --git a/src/app.controller.ts b/src/app.controller.ts index cce879e..6509517 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -3,10 +3,10 @@ import { AppService } from './app.service'; @Controller() export class AppController { - constructor(private readonly appService: AppService) {} + constructor(private readonly appService: AppService) {} - @Get() - getHello(): string { - return this.appService.getHello(); - } + @Get() + getHello(): string { + return this.appService.getHello(); + } } diff --git a/src/app.module.ts b/src/app.module.ts index 8662803..c016f2d 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,35 @@ +import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'; import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { GraphQLModule } from '@nestjs/graphql'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { ShopsModule } from './apis/shops/shops.module'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ - imports: [], - controllers: [AppController], - providers: [AppService], + imports: [ + ShopsModule, + ConfigModule.forRoot(), + GraphQLModule.forRoot({ + driver: ApolloDriver, + autoSchemaFile: true, + // autoSchemaFile: 'src/commons/graphql/schema.gql', + context: ({ req, res }) => ({ req, res }), + }), + TypeOrmModule.forRoot({ + type: process.env.DATABASE_TYPE as 'mysql', + host: process.env.DATABASE_HOST, + port: Number(process.env.DATABASE_PORT), + username: process.env.DATABASE_USERNAME, + password: process.env.DATABASE_PASSWORD, + database: process.env.DATABASE_DATABASE, + entities: [__dirname + '/apis/**/*.entity.*'], + synchronize: true, + logging: true, + }), + ], + controllers: [AppController], + providers: [AppService], }) export class AppModule {} From abcd396abf3a6467d3229914ae2e2e2503be013e Mon Sep 17 00:00:00 2001 From: YR8002 <120006167+YR8002@users.noreply.github.com> Date: Wed, 15 Mar 2023 19:07:56 +0900 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20=ED=8C=8C=EC=9D=BC=20=EC=A0=9C?= =?UTF-8?q?=EB=AA=A9=20=EC=98=A4=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit shops.entity.ts 에서 shop.entity.ts로 한 글자 수정 ㅜㅜ --- src/apis/shops/entities/shop.entity.ts | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/apis/shops/entities/shop.entity.ts diff --git a/src/apis/shops/entities/shop.entity.ts b/src/apis/shops/entities/shop.entity.ts new file mode 100644 index 0000000..71deb91 --- /dev/null +++ b/src/apis/shops/entities/shop.entity.ts @@ -0,0 +1,47 @@ +import { Field, ObjectType } from '@nestjs/graphql'; +import { + Column, + DeleteDateColumn, + Entity, + OneToMany, + PrimaryGeneratedColumn, +} from 'typeorm'; + +@Entity() +@ObjectType() +export class Shop { + @PrimaryGeneratedColumn('uuid') + @Field(() => String) + id: string; + + @Column({ length: 10 }) + @Field(() => String) + name: string; + + @Column({ length: 13 }) + @Field(() => String) + phone: string; + + @Column() + @Field(() => String) + openHour: string; + + @Column() + @Field(() => String) + closeHour: string; + + @Column({ length: 100 }) + @Field(() => String) + address: string; + + @DeleteDateColumn({ nullable: true }) + @Field(() => Date) + deleteAt?: Date; + + // // 가게(own):예약 = 1:N + // @OneToMany(() => Reservation, (reservation) => reservation.id, { + // nullable: true, + // }) + // @Field(() => [Reservation]) + // reservation?: Reservation[]; +} From 7497e293bd277a2d39be45f018104ce242eebb7d Mon Sep 17 00:00:00 2001 From: YR8002 <120006167+YR8002@users.noreply.github.com> Date: Wed, 15 Mar 2023 20:03:07 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EA=B0=80=EA=B2=8C=20API=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B0=80=EA=B2=8C=20Creat=20API,=20Read?= =?UTF-8?q?=20API=20=EC=83=9D=EC=84=B1=20=EC=99=84=EB=A3=8C=ED=96=88?= =?UTF-8?q?=EC=8A=B5=EB=8B=88=EB=8B=A4=20Read=20API=20=EB=8A=94=20fetch?= =?UTF-8?q?=EA=B0=80=20=ED=8F=AC=ED=95=A8=EB=90=9C=20API=EB=93=A4=EC=9D=84?= =?UTF-8?q?=20=EC=9D=98=EB=AF=B8=ED=95=A9=EB=8B=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/shops/entities/shops.entity.ts | 47 ------------------- .../interfaces/shops-service.interface.ts | 4 ++ src/apis/shops/shops.module.ts | 2 +- src/apis/shops/shops.resolver.ts | 28 +++++++++-- src/apis/shops/shops.service.ts | 36 ++++++++++++-- src/main.ts | 4 +- 6 files changed, 62 insertions(+), 59 deletions(-) delete mode 100644 src/apis/shops/entities/shops.entity.ts diff --git a/src/apis/shops/entities/shops.entity.ts b/src/apis/shops/entities/shops.entity.ts deleted file mode 100644 index 71deb91..0000000 --- a/src/apis/shops/entities/shops.entity.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Field, ObjectType } from '@nestjs/graphql'; -import { - Column, - DeleteDateColumn, - Entity, - OneToMany, - PrimaryGeneratedColumn, -} from 'typeorm'; - -@Entity() -@ObjectType() -export class Shop { - @PrimaryGeneratedColumn('uuid') - @Field(() => String) - id: string; - - @Column({ length: 10 }) - @Field(() => String) - name: string; - - @Column({ length: 13 }) - @Field(() => String) - phone: string; - - @Column() - @Field(() => String) - openHour: string; - - @Column() - @Field(() => String) - closeHour: string; - - @Column({ length: 100 }) - @Field(() => String) - address: string; - - @DeleteDateColumn({ nullable: true }) - @Field(() => Date) - deleteAt?: Date; - - // // 가게(own):예약 = 1:N - // @OneToMany(() => Reservation, (reservation) => reservation.id, { - // nullable: true, - // }) - // @Field(() => [Reservation]) - // reservation?: Reservation[]; -} diff --git a/src/apis/shops/interfaces/shops-service.interface.ts b/src/apis/shops/interfaces/shops-service.interface.ts index 0f6d6e8..7417571 100644 --- a/src/apis/shops/interfaces/shops-service.interface.ts +++ b/src/apis/shops/interfaces/shops-service.interface.ts @@ -3,3 +3,7 @@ import { CreateShopInput } from '../dto/create-shop.input'; export interface IShopsServiceCreate { createShopInput: CreateShopInput; } + +export interface IShopsServiceFindOne { + shopId: string; +} diff --git a/src/apis/shops/shops.module.ts b/src/apis/shops/shops.module.ts index aafa25d..5a82dc6 100644 --- a/src/apis/shops/shops.module.ts +++ b/src/apis/shops/shops.module.ts @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; -import { Shop } from './entities/shops.entity'; +import { Shop } from './entities/shop.entity'; import { ShopsResolver } from './shops.resolver'; import { ShopsService } from './shops.service'; diff --git a/src/apis/shops/shops.resolver.ts b/src/apis/shops/shops.resolver.ts index 545eefd..fa2d756 100644 --- a/src/apis/shops/shops.resolver.ts +++ b/src/apis/shops/shops.resolver.ts @@ -1,7 +1,6 @@ import { Args, Mutation, Resolver, Query } from '@nestjs/graphql'; -import { query } from 'express'; import { CreateShopInput } from './dto/create-shop.input'; -import { Shop } from './entities/shops.entity'; +import { Shop } from './entities/shop.entity'; import { ShopsService } from './shops.service'; @Resolver() @@ -10,9 +9,28 @@ export class ShopsResolver { private readonly shopsService: ShopsService, // ) {} - @Query(() => String) - fetchshop() { - return 'graphQL 동작을 위한 임시쿼리문입니다!!'; + @Query(() => [Shop]) + fetchShops(): Promise { + return this.shopsService.findAll(); + } + + @Query(() => Shop) + fetchShop( + @Args('shopId') shopId: string, // + ): Promise { + return this.shopsService.findOne({ shopId }); + } + + @Query(() => [Shop]) + fetchShopsWithDeleted(): Promise { + return this.shopsService.findAllDeleted(); + } + + @Query(() => Shop) + fetchShopWithDeleted( + @Args('shopId') shopId: string, // + ): Promise { + return this.shopsService.findOneDeleted({ shopId }); } @Mutation(() => Shop) diff --git a/src/apis/shops/shops.service.ts b/src/apis/shops/shops.service.ts index 03fb2dc..a9b77d5 100644 --- a/src/apis/shops/shops.service.ts +++ b/src/apis/shops/shops.service.ts @@ -1,7 +1,11 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { Shop } from './entities/shops.entity'; +import { Shop } from './entities/shop.entity'; +import { + IShopsServiceCreate, + IShopsServiceFindOne, +} from './interfaces/shops-service.interface'; @Injectable() export class ShopsService { @@ -10,13 +14,37 @@ export class ShopsService { private readonly shopsRepository: Repository, // ) {} - async create({ createShopInput }): Promise { + async create({ createShopInput }: IShopsServiceCreate): Promise { return await this.shopsRepository.save({ ...createShopInput }); } - findAll() {} + async findAll(): Promise { + return await this.shopsRepository.find({ + // relations: ['reservation'], + }); + } + + async findOne({ shopId }: IShopsServiceFindOne): Promise { + return await this.shopsRepository.findOne({ + where: { id: shopId }, + // relations: ['reservation'], + }); + } - // findOne() {} + async findAllDeleted(): Promise { + return await this.shopsRepository.find({ + withDeleted: true, + // relations: ['reservation'], + }); + } + + async findOneDeleted({ shopId }): Promise { + return await this.shopsRepository.findOne({ + where: { id: shopId }, + withDeleted: true, + // relations: ['reservation'], + }); + } // update() {} diff --git a/src/main.ts b/src/main.ts index f4c2401..137170f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,7 +2,7 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { - const app = await NestFactory.create(AppModule); - await app.listen(3000 ); + const app = await NestFactory.create(AppModule); + await app.listen(3000); } bootstrap();