From a6ce2cf0e89284887dfa90c56888a4f13d228768 Mon Sep 17 00:00:00 2001 From: Rohan-uel Date: Thu, 5 Dec 2024 23:14:46 +0000 Subject: [PATCH] Update add_book.js Allow admins to update book information in the library catalog. --- js/add_book.js | 99 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/js/add_book.js b/js/add_book.js index 1d9abfb..62daf66 100644 --- a/js/add_book.js +++ b/js/add_book.js @@ -1,36 +1,36 @@ + $(document).ready(function() { var firebaseConfig = { - apiKey: "AIzaSyBin1evT-H6jfR49WIhtVPsGMLzbEklIQY", - authDomain: "library-management-syste-f2a85.firebaseapp.com", - databaseURL: "https://library-management-syste-f2a85.firebaseio.com", - projectId: "library-management-syste-f2a85", - storageBucket: "library-management-syste-f2a85.appspot.com", - messagingSenderId: "914416876417", - appId: "1:914416876417:web:bf9e7762c1c283ba" + apiKey: "AIzaSyBA2O5kze6cSrMqrUpDYoP5KZepBsNEa4k", + authDomain: "library-management-syste-37dc2.firebaseapp.com", + databaseURL: "https://library-management-syste-37dc2-default-rtdb.firebaseio.com", + projectId: "library-management-syste-37dc2", + storageBucket: "library-management-syste-37dc2.firebasestorage.app", + messagingSenderId: "962676782645", + appId: "1:962676782645:web:c008daf774bcee5a80e37c", + measurementId: "G-NZ9VJF0LJ2" }; + // Initialize Firebase firebase.initializeApp(firebaseConfig); - var db = firebase.firestore(); - + $("#book-form").submit(function(e) { e.preventDefault(); }); $('#submit').click(function() { - add_this(); + add_this(); }); firebase.auth().onAuthStateChanged(user => { - if(!user) { + if (!user) { window.location = 'index.html'; - } + } }); - }); -function add_this() -{ +function add_this() { var BookCode = document.getElementById("book_code").value; var BookName = document.getElementById("book_name").value; var Author1 = document.getElementById("author1").value; @@ -38,21 +38,56 @@ function add_this() var Subject = document.getElementById("Subject").value; var tags = document.getElementById("tags").value; var db = firebase.firestore(); - - db.collection("books").doc(BookCode).set({ - bookcode: BookCode, - bookname : BookName, - author1: Author1, - author2: Author2, - subject : Subject, - tags : tags - }) - .then(function() { - console.log("Document successfully written!"); - window.alert("Successfully Book Added"); - window.location = 'admin_portal.html'; - }) - .catch(function(error) { - console.error("Error writing document: ", error); - }); + + if (!BookCode) { + alert("Book Code is required."); + return; + } + + // Prepare the data for update + var updateData = {}; + if (BookName) updateData.bookname = BookName; + if (Author1) updateData.author1 = Author1; + if (Author2) updateData.author2 = Author2; + if (Subject) updateData.subject = Subject; + if (tags) updateData.tags = tags; + + // Check if data exists in Firestore + db.collection("books").doc(BookCode).get() + .then((doc) => { + if (doc.exists) { + // If the document exists, update the fields + db.collection("books").doc(BookCode).update(updateData) + .then(() => { + alert("Book successfully updated!"); + console.log("Update successful!"); + }) + .catch((error) => { + alert("Error updating document: " + error); + console.error("Error updating document: ", error); + }); + } else { + // If the document does not exist, add the book + db.collection("books").doc(BookCode).set({ + bookcode: BookCode, + bookname: BookName || "", + author1: Author1 || "", + author2: Author2 || "", + subject: Subject || "", + tags: tags || "" + }) + .then(() => { + alert("Book successfully added!"); + console.log("Book added successfully!"); + }) + .catch((error) => { + alert("Error adding document: " + error); + console.error("Error adding document: ", error); + }); + } + }) + .catch((error) => { + alert("Error fetching document: " + error); + console.error("Error fetching document: ", error); + }); }