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/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[]; +} 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..7417571 --- /dev/null +++ b/src/apis/shops/interfaces/shops-service.interface.ts @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000..5a82dc6 --- /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/shop.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..fa2d756 --- /dev/null +++ b/src/apis/shops/shops.resolver.ts @@ -0,0 +1,42 @@ +import { Args, Mutation, Resolver, Query } from '@nestjs/graphql'; +import { CreateShopInput } from './dto/create-shop.input'; +import { Shop } from './entities/shop.entity'; +import { ShopsService } from './shops.service'; + +@Resolver() +export class ShopsResolver { + constructor( + private readonly shopsService: ShopsService, // + ) {} + + @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) + 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..a9b77d5 --- /dev/null +++ b/src/apis/shops/shops.service.ts @@ -0,0 +1,52 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Shop } from './entities/shop.entity'; +import { + IShopsServiceCreate, + IShopsServiceFindOne, +} from './interfaces/shops-service.interface'; + +@Injectable() +export class ShopsService { + constructor( + @InjectRepository(Shop) + private readonly shopsRepository: Repository, // + ) {} + + async create({ createShopInput }: IShopsServiceCreate): Promise { + return await this.shopsRepository.save({ ...createShopInput }); + } + + 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'], + }); + } + + 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() {} + + // 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 {} 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();