Skip to content
Snippets Groups Projects
Commit 2a48a113 authored by michael.minelli's avatar michael.minelli
Browse files

DojoBackendManager => Add linkUpdateCorrection route call function

parent ca770491
No related branches found
No related tags found
No related merge requests found
......@@ -192,6 +192,49 @@ class DojoBackendManager {
throw error;
}
}
public async linkUpdateCorrection(exerciseIdOrUrl: string, assignment: Assignment, isUpdate: boolean, verbose: boolean = true): Promise<boolean> {
const spinner: ora.Ora = ora(`${ isUpdate ? 'Updating' : 'Linking' } correction`);
if ( verbose ) {
spinner.start();
}
try {
const axiosFunction = isUpdate ? axios.patch : axios.post;
const route = isUpdate ? ApiRoute.ASSIGNMENT_CORRECTION_UPDATE : ApiRoute.ASSIGNMENT_CORRECTION_LINK;
await axiosFunction(this.getApiUrl(route).replace('{{assignmentNameOrUrl}}', encodeURIComponent(assignment.name)).replace('{{exerciseIdOrUrl}}', encodeURIComponent(exerciseIdOrUrl)), {
exerciseIdOrUrl: exerciseIdOrUrl
});
if ( verbose ) {
spinner.succeed(`Correction ${ isUpdate ? 'updated' : 'linked' }`);
}
return true;
} catch ( error ) {
if ( verbose ) {
if ( error instanceof AxiosError ) {
if ( error.response?.data ) {
if ( error.response.data.code === DojoStatusCode.ASSIGNMENT_EXERCISE_NOT_RELATED ) {
spinner.fail(`The exercise does not belong to the assignment.`);
} else if ( error.response.data.code === DojoStatusCode.EXERCISE_CORRECTION_ALREADY_EXIST ) {
spinner.fail(`This exercise is already labelled as a correction. If you want to update it, please use the update command.`);
} else if ( error.response.data.code === DojoStatusCode.EXERCISE_CORRECTION_NOT_EXIST ) {
spinner.fail(`The exercise is not labelled as a correction so it's not possible to update it.`);
}
} else {
spinner.fail(`Correction ${ isUpdate ? 'update' : 'link' } error: ${ error.response?.statusText }`);
}
} else {
spinner.fail(`Correction ${ isUpdate ? 'update' : 'link' } error: ${ error }`);
}
}
return false;
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment