Skip to content

Commit

Permalink
Merge pull request #8 from liglig99/workouts-microservice
Browse files Browse the repository at this point in the history
Workouts microservice
  • Loading branch information
Felioh authored May 4, 2024
2 parents 07d48d8 + 38cac04 commit bd93e95
Show file tree
Hide file tree
Showing 21 changed files with 684 additions and 71 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*]
indent_style = space
indent_sixe = 2
152 changes: 82 additions & 70 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,72 +1,84 @@
{
"version": "2.0.0",
"problemMatcher": {
"owner": "typescript",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern":[
{
"regexp": "^ERROR in (.*?):(\\d+):(\\d+)",
"file": 1,
"line": 2,
"column": 3,
},
{
"regexp": "(.*)",
"message": 1
}
],
"background": {
"activeOnStart": false,
"beginsPattern": "^(.*?)building your sources(.*)",
"endsPattern": "^(No errors found.|Found \\d+ error in \\d+ ms.)",
},
"version": "2.0.0",
"problemMatcher": {
"owner": "typescript",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": [
{
"regexp": "^ERROR in (.*?):(\\d+):(\\d+)",
"file": 1,
"line": 2,
"column": 3
},
{
"regexp": "(.*)",
"message": 1
}
],
"background": {
"activeOnStart": false,
"beginsPattern": "^(.*?)building your sources(.*)",
"endsPattern": "^(No errors found.|Found \\d+ error in \\d+ ms.)"
}
},
"tasks": [
{
"label": "Gateway: Dev",
"type": "shell",
"command": "pnpm start:dev gateway",
"group": "none",
"presentation": {
"echo": true,
"reveal": "silent",
"revealProblems": "onProblem",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"isBackground": true
},
"tasks": [
{
"label": "Gateway: Dev",
"type": "shell",
"command": "pnpm start:dev gateway",
"group": "none",
"presentation": {
"echo": true,
"reveal": "silent",
"revealProblems": "onProblem",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"isBackground": true,
},
{
"label": "Auth: Dev",
"type": "shell",
"command": "pnpm start:dev auth",
"group": "none",
"presentation": {
"echo": true,
"reveal": "silent",
"revealProblems": "onProblem",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"isBackground": true,
},
{
"label": "All: Dev",
"dependsOn": [
"Gateway: Dev",
"Auth: Dev"
],
"group": {
"kind": "build",
"isDefault": true
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
}
{
"label": "Auth: Dev",
"type": "shell",
"command": "pnpm start:dev auth",
"group": "none",
"presentation": {
"echo": true,
"reveal": "silent",
"revealProblems": "onProblem",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"isBackground": true
},
{
"label": "Workouts: Dev",
"type": "shell",
"command": "pnpm start:dev workouts",
"group": "none",
"presentation": {
"echo": true,
"reveal": "silent",
"revealProblems": "onProblem",
"panel": "shared",
"showReuseMessage": true,
"clear": true
},
"isBackground": true
},
{
"label": "All: Dev",
"dependsOn": ["Gateway: Dev", "Auth: Dev", "Workouts: Dev"],
"group": {
"kind": "build",
"isDefault": true
},
"runOptions": {
"runOn": "folderOpen"
}
}
]
}
8 changes: 7 additions & 1 deletion apps/gateway/src/gateway.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { JwtModule } from '@nestjs/jwt';
import { jwtConstants } from 'apps/auth/src/auth.constants';
import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from '@app/common/lib/auth.guard';
import { WorkoutsController } from './workouts.controller';

@Module({
imports: [
Expand All @@ -16,13 +17,18 @@ import { AuthGuard } from '@app/common/lib/auth.guard';
transport: Transport.TCP,
options: { port: 3001 },
},
{
name: 'WORKOUTS_SERVICE',
transport: Transport.TCP,
options: { port: 3002 },
},
]),
JwtModule.register({
secret: jwtConstants.secret,
signOptions: { expiresIn: '60s' },
}),
],
controllers: [GatewayController, AuthController],
controllers: [GatewayController, AuthController, WorkoutsController],
providers: [
GatewayService,
{
Expand Down
74 changes: 74 additions & 0 deletions apps/gateway/src/workouts.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Body, Controller, Get, Inject, Post, Query } from '@nestjs/common';
import { GatewayService } from './gateway.service';
import { ClientProxy } from '@nestjs/microservices';
import { Public } from '@app/common/lib/auth.guard';
import { CreateExcerciseDto } from 'apps/workouts/src/dto/create-excercise.dto';
import { CreateWorkoutDto } from 'apps/workouts/src/dto/create-workout.dto';
import { CreateWorkoutLogDto } from 'apps/workouts/src/dto/create-workout-log.dto';

@Public() //TODO: Remove this line to make this endpoint private
@Controller('workouts')
export class WorkoutsController {
constructor(
private readonly gatewayService: GatewayService,
@Inject('WORKOUTS_SERVICE') private readonly workoutsClient: ClientProxy,
) {}

@Post('create-excercise')
async createExcercise(
@Body() createExcerciseDto: CreateExcerciseDto,
): Promise<any> {
return this.workoutsClient.send(
{ cmd: 'createExcercise' },
createExcerciseDto,
);
}

@Get('excercises')
async getExcercises(
@Query('page') page: number = 1,
@Query('limit') limit: number = 10,
@Query('filter') filter: string = '',
): Promise<any> {
return this.workoutsClient.send(
{ cmd: 'getExcercises' },
{ page, limit, filter },
);
}

@Post('create-workout')
async createWorkout(
@Body() createWorkoutDto: CreateWorkoutDto,
): Promise<any> {
return this.workoutsClient.send({ cmd: 'createWorkout' }, createWorkoutDto);
}

@Get('workouts')
async getWorkouts(
@Query('page') page: number = 1,
@Query('limit') limit: number = 10,
@Query('filter') filter: string = '',
): Promise<any> {
return this.workoutsClient.send(
{ cmd: 'getWorkouts' },
{ page, limit, filter },
);
}

@Post('save-workout')
async saveWorkout(@Body() workout: CreateWorkoutLogDto): Promise<any> {
return this.workoutsClient.send({ cmd: 'saveWorkout' }, workout);
}

@Get('workout-logs')
async getWorkoutLogs(
@Query('page') page: number = 1,
@Query('limit') limit: number = 10,
@Query('sortOrder') sortOrder: string = 'desc',
): Promise<any> {
return this.workoutsClient.send(
{ cmd: 'getWorkoutLogs' },
{ page, limit, sortOrder },
);
}
}
6 changes: 6 additions & 0 deletions apps/workouts/src/dto/create-excercise.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsString } from 'class-validator';

export class CreateExcerciseDto {
@IsString()
name: string;
}
28 changes: 28 additions & 0 deletions apps/workouts/src/dto/create-workout-log.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Type } from 'class-transformer';
import { IsDateString, IsInt, IsString, ValidateNested } from 'class-validator';

class WorkoutLogExerciseDto {
@IsString()
exercise: string;

@IsInt()
sets: number;

@IsInt()
reps: number;

@IsInt()
weight: number;
}

export class CreateWorkoutLogDto {
@IsDateString()
startTime: Date;

@IsDateString()
endTime: Date;

@ValidateNested({ each: true })
@Type(() => WorkoutLogExerciseDto)
exercises: WorkoutLogExerciseDto[];
}
22 changes: 22 additions & 0 deletions apps/workouts/src/dto/create-workout.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Type } from 'class-transformer';
import { IsInt, IsString, ValidateNested } from 'class-validator';

class WorkoutExerciseDto {
@IsString()
exercise: string;

@IsInt()
sets: number;

@IsInt()
reps: number;
}

export class CreateWorkoutDto {
@IsString()
name: string;

@ValidateNested({ each: true })
@Type(() => WorkoutExerciseDto)
excercises: WorkoutExerciseDto[];
}
12 changes: 12 additions & 0 deletions apps/workouts/src/entities/excercise.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument } from 'mongoose';

export type ExcerciseDocument = HydratedDocument<Excercise>;

@Schema()
export class Excercise {
@Prop()
name: string;
}

export const ExcerciseSchema = SchemaFactory.createForClass(Excercise);
37 changes: 37 additions & 0 deletions apps/workouts/src/entities/workout-log-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, Types } from 'mongoose';
import { Excercise } from './excercise.schema';
import { Workout } from './workout.schema';

export type WorkoutLogDocument = HydratedDocument<WorkoutLog>;

@Schema()
export class WorkoutLog {
@Prop()
startTime: Date;

@Prop()
endTime: Date;

@Prop({
type: [
{
excercise: { type: Types.ObjectId, ref: 'Excercise' },
sets: Number,
reps: Number,
weight: Number,
},
],
})
exercises: {
exercise: Types.ObjectId | Excercise;
sets: number;
reps: number;
weight: number;
}[];

@Prop({ type: Types.ObjectId, ref: 'Workout' })
workout: Types.ObjectId | Workout;
}

export const WorkoutLogSchema = SchemaFactory.createForClass(WorkoutLog);
Loading

0 comments on commit bd93e95

Please sign in to comment.