From 2a48a113ec11e540a85f451f01afbc4ca064c97f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me>
Date: Wed, 21 Feb 2024 00:36:15 +0100
Subject: [PATCH] DojoBackendManager => Add linkUpdateCorrection route call
 function

---
 NodeApp/src/managers/DojoBackendManager.ts | 43 ++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/NodeApp/src/managers/DojoBackendManager.ts b/NodeApp/src/managers/DojoBackendManager.ts
index b8e8a41..fcd749d 100644
--- a/NodeApp/src/managers/DojoBackendManager.ts
+++ b/NodeApp/src/managers/DojoBackendManager.ts
@@ -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;
+        }
+    }
 }
 
 
-- 
GitLab