Fixed comment updating after voting on it
It reported bad score until user has refreshed the page.
This commit is contained in:
parent
6f801f2628
commit
818a8a9054
9 changed files with 65 additions and 36 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue