Skip to content

Commit

Permalink
기본 구성
Browse files Browse the repository at this point in the history
  • Loading branch information
dleo9307 committed Dec 1, 2020
1 parent 58c7946 commit 8548a98
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 5 deletions.
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
"@nestjs/config": "^0.6.1",
"@nestjs/core": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"@nestjs/swagger": "^4.7.5",
"mariadb": "^2.5.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.5.4",
"sequelize": "^5.22.3",
"sequelize-typescript": "^1.1.0"
"sequelize-typescript": "^1.1.0",
"swagger-ui-express": "^4.1.5"
},
"devDependencies": {
"@types/sequelize": "^4.28.9",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { MuscleModule } from './module/muscle/muscle.module';
import { DatabaseModule } from './database/database/database.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
MuscleModule,
DatabaseModule,
],
})
Expand Down
1 change: 0 additions & 1 deletion src/database/database/database.constants.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/database/database/database.providers.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Sequelize } from 'sequelize-typescript';
import { ConfigService } from '@nestjs/config';
import { SEQUELIZE } from './database.constants';

export const databaseProviders: {
provide: string;
useFactory: (configService: ConfigService) => Promise<Sequelize>;
inject: typeof ConfigService[];
} = {
inject: [ConfigService],
provide: SEQUELIZE,
provide: 'SEQUELIZE',
useFactory: async (configService: ConfigService) => {
const sequelize: Sequelize = new Sequelize({
dialect: configService.get('DATABASE_DIALECT'),
Expand All @@ -32,7 +31,7 @@ export const databaseProviders: {
logging: console.log,
});
sequelize.addModels([]);
await sequelize.sync();
await sequelize.sync({ alter: true });
return sequelize;
},
};
11 changes: 11 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
const app: INestApplication = await NestFactory.create(AppModule);

const options: any = new DocumentBuilder()
.setTitle('Muscular API')
.setDescription('Muscular API Document')
.setVersion('1.0')
.addTag('Muscular')
.build();
const document: any = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);

await app.listen(3000);
}
bootstrap();
4 changes: 4 additions & 0 deletions src/module/exercise/exercise.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('exercises')
export class ExerciseController {}
47 changes: 47 additions & 0 deletions src/module/exercise/exercise.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
AllowNull,
AutoIncrement,
Column,
ForeignKey,
Model,
PrimaryKey,
Table,
} from 'sequelize-typescript';
import { Muscle } from '../muscle/muscle.entity';

@Table
export class Exercise extends Model {
@PrimaryKey
@AutoIncrement
@Column
id: number;

@AllowNull(false)
@Column
@ForeignKey(() => Muscle)
muscleId: number;

@AllowNull(false)
@Column
name: string;

@Column
machine?: string;

@AllowNull(false)
@Column
round: number;

@AllowNull(false)
@Column
reps: number;

@Column
@AllowNull(false)
@Column
createdAt: Date;

@AllowNull(false)
@Column
updatedAt: Date;
}
4 changes: 4 additions & 0 deletions src/module/exercise/exercise.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Module } from '@nestjs/common';

@Module({})
export class ExerciseModule {}
9 changes: 9 additions & 0 deletions src/module/exercise/exercise.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Model } from 'sequelize-typescript';
import { Exercise } from './exercise.entity';

export const exerciseProviders: { useValue: typeof Model; provide: string }[] = [
{
provide: 'EXERCISE_REPOSITORY',
useValue: Exercise,
},
];
13 changes: 13 additions & 0 deletions src/module/exercise/exercise.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Inject, Injectable } from '@nestjs/common';
import { Exercise } from './exercise.entity';

import { Sequelize } from 'sequelize-typescript';

@Injectable()
export class ExerciseService {
constructor(
@Inject('EXERCISE_REPOSITORY')
private readonly exerciseRepository: typeof Exercise,
@Inject('SEQUELIZE') private readonly sequelizeProvider: Sequelize,
) {}
}
3 changes: 3 additions & 0 deletions src/module/muscle/dto/muscle.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class MuscleCreateInput {
readonly parts: string;
}
13 changes: 13 additions & 0 deletions src/module/muscle/muscle.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Body, Controller, Post } from '@nestjs/common';
import { MuscleService } from './muscle.service';
import { MuscleCreateInput } from './dto/muscle.dto';

@Controller('muscles')
export class MuscleController {
constructor(private readonly muscleService: MuscleService) {}

@Post()
async create(@Body() muscleCreateInput: MuscleCreateInput) {
return this.muscleService.create(muscleCreateInput);
}
}
21 changes: 21 additions & 0 deletions src/module/muscle/muscle.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AllowNull, AutoIncrement, Column, Model, PrimaryKey, Table } from 'sequelize-typescript';

@Table
export class Muscle extends Model {
@PrimaryKey
@AutoIncrement
@Column
id: number;

@AllowNull(false)
@Column
parts: string;

@AllowNull(false)
@Column
createdAt: Date;

@AllowNull(false)
@Column
updatedAt: Date;
}
10 changes: 10 additions & 0 deletions src/module/muscle/muscle.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { MuscleService } from './muscle.service';
import { MuscleController } from './muscle.controller';
import { muscleProviders } from './muscle.provider';

@Module({
providers: [MuscleService, ...muscleProviders],
controllers: [MuscleController],
})
export class MuscleModule {}
9 changes: 9 additions & 0 deletions src/module/muscle/muscle.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Model } from 'sequelize-typescript';
import { Muscle } from './muscle.entity';

export const muscleProviders: { useValue: typeof Model; provide: string }[] = [
{
provide: 'MUSCLE_REPOSITORY',
useValue: Muscle,
},
];
21 changes: 21 additions & 0 deletions src/module/muscle/muscle.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Inject, Injectable } from '@nestjs/common';
import { Sequelize } from 'sequelize-typescript';
import { Muscle } from './muscle.entity';
import { MuscleCreateInput } from './dto/muscle.dto';

@Injectable()
export class MuscleService {
constructor(
@Inject('MUSCLE_REPOSITORY')
private readonly muscleRepository: typeof Muscle,
@Inject('SEQUELIZE') private readonly sequelizeProvider: Sequelize,
) {}

async getPage(): Promise<Muscle[]> {
return this.muscleRepository.findAll();
}

async create(input: MuscleCreateInput): Promise<Muscle> {
return this.muscleRepository.create(input);
}
}

0 comments on commit 8548a98

Please sign in to comment.