From 6a3ddb4ed215b58f493158638fa0c0e8345d27c6 Mon Sep 17 00:00:00 2001
From: IGyunderov <i.gyunderov@student.utwente.nl>
Date: Mon, 6 May 2024 16:08:27 +0200
Subject: [PATCH] Issue #2 solved

---
 src/main/webapp/js/requests.js | 37 ++++++++++++++++++++++++++++++++++
 src/main/webapp/trainers.html  | 12 +++++++++++
 2 files changed, 49 insertions(+)

diff --git a/src/main/webapp/js/requests.js b/src/main/webapp/js/requests.js
index d15df8f..e2eaebf 100644
--- a/src/main/webapp/js/requests.js
+++ b/src/main/webapp/js/requests.js
@@ -111,4 +111,41 @@ function updateDetailsTrainers(trainerId) {
       </div>
     </div>
     `
+}
+
+
+function createTrainer() {
+    const name = document.getElementById('trainerName').value;
+    const profileUrl = document.getElementById('trainerProfileUrl').value;
+
+    const trainer = {
+        name: name,
+        profileUrl: profileUrl
+    };
+
+    fetch('/pokemon/api/trainers', {
+        method: 'POST',
+        headers: {
+            'Content-Type': 'application/json',
+        },
+        body: JSON.stringify(trainer),
+    })
+        .then(response => response.json())
+        .then(data => {
+            console.log('Success:', data);
+            // Refresh the trainers table
+            fetch('/pokemon/api/trainers')
+                .then(res => res.json())
+                .then(data => {
+                    pokemonTrainers = data;
+                    createPokemonTrainersTable();
+                })
+                .catch(err => {
+                    console.error(`Unable to fetch Pokemon Trainers: ${err.status}`);
+                    console.error(err);
+                });
+        })
+        .catch((error) => {
+            console.error('Error:', error);
+        });
 }
\ No newline at end of file
diff --git a/src/main/webapp/trainers.html b/src/main/webapp/trainers.html
index 48fac72..1e12a08 100644
--- a/src/main/webapp/trainers.html
+++ b/src/main/webapp/trainers.html
@@ -40,5 +40,17 @@
     </div>
 </div>
 
+<form id="createTrainerForm" onsubmit="event.preventDefault(); createTrainer();">
+    <div class="form-group col-md-6">
+        <label for="trainerName">Name</label>
+        <input type="text" class="form-control" id="trainerName" required>
+    </div>
+    <div class="form-group col-md-6">
+        <label for="trainerProfileUrl">Profile URL (optional)</label>
+        <input type="text" class="form-control" id="trainerProfileUrl">
+    </div>
+    <button type="submit" class="btn btn-primary">Create Trainer</button>
+</form>
+
 </body>
 </html>
\ No newline at end of file
-- 
GitLab