diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts index b82e7c597b4fc64e94f3929132db5d4b8e488c06..b0675bae4929516b217f87d51dfd128003bb9f69 100644 --- a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts @@ -7,6 +7,7 @@ import Exercise from '../../../sharedByClients/models/Exercise.js'; import * as Gitlab from '@gitbeaker/rest'; import TextStyle from '../../../types/TextStyle.js'; import GitlabManager from '../../../managers/GitlabManager.js'; +import inquirer from 'inquirer'; type CommandOptions = { assignment: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean } @@ -16,7 +17,7 @@ class ExerciseCreateCommand extends CommanderCommand { protected commandName: string = 'create'; private members!: Array<Gitlab.UserSchema> | undefined; - private assignment!: Assignment | undefined; + private assignment!: (Assignment & { myExercises?: Array<Exercise> }) | undefined; private exercise!: Exercise; protected defineCommand() { @@ -46,7 +47,7 @@ class ExerciseCreateCommand extends CommanderCommand { text : 'Checking if assignment exists', indent: 4 }).start(); - this.assignment = await DojoBackendManager.getAssignment(options.assignment); + this.assignment = await DojoBackendManager.getAssignment(options.assignment, true); if ( !this.assignment ) { assignmentGetSpinner.fail(`Assignment "${ options.assignment }" doesn't exists`); throw new Error(); @@ -62,6 +63,36 @@ class ExerciseCreateCommand extends CommanderCommand { throw new Error(); } assignmentPublishedSpinner.succeed(`Assignment "${ this.assignment.name }" is published`); + + if ( this.assignment.myExercises && this.assignment.myExercises.length > 0 ) { + const oraInfo = (message: string, indent: number = 12) => { + ora({ + text : message, + indent: indent + }).start().info(); + }; + + oraInfo(`${ TextStyle.WARNING('You already created ' + this.assignment.myExercises.length + ' exercises for this assignment:') }`, 4); + + for ( const exercise of this.assignment.myExercises ) { + oraInfo(`${ TextStyle.LIST_ITEM_NAME('Exercice Id:') } ${ exercise.id }`, 8); + oraInfo(`${ TextStyle.LIST_ITEM_NAME('Creation date:') } ${ exercise.gitlabCreationInfo.created_at }`); + oraInfo(`${ TextStyle.LIST_ITEM_NAME('Repository:') } ${ exercise.gitlabCreationInfo.web_url }`); + oraInfo(`${ TextStyle.LIST_ITEM_NAME('Members:') } ${ exercise.members?.map(member => member.gitlabUsername).join(', ') ?? 'There is only you' }`); + } + + const confirm: boolean = (await inquirer.prompt({ + name : 'confirm', + prefix : ' ', + message: `${ TextStyle.QUESTION('?') } You already created ${ this.assignment.myExercises.length } exercises for this assignment (see above). Are you sure you want to create a new one?`, + type : 'confirm', + default: false + })).confirm; + + if ( !confirm ) { + throw new Error(); + } + } } private async createExercise() { diff --git a/NodeApp/src/managers/DojoBackendManager.ts b/NodeApp/src/managers/DojoBackendManager.ts index a3fd87ee0f19d1db497c58c261e9c1a665cf80b9..5a2f45bad5baba805663e6f7978c70d5711de1e3 100644 --- a/NodeApp/src/managers/DojoBackendManager.ts +++ b/NodeApp/src/managers/DojoBackendManager.ts @@ -93,9 +93,9 @@ class DojoBackendManager { } - public async getAssignment(nameOrUrl: string): Promise<Assignment | undefined> { + public async getAssignment(nameOrUrl: string, getMyExercises: boolean = false): Promise<Assignment | undefined> { try { - return (await axios.get<DojoBackendResponse<Assignment>>(DojoBackendHelper.getApiUrl(ApiRoute.ASSIGNMENT_GET, { assignmentNameOrUrl: nameOrUrl }))).data.data; + return (await axios.get<DojoBackendResponse<Assignment>>(DojoBackendHelper.getApiUrl(ApiRoute.ASSIGNMENT_GET, { assignmentNameOrUrl: nameOrUrl }), getMyExercises ? { params: { getMyExercises } } : {})).data.data; } catch ( error ) { return undefined; } diff --git a/NodeApp/src/types/TextStyle.ts b/NodeApp/src/types/TextStyle.ts index e3f82ea36ea530bd395a1282dc3100f370d5a7e3..266f1e3129c6936bb1f9ac05dbd977e555976413 100644 --- a/NodeApp/src/types/TextStyle.ts +++ b/NodeApp/src/types/TextStyle.ts @@ -5,6 +5,7 @@ class TextStyle { public readonly BLOCK = chalk.cyan; public readonly CODE = chalk.bgHex('F7F7F7').grey.italic; public readonly LIST_ITEM_NAME = chalk.magenta; + public readonly QUESTION = chalk.greenBright; public readonly URL = chalk.blue.underline; public readonly WARNING = chalk.red; }