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));
UserModel::updateUserScore(Auth::getCurrentUser(), $post, $score);
return $post;
}
public function requiresPrivilege()

View file

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

View file

@ -18,9 +18,9 @@ class PostEntity extends AbstractEntity implements IValidatable
protected $uploaderId;
protected $source;
public $commentCount = 0;
public $favCount = 0;
public $score = 0;
protected $commentCount = 0;
protected $favCount = 0;
protected $score = 0;
public function validate()
{
@ -85,6 +85,21 @@ class PostEntity extends AbstractEntity implements IValidatable
return $favorites;
}
public function getScore()
{
return $this->score;
}
public function getCommentCount()
{
return $this->commentCount;
}
public function getFavoriteCount()
{
return $this->favCount;
}
public function getRelations()
{
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->setTable('favoritee');
$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()
@ -356,7 +356,7 @@ class UserEntity extends AbstractEntity implements IValidatable
$stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count'));
$stmt->setTable('comment');
$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()
@ -365,6 +365,6 @@ class UserEntity extends AbstractEntity implements IValidatable
$stmt->setColumn(new Sql\AliasFunctor(new Sql\CountFunctor('1'), 'count'));
$stmt->setTable('post');
$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
$x =
[
'score' => $this->context->post->score,
'comments' => $this->context->post->commentCount,
'favs' => $this->context->post->favCount,
'score' => $this->context->post->getScore(),
'comments' => $this->context->post->getCommentCount(),
'favs' => $this->context->post->getFavoriteCount(),
];
?>

View file

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