From 2aefafa47347155fe077152e253c59e43076c743 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 23 Feb 2014 22:46:51 +0100 Subject: [PATCH] Favoriting a post automatically votes it up now It's still possible for user to withdraw his vote afterwards for whatever reason. --- src/Controllers/PostController.php | 1 + src/Upgrades/mysql/Upgrade11.sql | 19 +++++++++++++++++++ src/Upgrades/sqlite/Upgrade11.sql | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/Upgrades/mysql/Upgrade11.sql create mode 100644 src/Upgrades/sqlite/Upgrade11.sql diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index b5de07c9..9f505bbd 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -346,6 +346,7 @@ class PostController if (!$this->context->loggedIn) throw new SimpleException('Not logged in'); + UserModel::updateUserScore($this->context->user, $post, 1); UserModel::addToUserFavorites($this->context->user, $post); StatusHelper::success(); } diff --git a/src/Upgrades/mysql/Upgrade11.sql b/src/Upgrades/mysql/Upgrade11.sql new file mode 100644 index 00000000..9f233d82 --- /dev/null +++ b/src/Upgrades/mysql/Upgrade11.sql @@ -0,0 +1,19 @@ +INSERT + INTO post_score(user_id, post_id, score) + SELECT user_id, favoritee.post_id, 1 + FROM favoritee WHERE NOT EXISTS + ( + SELECT * + FROM post_score ps2 + WHERE favoritee.post_id = ps2.post_id + AND favoritee.user_id = ps2.user_id + ); + +UPDATE post_score + SET score = 1 + WHERE user_id IN + ( + SELECT user_id + FROM favoritee + WHERE favoritee.post_id = post_score.post_id + ); diff --git a/src/Upgrades/sqlite/Upgrade11.sql b/src/Upgrades/sqlite/Upgrade11.sql new file mode 100644 index 00000000..9f233d82 --- /dev/null +++ b/src/Upgrades/sqlite/Upgrade11.sql @@ -0,0 +1,19 @@ +INSERT + INTO post_score(user_id, post_id, score) + SELECT user_id, favoritee.post_id, 1 + FROM favoritee WHERE NOT EXISTS + ( + SELECT * + FROM post_score ps2 + WHERE favoritee.post_id = ps2.post_id + AND favoritee.user_id = ps2.user_id + ); + +UPDATE post_score + SET score = 1 + WHERE user_id IN + ( + SELECT user_id + FROM favoritee + WHERE favoritee.post_id = post_score.post_id + );