Skip to content
Merged
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
4 changes: 2 additions & 2 deletions apps/api/src/entities/exercise.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Video } from './video.entity';

@Entity()
export class Exercise {
@PrimaryKey()
id!: number;
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;

@ManyToOne(() => ExerciseType)
exerciseType!: ExerciseType;
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/entities/exerciseType.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Exercise } from './exercise.entity';

@Entity()
export class ExerciseType {
@PrimaryKey()
id!: number;
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;

@OneToMany(
() => Exercise,
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/entities/test.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Entity, PrimaryKey, Property } from '@mikro-orm/core';

@Entity()
export class Test {
@PrimaryKey()
id!: number;
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;

@Property()
createdAt = new Date();
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/entities/video.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ interface VideoMetadata {

@Entity()
export class Video {
@PrimaryKey()
id!: number;
@PrimaryKey({ type: 'uuid', defaultRaw: 'gen_random_uuid()' })
id!: string;

@Property()
src!: string;
Expand Down
44 changes: 24 additions & 20 deletions apps/api/src/migrations/.snapshot-dropit.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"columns": {
"id": {
"name": "id",
"type": "serial",
"type": "uuid",
"unsigned": false,
"autoincrement": true,
"primary": true,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
"default": "gen_random_uuid()",
"mappedType": "uuid"
},
"name": {
"name": "name",
Expand Down Expand Up @@ -68,12 +69,13 @@
"columns": {
"id": {
"name": "id",
"type": "serial",
"type": "uuid",
"unsigned": false,
"autoincrement": true,
"primary": true,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
"default": "gen_random_uuid()",
"mappedType": "uuid"
},
"created_at": {
"name": "created_at",
Expand Down Expand Up @@ -128,12 +130,13 @@
"columns": {
"id": {
"name": "id",
"type": "serial",
"type": "uuid",
"unsigned": false,
"autoincrement": true,
"primary": true,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
"default": "gen_random_uuid()",
"mappedType": "uuid"
},
"src": {
"name": "src",
Expand Down Expand Up @@ -198,30 +201,31 @@
"columns": {
"id": {
"name": "id",
"type": "serial",
"type": "uuid",
"unsigned": false,
"autoincrement": true,
"primary": true,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
"default": "gen_random_uuid()",
"mappedType": "uuid"
},
"exercise_type_id": {
"name": "exercise_type_id",
"type": "int",
"type": "uuid",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "integer"
"mappedType": "uuid"
},
"video_id": {
"name": "video_id",
"type": "int",
"type": "uuid",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "integer"
"mappedType": "uuid"
},
"name": {
"name": "name",
Expand Down
75 changes: 75 additions & 0 deletions apps/api/src/migrations/Migration20250103192244_idNumberToUuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20250103192244_idNumberToUuid extends Migration {

override async up(): Promise<void> {
this.addSql(`alter table "exercise" drop constraint "exercise_exercise_type_id_foreign";`);
this.addSql(`alter table "exercise" drop constraint "exercise_video_id_foreign";`);

this.addSql(`alter table "exercise_type" alter column "id" drop default;`);
this.addSql(`alter table "exercise_type" alter column "id" type uuid using ("id"::text::uuid);`);
this.addSql(`alter table "exercise_type" alter column "id" set default gen_random_uuid();`);

this.addSql(`alter table "test" alter column "id" drop default;`);
this.addSql(`alter table "test" alter column "id" type uuid using ("id"::text::uuid);`);
this.addSql(`alter table "test" alter column "id" set default gen_random_uuid();`);

this.addSql(`alter table "video" alter column "id" drop default;`);
this.addSql(`alter table "video" alter column "id" type uuid using ("id"::text::uuid);`);
this.addSql(`alter table "video" alter column "id" set default gen_random_uuid();`);

this.addSql(`alter table "exercise" alter column "id" drop default;`);
this.addSql(`alter table "exercise" alter column "id" type uuid using ("id"::text::uuid);`);
this.addSql(`alter table "exercise" alter column "id" set default gen_random_uuid();`);
this.addSql(`alter table "exercise" alter column "exercise_type_id" drop default;`);
this.addSql(`alter table "exercise" alter column "exercise_type_id" type uuid using ("exercise_type_id"::text::uuid);`);
this.addSql(`alter table "exercise" alter column "video_id" drop default;`);
this.addSql(`alter table "exercise" alter column "video_id" type uuid using ("video_id"::text::uuid);`);
this.addSql(`alter table "exercise" add constraint "exercise_exercise_type_id_foreign" foreign key ("exercise_type_id") references "exercise_type" ("id") on update cascade;`);
this.addSql(`alter table "exercise" add constraint "exercise_video_id_foreign" foreign key ("video_id") references "video" ("id") on update cascade on delete set null;`);
}

override async down(): Promise<void> {
this.addSql(`alter table "exercise_type" alter column "id" type text using ("id"::text);`);

this.addSql(`alter table "test" alter column "id" type text using ("id"::text);`);

this.addSql(`alter table "video" alter column "id" type text using ("id"::text);`);

this.addSql(`alter table "exercise" alter column "id" type text using ("id"::text);`);
this.addSql(`alter table "exercise" alter column "exercise_type_id" type text using ("exercise_type_id"::text);`);
this.addSql(`alter table "exercise" alter column "video_id" type text using ("video_id"::text);`);

this.addSql(`alter table "exercise" drop constraint "exercise_exercise_type_id_foreign";`);
this.addSql(`alter table "exercise" drop constraint "exercise_video_id_foreign";`);

this.addSql(`alter table "exercise_type" alter column "id" drop default;`);
this.addSql(`alter table "exercise_type" alter column "id" type int using ("id"::int);`);
this.addSql(`create sequence if not exists "exercise_type_id_seq";`);
this.addSql(`select setval('exercise_type_id_seq', (select max("id") from "exercise_type"));`);
this.addSql(`alter table "exercise_type" alter column "id" set default nextval('exercise_type_id_seq');`);

this.addSql(`alter table "test" alter column "id" drop default;`);
this.addSql(`alter table "test" alter column "id" type int using ("id"::int);`);
this.addSql(`create sequence if not exists "test_id_seq";`);
this.addSql(`select setval('test_id_seq', (select max("id") from "test"));`);
this.addSql(`alter table "test" alter column "id" set default nextval('test_id_seq');`);

this.addSql(`alter table "video" alter column "id" drop default;`);
this.addSql(`alter table "video" alter column "id" type int using ("id"::int);`);
this.addSql(`create sequence if not exists "video_id_seq";`);
this.addSql(`select setval('video_id_seq', (select max("id") from "video"));`);
this.addSql(`alter table "video" alter column "id" set default nextval('video_id_seq');`);

this.addSql(`alter table "exercise" alter column "id" drop default;`);
this.addSql(`alter table "exercise" alter column "id" type int using ("id"::int);`);
this.addSql(`alter table "exercise" alter column "exercise_type_id" type int using ("exercise_type_id"::int);`);
this.addSql(`alter table "exercise" alter column "video_id" type int using ("video_id"::int);`);
this.addSql(`create sequence if not exists "exercise_id_seq";`);
this.addSql(`select setval('exercise_id_seq', (select max("id") from "exercise"));`);
this.addSql(`alter table "exercise" alter column "id" set default nextval('exercise_id_seq');`);
this.addSql(`alter table "exercise" add constraint "exercise_exercise_type_id_foreign" foreign key ("exercise_type_id") references "exercise_type" ("id") on update cascade;`);
this.addSql(`alter table "exercise" add constraint "exercise_video_id_foreign" foreign key ("video_id") references "video" ("id") on update cascade on delete set null;`);
}

}
13 changes: 3 additions & 10 deletions apps/api/src/modules/exercise/exercise.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ export class ExerciseController implements NestControllerInterface<typeof c> {
// Dans le contrat, pathParams = { id: z.string() }
// => on cast en number (ou on utilise z.coerce.number() dans le contrat)
try {
const id = parseInt(params.id, 10);

const exercise = await this.exerciseService.getExercise(id);
const exercise = await this.exerciseService.getExercise(params.id);

return {
status: 200 as const,
Expand Down Expand Up @@ -102,12 +100,9 @@ export class ExerciseController implements NestControllerInterface<typeof c> {
async updateExercise(
@TsRestRequest() { params, body }: RequestShapes['updateExercise']
) {
// Todo: revoir si je passe en number ou string dans le contrat
try {
const id = parseInt(params.id, 10);

const updatedExercise = await this.exerciseService.updateExercise(
id,
params.id,
body
);

Expand All @@ -132,9 +127,7 @@ export class ExerciseController implements NestControllerInterface<typeof c> {
@TsRestRequest() { params }: RequestShapes['deleteExercise']
) {
try {
const id = parseInt(params.id, 10);

await this.exerciseService.deleteExercise(id);
await this.exerciseService.deleteExercise(params.id);

return {
status: 200 as const,
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/modules/exercise/exercise.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ExerciseService {
});
}

async getExercise(id: number): Promise<ExerciseResponse> {
async getExercise(id: string): Promise<ExerciseResponse> {
const exercise = await this.em.findOne(
Exercise,
{ id },
Expand Down Expand Up @@ -121,7 +121,7 @@ export class ExerciseService {
}

async updateExercise(
id: number,
id: string,
exercise: UpdateExercise
): Promise<ExerciseResponse> {
const exerciseToUpdate = await this.em.findOne(
Expand Down Expand Up @@ -168,7 +168,7 @@ export class ExerciseService {
};
}

async deleteExercise(id: number): Promise<{ message: string }> {
async deleteExercise(id: string): Promise<{ message: string }> {
const exerciseToDelete = await this.em.findOne(Exercise, { id });

if (!exerciseToDelete) {
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/modules/exerciseType/exerciseType.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ExerciseTypeController {
}

@Get(':id')
async getExerciseType(@Param('id') id: number) {
async getExerciseType(@Param('id') id: string) {
return this.exerciseTypeService.getExerciseType(id);
}

Expand All @@ -31,14 +31,14 @@ export class ExerciseTypeController {

@Put(':id')
async updateExerciseType(
@Param('id') id: number,
@Param('id') id: string,
@Body() exerciseType: ExerciseType
) {
return this.exerciseTypeService.updateExerciseType(id, exerciseType);
}

@Delete(':id')
async deleteExerciseType(@Param('id') id: number) {
async deleteExerciseType(@Param('id') id: string) {
return this.exerciseTypeService.deleteExerciseType(id);
}
}
6 changes: 3 additions & 3 deletions apps/api/src/modules/exerciseType/exerciseType.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class ExerciseTypeService {
return this.em.find(ExerciseType, {});
}

async getExerciseType(id: number) {
async getExerciseType(id: string) {
return this.em.findOne(ExerciseType, { id });
}

Expand All @@ -22,7 +22,7 @@ export class ExerciseTypeService {
return exerciseTypeToCreate;
}

async updateExerciseType(id: number, exerciseType: ExerciseTypeDto) {
async updateExerciseType(id: string, exerciseType: ExerciseTypeDto) {
const exerciseTypeToUpdate = await this.em.findOne(ExerciseType, { id });

if (!exerciseTypeToUpdate) {
Expand All @@ -35,7 +35,7 @@ export class ExerciseTypeService {
return exerciseTypeToUpdate;
}

async deleteExerciseType(id: number) {
async deleteExerciseType(id: string) {
const exerciseTypeToDelete = await this.em.findOne(ExerciseType, { id });

if (!exerciseTypeToDelete) {
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/modules/video/video.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class VideoController {
}

@Get(':id')
async getVideo(@Param('id') id: number) {
async getVideo(@Param('id') id: string) {
return this.videoService.getVideo(id);
}

Expand All @@ -30,12 +30,12 @@ export class VideoController {
}

@Put(':id')
async updateVideo(@Param('id') id: number, @Body() video: Video) {
async updateVideo(@Param('id') id: string, @Body() video: Video) {
return this.videoService.updateVideo(id, video);
}

@Delete(':id')
async deleteVideo(@Param('id') id: number) {
async deleteVideo(@Param('id') id: string) {
return this.videoService.deleteVideo(id);
}
}
6 changes: 3 additions & 3 deletions apps/api/src/modules/video/video.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export class VideoService {
return this.em.find(Video, {});
}

async getVideo(id: number) {
async getVideo(id: string) {
return this.em.findOne(Video, { id });
}

async createVideo(video: Video) {
return this.em.persistAndFlush(video);
}

async updateVideo(id: number, video: Video) {
async updateVideo(id: string, video: Video) {
const videoToUpdate = await this.em.findOne(Video, { id });

if (!videoToUpdate) {
Expand All @@ -29,7 +29,7 @@ export class VideoService {
await this.em.flush();
}

async deleteVideo(id: number) {
async deleteVideo(id: string) {
const videoToDelete = await this.em.findOne(Video, { id });

if (!videoToDelete) {
Expand Down
Loading
Loading