From 4896f52871e35e3bf46a7129ed92ad07eea0b739 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 18 Apr 2015 14:23:35 +0900 Subject: [PATCH] Adding more database constraints --- db/build_db.js | 2 +- db/hscd.sql | 6 ++++-- server.go | 23 +++++++++++++---------- static/scripts/profile.js | 7 ++++--- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/db/build_db.js b/db/build_db.js index 1c11451..e993f21 100755 --- a/db/build_db.js +++ b/db/build_db.js @@ -91,7 +91,7 @@ conn.query('CREATE TABLE history(date DATETIME NOT NULL, reviewId INT NOT NULL, // conn.query('DROP TABLE IF EXISTS historyGroups'); -conn.query('CREATE TABLE historyGroups(categoryId INT NOT NULL, categoryValue FLOAT NOT NULL, historyId INT NOT NULL, FOREIGN KEY(historyId) REFERENCES history(id))'); +conn.query('CREATE TABLE historyGroups(categoryId INT NOT NULL, categoryValue FLOAT NOT NULL, historyId INT NOT NULL, FOREIGN KEY(historyId) REFERENCES history(id), FOREIGN KEY(categoryId) REFERENCES categories(id))'); // diff --git a/db/hscd.sql b/db/hscd.sql index e7916e4..ff3c76f 100644 --- a/db/hscd.sql +++ b/db/hscd.sql @@ -77,7 +77,9 @@ CREATE TABLE `historyGroups` ( `categoryValue` float NOT NULL, `historyId` int(11) NOT NULL, KEY `historyId` (`historyId`), - CONSTRAINT `historyGroups_ibfk_1` FOREIGN KEY (`historyId`) REFERENCES `history` (`id`) + KEY `categoryId` (`categoryId`), + CONSTRAINT `historyGroups_ibfk_1` FOREIGN KEY (`historyId`) REFERENCES `history` (`id`), + CONSTRAINT `historyGroups_ibfk_2` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; @@ -133,4 +135,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2015-03-26 13:31:52 +-- Dump completed on 2015-04-18 13:51:01 diff --git a/server.go b/server.go index 1092176..3c5e715 100644 --- a/server.go +++ b/server.go @@ -185,17 +185,9 @@ func removeCategory(rw http.ResponseWriter, req *http.Request) { return } - result, err := db.Exec("DELETE FROM categories WHERE id = (?)", request.Id) - if err != nil { - log.Fatal(err) - } + _, err := db.Exec("DELETE FROM categories WHERE id = (?)", request.Id) - affectedRows, err := result.RowsAffected() - if err != nil { - log.Fatal(err) - } - - js, err := json.Marshal(jsonRemoveCategoryResponse{affectedRows > 0}) + js, err := json.Marshal(jsonRemoveCategoryResponse{err == nil}) if err != nil { log.Fatal(err) } @@ -236,6 +228,17 @@ func accessReview(rw http.ResponseWriter, req *http.Request) { } for id, value := range request.Profile { + catRow := db.QueryRow("SELECT EXISTS(SELECT NULL FROM categories WHERE id = ?)", id) + + var catExists int + if err := catRow.Scan(&catExists); err != nil { + log.Fatal(err) + } + + if catExists == 0 { + continue + } + if _, err := db.Exec("INSERT INTO historyGroups(categoryId, categoryValue, historyId) VALUES(?, ?, ?)", id, value, insertId); err != nil { log.Fatal(err) } diff --git a/static/scripts/profile.js b/static/scripts/profile.js index bcfd6ec..4db3452 100644 --- a/static/scripts/profile.js +++ b/static/scripts/profile.js @@ -57,6 +57,9 @@ $(this).remove(); }); } + else { + alert('Category could not be deleted because it is referenced by user access history.'); + } }, 'json'); } @@ -69,9 +72,7 @@ }); $('#categories button').unbind().click(function() { - if (confirm('Are you sure you want to delete this category?')) { - removeCategory(parseInt($(this).attr('data-categoryId'))); - } + removeCategory(parseInt($(this).attr('data-categoryId'))); }); }