diff --git a/package-lock.json b/package-lock.json index 042dbd6..9529900 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1068,6 +1068,11 @@ "uuid": "8.3.1" } }, + "@nestjs/mapped-types": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@nestjs/mapped-types/-/mapped-types-0.1.1.tgz", + "integrity": "sha512-FROYmmZ2F+tLJP/aHasPMX40iUHQPtEAzOAcfAp21baebN5iLUrdyTuphoXjIqubfPFSwtnAGpVm9kLJjQ//ig==" + }, "@nestjs/platform-express": { "version": "7.5.5", "resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-7.5.5.tgz", @@ -1118,6 +1123,16 @@ } } }, + "@nestjs/swagger": { + "version": "4.7.5", + "resolved": "https://registry.npmjs.org/@nestjs/swagger/-/swagger-4.7.5.tgz", + "integrity": "sha512-Qoj7lnvWoCIdJL50b85fdc+v2X9OY8HXaN9ngbBxAAfqLch0YfHgGdU95oBAtV6GYKbAGBqMCsmogr1TzWENwQ==", + "requires": { + "@nestjs/mapped-types": "0.1.1", + "lodash": "4.17.20", + "path-to-regexp": "3.2.0" + } + }, "@nestjs/testing": { "version": "7.5.5", "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-7.5.5.tgz", @@ -8314,6 +8329,19 @@ "supports-color": "^7.0.0" } }, + "swagger-ui-dist": { + "version": "3.37.2", + "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.37.2.tgz", + "integrity": "sha512-XIT4asxgeL4GUNPPsqpEqLt20M/u6OhFYqTh42IoEAvAyv5e9EGw5uhP9dLAD10opcMYqdkJ5qU+MpN2HZ5xyA==" + }, + "swagger-ui-express": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/swagger-ui-express/-/swagger-ui-express-4.1.5.tgz", + "integrity": "sha512-hs9OqBu2jwmhYyFUhdTiwurvbZC+bq2XnWmmbYymVdwhgJCcGkLdnqymX24ZYUve2nkYSvKPEDCo20ZF+vyw9A==", + "requires": { + "swagger-ui-dist": "^3.18.1" + } + }, "symbol-observable": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-2.0.3.tgz", diff --git a/package.json b/package.json index 7c5be86..4baaec0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app.module.ts b/src/app.module.ts index 2c98a40..96f5ff2 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,5 +1,6 @@ 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({ @@ -7,6 +8,7 @@ import { DatabaseModule } from './database/database/database.module'; ConfigModule.forRoot({ isGlobal: true, }), + MuscleModule, DatabaseModule, ], }) diff --git a/src/database/database/database.constants.ts b/src/database/database/database.constants.ts deleted file mode 100644 index 1ac44a8..0000000 --- a/src/database/database/database.constants.ts +++ /dev/null @@ -1 +0,0 @@ -export const SEQUELIZE: string = 'SEQUELIZE'; diff --git a/src/database/database/database.providers.ts b/src/database/database/database.providers.ts index 2d63788..7721219 100644 --- a/src/database/database/database.providers.ts +++ b/src/database/database/database.providers.ts @@ -1,6 +1,5 @@ import { Sequelize } from 'sequelize-typescript'; import { ConfigService } from '@nestjs/config'; -import { SEQUELIZE } from './database.constants'; export const databaseProviders: { provide: string; @@ -8,7 +7,7 @@ export const databaseProviders: { inject: typeof ConfigService[]; } = { inject: [ConfigService], - provide: SEQUELIZE, + provide: 'SEQUELIZE', useFactory: async (configService: ConfigService) => { const sequelize: Sequelize = new Sequelize({ dialect: configService.get('DATABASE_DIALECT'), @@ -32,7 +31,7 @@ export const databaseProviders: { logging: console.log, }); sequelize.addModels([]); - await sequelize.sync(); + await sequelize.sync({ alter: true }); return sequelize; }, }; diff --git a/src/main.ts b/src/main.ts index 327dbee..1464658 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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(); diff --git a/src/module/exercise/exercise.controller.ts b/src/module/exercise/exercise.controller.ts new file mode 100644 index 0000000..7cb3f0e --- /dev/null +++ b/src/module/exercise/exercise.controller.ts @@ -0,0 +1,4 @@ +import { Controller } from '@nestjs/common'; + +@Controller('exercises') +export class ExerciseController {} diff --git a/src/module/exercise/exercise.entity.ts b/src/module/exercise/exercise.entity.ts new file mode 100644 index 0000000..6390f8b --- /dev/null +++ b/src/module/exercise/exercise.entity.ts @@ -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; +} diff --git a/src/module/exercise/exercise.module.ts b/src/module/exercise/exercise.module.ts new file mode 100644 index 0000000..785870f --- /dev/null +++ b/src/module/exercise/exercise.module.ts @@ -0,0 +1,4 @@ +import { Module } from '@nestjs/common'; + +@Module({}) +export class ExerciseModule {} diff --git a/src/module/exercise/exercise.provider.ts b/src/module/exercise/exercise.provider.ts new file mode 100644 index 0000000..14888ad --- /dev/null +++ b/src/module/exercise/exercise.provider.ts @@ -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, + }, +]; diff --git a/src/module/exercise/exercise.service.ts b/src/module/exercise/exercise.service.ts new file mode 100644 index 0000000..4b92faa --- /dev/null +++ b/src/module/exercise/exercise.service.ts @@ -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, + ) {} +} diff --git a/src/module/muscle/dto/muscle.dto.ts b/src/module/muscle/dto/muscle.dto.ts new file mode 100644 index 0000000..eee35ee --- /dev/null +++ b/src/module/muscle/dto/muscle.dto.ts @@ -0,0 +1,3 @@ +export class MuscleCreateInput { + readonly parts: string; +} diff --git a/src/module/muscle/muscle.controller.ts b/src/module/muscle/muscle.controller.ts new file mode 100644 index 0000000..fff4c43 --- /dev/null +++ b/src/module/muscle/muscle.controller.ts @@ -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); + } +} diff --git a/src/module/muscle/muscle.entity.ts b/src/module/muscle/muscle.entity.ts new file mode 100644 index 0000000..6695a2f --- /dev/null +++ b/src/module/muscle/muscle.entity.ts @@ -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; +} diff --git a/src/module/muscle/muscle.module.ts b/src/module/muscle/muscle.module.ts new file mode 100644 index 0000000..af9204a --- /dev/null +++ b/src/module/muscle/muscle.module.ts @@ -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 {} diff --git a/src/module/muscle/muscle.provider.ts b/src/module/muscle/muscle.provider.ts new file mode 100644 index 0000000..c97e5df --- /dev/null +++ b/src/module/muscle/muscle.provider.ts @@ -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, + }, +]; diff --git a/src/module/muscle/muscle.service.ts b/src/module/muscle/muscle.service.ts new file mode 100644 index 0000000..e37def3 --- /dev/null +++ b/src/module/muscle/muscle.service.ts @@ -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 { + return this.muscleRepository.findAll(); + } + + async create(input: MuscleCreateInput): Promise { + return this.muscleRepository.create(input); + } +}