From e4eb33f6ee70e101ede0a83af6b8997c1835d9d3 Mon Sep 17 00:00:00 2001
From: IGyunderov <i.gyunderov@student.utwente.nl>
Date: Mon, 6 May 2024 17:22:52 +0200
Subject: [PATCH] Issue #3 solved

---
 src/main/webapp/js/requests.js | 43 ++++++++++++++++++++++++++++++++++
 src/main/webapp/trainers.html  | 23 +++++++++++++++---
 2 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/src/main/webapp/js/requests.js b/src/main/webapp/js/requests.js
index e2eaebf..af25a72 100644
--- a/src/main/webapp/js/requests.js
+++ b/src/main/webapp/js/requests.js
@@ -148,4 +148,47 @@ function createTrainer() {
         .catch((error) => {
             console.error('Error:', error);
         });
+}
+
+function updateTrainer() {
+    const id = document.getElementById('updateTrainerId').value;
+    const name = document.getElementById('updateTrainerName').value;
+    const profileUrl = document.getElementById('updateTrainerProfileUrl').value;
+    const currentTrainer = pokemonTrainers?.data?.find(item => item.id == id);
+    const creationDate = currentTrainer.created;
+
+    const trainer = {
+        id: id,
+        name: name,
+        profileUrl: profileUrl,
+        created: creationDate,
+        lastUpDate: new Date().toLocaleDateString()
+
+    };
+
+    fetch(`/pokemon/api/trainers/${id}`, {
+        method: 'PUT',
+        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 1e12a08..4875778 100644
--- a/src/main/webapp/trainers.html
+++ b/src/main/webapp/trainers.html
@@ -39,8 +39,9 @@
         <div id="detailsDiv" class="col-4">No data</div>
     </div>
 </div>
-
-<form id="createTrainerForm" onsubmit="event.preventDefault(); createTrainer();">
+</br>
+<h1>Create Trainer</h1>
+<form id="createTrainerForm" onsubmit="createTrainer();">
     <div class="form-group col-md-6">
         <label for="trainerName">Name</label>
         <input type="text" class="form-control" id="trainerName" required>
@@ -51,6 +52,22 @@
     </div>
     <button type="submit" class="btn btn-primary">Create Trainer</button>
 </form>
-
+</br>
+<h1>Update Trainer</h1>
+<form id="updateTrainerForm" onsubmit="updateTrainer();">
+    <div class="form-group col-md-6">
+        <label for="updateTrainerId">ID</label>
+        <input type="text" class="form-control" id="updateTrainerId" required>
+    </div>
+    <div class="form-group col-md-6">
+        <label for="updateTrainerName">Name</label>
+        <input type="text" class="form-control" id="updateTrainerName" required>
+    </div>
+    <div class="form-group col-md-6">
+        <label for="updateTrainerProfileUrl">Profile URL (optional)</label>
+        <input type="text" class="form-control" id="updateTrainerProfileUrl">
+    </div>
+    <button type="submit" class="btn btn-primary">Update Trainer</button>
+</form>
 </body>
 </html>
\ No newline at end of file
-- 
GitLab