Continued work on getter/setters: post cached keys

This commit is contained in:
Marcin Kurczewski 2014-05-09 13:46:29 +02:00
parent a14afd8e27
commit 8ee80ea170
6 changed files with 34 additions and 17 deletions

View file

@ -9,6 +9,8 @@ class ScorePostJob extends AbstractPostJob
$score = intval($this->getArgument(self::SCORE)); $score = intval($this->getArgument(self::SCORE));
UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score); UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score);
return $post;
} }
public function requiresPrivilege() public function requiresPrivilege()

View file

@ -241,12 +241,12 @@ class PostController
//todo: //todo:
//move these to PostEntity when implementing ApiController //move these to PostEntity when implementing ApiController
$favorite = Auth::getCurrentUser()->hasFavorited($post); $isUserFavorite = Auth::getCurrentUser()->hasFavorited($post);
$score = Auth::getCurrentUser()->getScore($post); $userScore = Auth::getCurrentUser()->getScore($post);
$flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', [])); $flagged = in_array(TextHelper::reprPost($post), SessionHelper::get('flagged', []));
$context->favorite = $favorite; $context->isUserFavorite = $isUserFavorite;
$context->score = $score; $context->userScore = $userScore;
$context->flagged = $flagged; $context->flagged = $flagged;
$context->transport->post = $post; $context->transport->post = $post;
$context->transport->prevPostId = $prevPostId ? $prevPostId : null; $context->transport->prevPostId = $prevPostId ? $prevPostId : null;

View file

@ -18,9 +18,9 @@ class PostEntity extends AbstractEntity implements IValidatable
protected $uploaderId; protected $uploaderId;
protected $source; protected $source;
public $commentCount = 0; protected $commentCount = 0;
public $favCount = 0; protected $favCount = 0;
public $score = 0; protected $score = 0;
public function validate() public function validate()
{ {
@ -85,6 +85,21 @@ class PostEntity extends AbstractEntity implements IValidatable
return $favorites; return $favorites;
} }
public function getScore()
{
return $this->score;
}
public function getCommentCount()
{
return $this->commentCount;
}
public function getFavoriteCount()
{
return $this->favCount;
}
public function getRelations() public function getRelations()
{ {
if ($this->hasCache('relations')) if ($this->hasCache('relations'))

View file

@ -347,7 +347,7 @@ class UserEntity extends AbstractEntity implements IValidatable
$stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count'));
$stmt->setTable('favoritee'); $stmt->setTable('favoritee');
$stmt->setCriterion(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->getId()))); $stmt->setCriterion(new Sql\EqualsFunctor('user_id', new Sql\Binding($this->getId())));
return Database::fetchOne($stmt)['count']; return (int) Database::fetchOne($stmt)['count'];
} }
public function getCommentCount() public function getCommentCount()
@ -356,7 +356,7 @@ class UserEntity extends AbstractEntity implements IValidatable
$stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count'));
$stmt->setTable('comment'); $stmt->setTable('comment');
$stmt->setCriterion(new Sql\EqualsFunctor('commenter_id', new Sql\Binding($this->getId()))); $stmt->setCriterion(new Sql\EqualsFunctor('commenter_id', new Sql\Binding($this->getId())));
return Database::fetchOne($stmt)['count']; return (int) Database::fetchOne($stmt)['count'];
} }
public function getPostCount() public function getPostCount()
@ -365,6 +365,6 @@ class UserEntity extends AbstractEntity implements IValidatable
$stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count')); $stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count'));
$stmt->setTable('post'); $stmt->setTable('post');
$stmt->setCriterion(new Sql\EqualsFunctor('uploader_id', new Sql\Binding($this->getId()))); $stmt->setCriterion(new Sql\EqualsFunctor('uploader_id', new Sql\Binding($this->getId())));
return Database::fetchOne($stmt)['count']; return (int) Database::fetchOne($stmt)['count'];
} }
} }

View file

@ -54,9 +54,9 @@ if ($masstag)
<?php <?php
$x = $x =
[ [
'score' => $this->context->post->score, 'score' => $this->context->post->getScore(),
'comments' => $this->context->post->commentCount, 'comments' => $this->context->post->getCommentCount(),
'favs' => $this->context->post->favCount, 'favs' => $this->context->post->getFavoriteCount(),
]; ];
?> ?>

View file

@ -148,7 +148,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<div class="key-value score"> <div class="key-value score">
<span class="key">Score:</span> <span class="key">Score:</span>
<span class="value"> <span class="value">
<?= $this->context->transport->post->score ?> <?= $this->context->transport->post->getScore() ?>
<?php <?php
$scoreLink = function($score) $scoreLink = function($score)
@ -161,7 +161,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
<?php if (Access::check(new Privilege( <?php if (Access::check(new Privilege(
Privilege::ScorePost, Privilege::ScorePost,
Access::getIdentity($this->context->transport->post->getUploader())))): ?> Access::getIdentity($this->context->transport->post->getUploader())))): ?>
<?php if ($this->context->score === 1): ?> <?php if ($this->context->userScore === 1): ?>
<a class="simple-action selected" href="<?= $scoreLink(0) ?>"> <a class="simple-action selected" href="<?= $scoreLink(0) ?>">
<?php else: ?> <?php else: ?>
<a class="simple-action" href="<?= $scoreLink(1) ?>"> <a class="simple-action" href="<?= $scoreLink(1) ?>">
@ -171,7 +171,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
,&nbsp; ,&nbsp;
<?php if ($this->context->score === -1): ?> <?php if ($this->context->userScore === -1): ?>
<a class="simple-action selected" href="<?= $scoreLink(0) ?>"> <a class="simple-action selected" href="<?= $scoreLink(0) ?>">
<?php else: ?> <?php else: ?>
<a class="simple-action" href="<?= $scoreLink(-1) ?>"> <a class="simple-action" href="<?= $scoreLink(-1) ?>">
@ -207,7 +207,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
Privilege::FavoritePost, Privilege::FavoritePost,
Access::getIdentity($this->context->transport->post->getUploader())))): ?> Access::getIdentity($this->context->transport->post->getUploader())))): ?>
<div class="hl-option"> <div class="hl-option">
<?php if (!$this->context->favorite): ?> <?php if (!$this->context->isUserFavorite): ?>
<a class="add-fav icon simple-action" <a class="add-fav icon simple-action"
href="<?= \Chibi\Router::linkTo( href="<?= \Chibi\Router::linkTo(
['PostController', 'addFavoriteAction'], ['PostController', 'addFavoriteAction'],