From 4990ada171c61ad54a40819874c393cf5b1285b0 Mon Sep 17 00:00:00 2001
From: Rosanny <rosanny.sihombing@hft-stuttgart.de>
Date: Thu, 11 Feb 2021 18:57:28 +0100
Subject: [PATCH] update createNewPages and add updateProject

---
 routes/gitlab.js | 62 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 50 insertions(+), 12 deletions(-)

diff --git a/routes/gitlab.js b/routes/gitlab.js
index 67b300a0..49e89807 100644
--- a/routes/gitlab.js
+++ b/routes/gitlab.js
@@ -27,15 +27,14 @@ var gitlab = {
             callback(res)
         })
     },
-    // todo: fixing callback
-    createNewPages: function(newPagesdata, callback) {
+    createNewPages: function(newPagesData, newLogoFile, callback) {
         let data = new formData()
-        data.append('avatar', fs.createReadStream(newPagesdata.avatar))
+        data.append('avatar', fs.createReadStream(newLogoFile))
 
         let dataConfig = {
             method: 'post',
-            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/user/'+newPagesdata.gitlabUserId+
-                '?name='+newPagesdata.name+'&description='+newPagesdata.description+'&tag_list=website'+
+            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/user/'+newPagesData.getOwnerGitlabId()+
+                '?name='+newPagesData.getName()+'&description='+newPagesData.getDesc()+'&tag_list=website'+
                 '&use_custom_template=true&template_name=page_basic',
             headers: { 
                 'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects, 
@@ -45,13 +44,52 @@ var gitlab = {
         }
 
         axios(dataConfig)
-        .then(function (response) {
-            callback(response.data)
+        .then(response => {
+            let res = {
+                error: false,
+                data: response.data}
+            callback(res)
+        })
+        .catch(err => {
+            console.log(err)
+            let res = {
+                error: true,
+                data: err.response.data}
+            callback(res)
+        })
+    },
+    updateProject: function(updatedProjectData, newLogoFile, callback){
+        console.log(updatedProjectData)
+        let data = new formData()
+        if (newLogoFile) {
+            data.append('avatar', fs.createReadStream(newLogoFile))
+        }
+        
+        let dataConfig = {
+            method: 'put',
+            url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects/'+updatedProjectData.getId()+
+                '?name='+updatedProjectData.getName()+'&description='+updatedProjectData.getDesc(),
+            headers: { 
+                'Authorization': 'Bearer '+config.gitlab.token_readWriteProjects, 
+                ...data.getHeaders()
+            },
+            data : data
+        }
+
+        axios(dataConfig)
+        .then(response => {
+            let res = {
+                error: false,
+                data: response.data}
+            callback(res)
+        })
+        .catch(err => {
+            console.log(err)
+            let res = {
+                error: true,
+                data: err.response.data}
+            callback(res)
         })
-        .catch(function (err) {
-            if(err)
-                callback(err.response.data)
-        }) 
     },
     getUserProjects: function(gitlabUserId, callback) {
         axios.get('https://transfer.hft-stuttgart.de/gitlab/api/v4/users/'+gitlabUserId+'/projects?private_token='+
@@ -65,7 +103,7 @@ var gitlab = {
         .catch(err => {
             let res = {
                 error: true,
-                data: err}
+                data: err.message}
             callback(res)
         })
     },
-- 
GitLab