Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/apis/shops/dto/create-shop.input.ts
Original file line number Diff line number Diff line change
@@ -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;
}
47 changes: 47 additions & 0 deletions src/apis/shops/entities/shop.entity.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
9 changes: 9 additions & 0 deletions src/apis/shops/interfaces/shops-service.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CreateShopInput } from '../dto/create-shop.input';

export interface IShopsServiceCreate {
createShopInput: CreateShopInput;
}

export interface IShopsServiceFindOne {
shopId: string;
}
18 changes: 18 additions & 0 deletions src/apis/shops/shops.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
42 changes: 42 additions & 0 deletions src/apis/shops/shops.resolver.ts
Original file line number Diff line number Diff line change
@@ -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<Shop[]> {
return this.shopsService.findAll();
}

@Query(() => Shop)
fetchShop(
@Args('shopId') shopId: string, //
): Promise<Shop> {
return this.shopsService.findOne({ shopId });
}

@Query(() => [Shop])
fetchShopsWithDeleted(): Promise<Shop[]> {
return this.shopsService.findAllDeleted();
}

@Query(() => Shop)
fetchShopWithDeleted(
@Args('shopId') shopId: string, //
): Promise<Shop> {
return this.shopsService.findOneDeleted({ shopId });
}

@Mutation(() => Shop)
createShop(
@Args('createShopInput') createShopInput: CreateShopInput,
): Promise<Shop> {
return this.shopsService.create({ createShopInput });
}
}
52 changes: 52 additions & 0 deletions src/apis/shops/shops.service.ts
Original file line number Diff line number Diff line change
@@ -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<Shop>, //
) {}

async create({ createShopInput }: IShopsServiceCreate): Promise<Shop> {
return await this.shopsRepository.save({ ...createShopInput });
}

async findAll(): Promise<Shop[]> {
return await this.shopsRepository.find({
// relations: ['reservation'],
});
}

async findOne({ shopId }: IShopsServiceFindOne): Promise<Shop> {
return await this.shopsRepository.findOne({
where: { id: shopId },
// relations: ['reservation'],
});
}

async findAllDeleted(): Promise<Shop[]> {
return await this.shopsRepository.find({
withDeleted: true,
// relations: ['reservation'],
});
}

async findOneDeleted({ shopId }): Promise<Shop> {
return await this.shopsRepository.findOne({
where: { id: shopId },
withDeleted: true,
// relations: ['reservation'],
});
}

// update() {}

// delete() {}
}
26 changes: 13 additions & 13 deletions src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
appController = app.get<AppController>(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!');
});
});
});
10 changes: 5 additions & 5 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
31 changes: 28 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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<ApolloDriverConfig>({
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 {}
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();