From 818a8a90548335297d4d26fbdf264aa49fc14feb Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 10 Nov 2014 15:54:08 +0100 Subject: [PATCH] Fixed comment updating after voting on it It reported bad score until user has refreshed the page. --- .../js/Presenters/PostCommentListPresenter.js | 4 +-- src/Controllers/ScoreController.php | 14 +++++--- .../ViewProxies/CommentViewProxy.php | 2 +- src/Controllers/ViewProxies/PostViewProxy.php | 2 +- src/Dao/ScoreDao.php | 36 +++++++++++++------ src/Services/FavoritesService.php | 2 +- src/Services/ScoreService.php | 23 ++++++++---- tests/Dao/ScoreDaoTest.php | 8 ++--- tests/Services/ScoreServiceTest.php | 10 +++--- 9 files changed, 65 insertions(+), 36 deletions(-) diff --git a/public_html/js/Presenters/PostCommentListPresenter.js b/public_html/js/Presenters/PostCommentListPresenter.js index c432f085..eaeb5da7 100644 --- a/public_html/js/Presenters/PostCommentListPresenter.js +++ b/public_html/js/Presenters/PostCommentListPresenter.js @@ -210,8 +210,8 @@ App.Presenters.PostCommentListPresenter = function( function score(comment, scoreValue) { promise.wait(api.post('/comments/' + comment.id + '/score', {score: scoreValue})) .then(function(response) { - comment.score = response.json.score; - comment.ownScore = parseInt(response.json.score); + comment.score = parseInt(response.json.score); + comment.ownScore = parseInt(response.json.ownScore); updateComment(comment); }).fail(showGenericError); } diff --git a/src/Controllers/ScoreController.php b/src/Controllers/ScoreController.php index 6173c496..c0efc8ae 100644 --- a/src/Controllers/ScoreController.php +++ b/src/Controllers/ScoreController.php @@ -71,15 +71,19 @@ final class ScoreController extends AbstractController $this->privilegeService->assertLoggedIn(); $score = intval($this->inputReader->score); $user = $this->authService->getLoggedInUser(); - $result = $this->scoreService->setScore($user, $entity, $score); - return ['score' => $result->getScore()]; + $result = $this->scoreService->setUserScore($user, $entity, $score); + return [ + 'score' => $this->scoreService->getScoreValue($entity), + 'ownScore' => $result->getScore(), + ]; } private function getScore(Entity $entity) { - $this->privilegeService->assertLoggedIn(); $user = $this->authService->getLoggedInUser(); - $result = $this->scoreService->getScore($user, $entity); - return ['score' => $result ? $result->getScore() : 0]; + return [ + 'score' => $this->scoreService->getScoreValue($entity), + 'ownScore' => $this->scoreService->getUserScoreValue($user, $entity), + ]; } } diff --git a/src/Controllers/ViewProxies/CommentViewProxy.php b/src/Controllers/ViewProxies/CommentViewProxy.php index a3e50e46..92259b82 100644 --- a/src/Controllers/ViewProxies/CommentViewProxy.php +++ b/src/Controllers/ViewProxies/CommentViewProxy.php @@ -35,7 +35,7 @@ class CommentViewProxy extends AbstractViewProxy $result->score = $comment->getScore(); if (!empty($config[self::FETCH_OWN_SCORE]) and $this->authService->isLoggedIn()) - $result->ownScore = $this->scoreService->getScoreValue($this->authService->getLoggedInUser(), $comment); + $result->ownScore = $this->scoreService->getUserScoreValue($this->authService->getLoggedInUser(), $comment); } return $result; } diff --git a/src/Controllers/ViewProxies/PostViewProxy.php b/src/Controllers/ViewProxies/PostViewProxy.php index 1bfc7dba..2896d3a7 100644 --- a/src/Controllers/ViewProxies/PostViewProxy.php +++ b/src/Controllers/ViewProxies/PostViewProxy.php @@ -108,7 +108,7 @@ class PostViewProxy extends AbstractViewProxy } if (!empty($config[self::FETCH_OWN_SCORE]) and $this->authService->isLoggedIn()) - $result->ownScore = $this->scoreService->getScoreValue($this->authService->getLoggedInUser(), $post); + $result->ownScore = $this->scoreService->getUserScoreValue($this->authService->getLoggedInUser(), $post); if (!empty($config[self::FETCH_FAVORITES])) $result->favorites = $this->userViewProxy->fromArray($this->favoritesService->getFavoriteUsers($post)); diff --git a/src/Dao/ScoreDao.php b/src/Dao/ScoreDao.php index d70883af..c4b89bb3 100644 --- a/src/Dao/ScoreDao.php +++ b/src/Dao/ScoreDao.php @@ -25,25 +25,27 @@ class ScoreDao extends AbstractDao implements ICrudDao $this->timeService = $timeService; } - public function getScore(User $user, Entity $entity) + public function getScoreValue(Entity $entity) { - $query = $this->pdo->from($this->tableName)->where('userId', $user->getId()); + $query = $this->getBaseQuery($entity); + $query->select(null); + $query->select('SUM(score) AS score'); + return iterator_to_array($query)[0]['score']; + } - if ($entity instanceof Post) - $query->where('postId', $entity->getId()); - elseif ($entity instanceof Comment) - $query->where('commentId', $entity->getId()); - else - throw new \InvalidArgumentException(); + public function getUserScore(User $user, Entity $entity) + { + $query = $this->getBaseQuery($entity); + $query->where('userId', $user->getId()); $arrayEntities = iterator_to_array($query); $entities = $this->arrayToEntities($arrayEntities); return array_shift($entities); } - public function setScore(User $user, Entity $entity, $scoreValue) + public function setUserScore(User $user, Entity $entity, $scoreValue) { - $score = $this->getScore($user, $entity); + $score = $this->getUserScore($user, $entity); if (!$score) { $score = new Score(); @@ -61,4 +63,18 @@ class ScoreDao extends AbstractDao implements ICrudDao $this->save($score); return $score; } + + private function getBaseQuery($entity) + { + $query = $this->pdo->from($this->tableName); + + if ($entity instanceof Post) + $query->where('postId', $entity->getId()); + elseif ($entity instanceof Comment) + $query->where('commentId', $entity->getId()); + else + throw new \InvalidArgumentException(); + + return $query; + } } diff --git a/src/Services/FavoritesService.php b/src/Services/FavoritesService.php index 437127d2..2270237b 100644 --- a/src/Services/FavoritesService.php +++ b/src/Services/FavoritesService.php @@ -49,7 +49,7 @@ class FavoritesService { $transactionFunc = function() use ($user, $entity) { - $this->scoreDao->setScore($user, $entity, 1); + $this->scoreDao->setUserScore($user, $entity, 1); return $this->favoritesDao->set($user, $entity); }; diff --git a/src/Services/ScoreService.php b/src/Services/ScoreService.php index f85d6156..8c095476 100644 --- a/src/Services/ScoreService.php +++ b/src/Services/ScoreService.php @@ -31,24 +31,33 @@ class ScoreService $this->timeService = $timeService; } - public function getScore(User $user, Entity $entity) + public function getScoreValue(Entity $entity) { - $transactionFunc = function() use ($user, $entity) + $transactionFunc = function() use ($entity) { - return $this->scoreDao->getScore($user, $entity); + return $this->scoreDao->getScoreValue($entity); }; return $this->transactionManager->rollback($transactionFunc); } - public function getScoreValue(User $user, Entity $entity) + public function getUserScore(User $user, Entity $entity) { - $score = $this->getScore($user, $entity); + $transactionFunc = function() use ($user, $entity) + { + return $this->scoreDao->getUserScore($user, $entity); + }; + return $this->transactionManager->rollback($transactionFunc); + } + + public function getUserScoreValue(User $user, Entity $entity) + { + $score = $this->getUserScore($user, $entity); if (!$score) return 0; return $score->getScore(); } - public function setScore(User $user, Entity $entity, $scoreValue) + public function setUserScore(User $user, Entity $entity, $scoreValue) { if ($scoreValue !== 1 and $scoreValue !== 0 and $scoreValue !== -1) throw new \DomainException('Bad score'); @@ -58,7 +67,7 @@ class ScoreService if (($scoreValue !== 1) and ($entity instanceof Post)) $this->favoritesDao->delete($user, $entity); - return $this->scoreDao->setScore($user, $entity, $scoreValue); + return $this->scoreDao->setUserScore($user, $entity, $scoreValue); }; return $this->transactionManager->commit($transactionFunc); } diff --git a/tests/Dao/ScoreDaoTest.php b/tests/Dao/ScoreDaoTest.php index 6c4c0958..829f06ae 100644 --- a/tests/Dao/ScoreDaoTest.php +++ b/tests/Dao/ScoreDaoTest.php @@ -71,10 +71,10 @@ final class ScoreDaoTest extends AbstractDatabaseTestCase $scoreDao->save($score2); $scoreDao->save($score3); - $this->assertEntitiesEqual($score1, $scoreDao->getScore($user1, $post1)); - $this->assertEntitiesEqual($score2, $scoreDao->getScore($user2, $post2)); - $this->assertEntitiesEqual($score3, $scoreDao->getScore($user1, $post2)); - $this->assertNull($scoreDao->getScore($user2, $post1)); + $this->assertEntitiesEqual($score1, $scoreDao->getUserScore($user1, $post1)); + $this->assertEntitiesEqual($score2, $scoreDao->getUserScore($user2, $post2)); + $this->assertEntitiesEqual($score3, $scoreDao->getUserScore($user1, $post2)); + $this->assertNull($scoreDao->getUserScore($user2, $post1)); } public function findByPost(Post $post) diff --git a/tests/Services/ScoreServiceTest.php b/tests/Services/ScoreServiceTest.php index 273f7079..e0403a00 100644 --- a/tests/Services/ScoreServiceTest.php +++ b/tests/Services/ScoreServiceTest.php @@ -36,10 +36,10 @@ final class ScoreServiceTest extends AbstractTestCase $score->setUserId($user->getId()); $score->setPostId($post->getId()); $score->setScore(1); - $this->scoreDaoMock->expects($this->once())->method('setScore')->with($user, $post)->willReturn(null); + $this->scoreDaoMock->expects($this->once())->method('setUserScore')->with($user, $post)->willReturn(null); $scoreService = $this->getScoreService(); - $scoreService->setScore($user, $post, 1); + $scoreService->setUserScore($user, $post, 1); } public function testSettingInvalid() @@ -48,7 +48,7 @@ final class ScoreServiceTest extends AbstractTestCase $post = new Post(2); $this->setExpectedException(\Exception::class); $scoreService = $this->getScoreService(); - $scoreService->setScore($user, $post, 2); + $scoreService->setUserScore($user, $post, 2); } public function testGetting() @@ -56,10 +56,10 @@ final class ScoreServiceTest extends AbstractTestCase $user = new User(); $post = new Post(); $score = new Score(3); - $this->scoreDaoMock->expects($this->once())->method('getScore')->with($user, $post)->willReturn($score); + $this->scoreDaoMock->expects($this->once())->method('getUserScore')->with($user, $post)->willReturn($score); $scoreService = $this->getScoreService(); - $retrievedScore = $scoreService->getScore($user, $post); + $retrievedScore = $scoreService->getUserScore($user, $post); $this->assertEquals($score, $retrievedScore); }