Changed favorite and score DAO to be more flexible
This commit is contained in:
parent
adfc120642
commit
edb9055299
18 changed files with 386 additions and 300 deletions
|
@ -1,50 +1,60 @@
|
|||
<?php
|
||||
namespace Szurubooru\Controllers;
|
||||
|
||||
class PostScoreController extends AbstractController
|
||||
class ScoreController extends AbstractController
|
||||
{
|
||||
private $privilegeService;
|
||||
private $authService;
|
||||
private $postService;
|
||||
private $postScoreService;
|
||||
private $scoreService;
|
||||
private $inputReader;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Services\PrivilegeService $privilegeService,
|
||||
\Szurubooru\Services\AuthService $authService,
|
||||
\Szurubooru\Services\PostService $postService,
|
||||
\Szurubooru\Services\PostScoreService $postScoreService,
|
||||
\Szurubooru\Services\ScoreService $scoreService,
|
||||
\Szurubooru\Helpers\InputReader $inputReader)
|
||||
{
|
||||
$this->privilegeService = $privilegeService;
|
||||
$this->authService = $authService;
|
||||
$this->postService = $postService;
|
||||
$this->postScoreService = $postScoreService;
|
||||
$this->scoreService = $scoreService;
|
||||
$this->inputReader = $inputReader;
|
||||
}
|
||||
|
||||
public function registerRoutes(\Szurubooru\Router $router)
|
||||
{
|
||||
$router->get('/api/posts/:postNameOrId/score', [$this, 'getScore']);
|
||||
$router->post('/api/posts/:postNameOrId/score', [$this, 'setScore']);
|
||||
$router->get('/api/posts/:postNameOrId/score', [$this, 'getPostScore']);
|
||||
$router->post('/api/posts/:postNameOrId/score', [$this, 'setPostScore']);
|
||||
}
|
||||
|
||||
public function getScore($postNameOrId)
|
||||
public function getPostScore($postNameOrId)
|
||||
{
|
||||
$this->privilegeService->assertLoggedIn();
|
||||
$user = $this->authService->getLoggedInUser();
|
||||
$post = $this->postService->getByNameOrId($postNameOrId);
|
||||
$result = $this->postScoreService->getScore($user, $post);
|
||||
return ['score' => $result ? $result->getScore() : 0];
|
||||
return $this->getScore($post);
|
||||
}
|
||||
|
||||
public function setScore($postNameOrId)
|
||||
public function setPostScore($postNameOrId)
|
||||
{
|
||||
$post = $this->postService->getByNameOrId($postNameOrId);
|
||||
return $this->setScore($post);
|
||||
}
|
||||
|
||||
private function setScore(\Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$this->privilegeService->assertLoggedIn();
|
||||
$score = intval($this->inputReader->score);
|
||||
$user = $this->authService->getLoggedInUser();
|
||||
$post = $this->postService->getByNameOrId($postNameOrId);
|
||||
$result = $this->postScoreService->setScore($user, $post, $score);
|
||||
$result = $this->scoreService->setScore($user, $entity, $score);
|
||||
return ['score' => $result->getScore()];
|
||||
}
|
||||
|
||||
private function getScore(\Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$this->privilegeService->assertLoggedIn();
|
||||
$user = $this->authService->getLoggedInUser();
|
||||
$result = $this->scoreService->getScore($user, $entity);
|
||||
return ['score' => $result ? $result->getScore() : 0];
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@ class PostViewProxy extends AbstractViewProxy
|
|||
private $authService;
|
||||
private $historyService;
|
||||
private $favoritesService;
|
||||
private $postScoreService;
|
||||
private $scoreService;
|
||||
private $tagViewProxy;
|
||||
private $userViewProxy;
|
||||
private $snapshotViewProxy;
|
||||
|
@ -24,7 +24,7 @@ class PostViewProxy extends AbstractViewProxy
|
|||
\Szurubooru\Services\AuthService $authService,
|
||||
\Szurubooru\Services\HistoryService $historyService,
|
||||
\Szurubooru\Services\FavoritesService $favoritesService,
|
||||
\Szurubooru\Services\PostScoreService $postScoreService,
|
||||
\Szurubooru\Services\ScoreService $scoreService,
|
||||
TagViewProxy $tagViewProxy,
|
||||
UserViewProxy $userViewProxy,
|
||||
SnapshotViewProxy $snapshotViewProxy)
|
||||
|
@ -33,7 +33,7 @@ class PostViewProxy extends AbstractViewProxy
|
|||
$this->authService = $authService;
|
||||
$this->historyService = $historyService;
|
||||
$this->favoritesService = $favoritesService;
|
||||
$this->postScoreService = $postScoreService;
|
||||
$this->scoreService = $scoreService;
|
||||
$this->tagViewProxy = $tagViewProxy;
|
||||
$this->userViewProxy = $userViewProxy;
|
||||
$this->snapshotViewProxy = $snapshotViewProxy;
|
||||
|
@ -83,7 +83,7 @@ class PostViewProxy extends AbstractViewProxy
|
|||
}
|
||||
|
||||
if (!empty($config[self::FETCH_OWN_SCORE]) and $this->authService->isLoggedIn())
|
||||
$result->ownScore = $this->postScoreService->getScoreValue($this->authService->getLoggedInUser(), $post);
|
||||
$result->ownScore = $this->scoreService->getScoreValue($this->authService->getLoggedInUser(), $post);
|
||||
|
||||
if (!empty($config[self::FETCH_FAVORITES]))
|
||||
$result->favorites = $this->userViewProxy->fromArray($this->favoritesService->getFavoriteUsers($post));
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
namespace Szurubooru\Dao\EntityConverters;
|
||||
|
||||
class PostScoreEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||
class ScoreEntityConverter extends AbstractEntityConverter implements IEntityConverter
|
||||
{
|
||||
public function toArray(\Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ class PostScoreEntityConverter extends AbstractEntityConverter implements IEntit
|
|||
|
||||
public function toBasicEntity(array $array)
|
||||
{
|
||||
$entity = new \Szurubooru\Entities\PostScore($array['id']);
|
||||
$entity = new \Szurubooru\Entities\Score($array['id']);
|
||||
$entity->setUserId($array['userId']);
|
||||
$entity->setPostId($array['postId']);
|
||||
$entity->setTime($this->dbTimeToEntityTime($array['time']));
|
|
@ -23,28 +23,36 @@ class FavoritesDao extends AbstractDao implements ICrudDao
|
|||
$this->timeService = $timeService;
|
||||
}
|
||||
|
||||
public function findByPost(\Szurubooru\Entities\Post $post)
|
||||
public function findByEntity(\Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
return $this->findBy('postId', $post->getId());
|
||||
if ($entity instanceof \Szurubooru\Entities\Post)
|
||||
return $this->findBy('postId', $entity->getId());
|
||||
else
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
|
||||
public function set(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
public function set(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$favorite = $this->get($user, $post);
|
||||
$favorite = $this->get($user, $entity);
|
||||
if (!$favorite)
|
||||
{
|
||||
$favorite = new \Szurubooru\Entities\Favorite();
|
||||
$favorite->setUser($user);
|
||||
$favorite->setPost($post);
|
||||
$favorite->setTime($this->timeService->getCurrentTime());
|
||||
$favorite->setUser($user);
|
||||
|
||||
if ($entity instanceof \Szurubooru\Entities\Post)
|
||||
$favorite->setPost($entity);
|
||||
else
|
||||
throw new \InvalidArgumentException();
|
||||
|
||||
$this->save($favorite);
|
||||
}
|
||||
return $favorite;
|
||||
}
|
||||
|
||||
public function delete(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
public function delete(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$favorite = $this->get($user, $post);
|
||||
$favorite = $this->get($user, $entity);
|
||||
if ($favorite)
|
||||
$this->deleteById($favorite->getId());
|
||||
}
|
||||
|
@ -66,11 +74,15 @@ class FavoritesDao extends AbstractDao implements ICrudDao
|
|||
});
|
||||
}
|
||||
|
||||
private function get(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
private function get(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$query = $this->fpdo->from($this->tableName)
|
||||
->where('userId', $user->getId())
|
||||
->where('postId', $post->getId());
|
||||
$query = $this->fpdo->from($this->tableName)->where('userId', $user->getId());
|
||||
|
||||
if ($entity instanceof \Szurubooru\Entities\Post)
|
||||
$query->where('postId', $entity->getId());
|
||||
else
|
||||
throw new \InvalidArgumentException();
|
||||
|
||||
$arrayEntities = iterator_to_array($query);
|
||||
$entities = $this->arrayToEntities($arrayEntities);
|
||||
return array_shift($entities);
|
||||
|
|
|
@ -133,11 +133,11 @@ class PostDao extends AbstractDao implements ICrudDao
|
|||
$userName = $values[0];
|
||||
$score = $values[1];
|
||||
$sql = 'EXISTS (
|
||||
SELECT 1 FROM postScores
|
||||
INNER JOIN users ON postScores.userId = users.id
|
||||
WHERE postScores.postId = posts.id
|
||||
SELECT 1 FROM scores
|
||||
INNER JOIN users ON scores.userId = users.id
|
||||
WHERE scores.postId = posts.id
|
||||
AND LOWER(users.name) = LOWER(?)
|
||||
AND postScores.score = ?)';
|
||||
AND scores.score = ?)';
|
||||
if ($requirement->isnegated())
|
||||
$sql = 'NOT ' . $sql;
|
||||
$query->where($sql, $userName, $score);
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
<?php
|
||||
namespace Szurubooru\Dao;
|
||||
|
||||
class PostScoreDao extends AbstractDao implements ICrudDao
|
||||
{
|
||||
private $userDao;
|
||||
private $postDao;
|
||||
private $timeService;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\DatabaseConnection $databaseConnection,
|
||||
\Szurubooru\Dao\UserDao $userDao,
|
||||
\Szurubooru\Dao\PostDao $postDao,
|
||||
\Szurubooru\Services\TimeService $timeService)
|
||||
{
|
||||
parent::__construct(
|
||||
$databaseConnection,
|
||||
'postScores',
|
||||
new \Szurubooru\Dao\EntityConverters\PostScoreEntityConverter());
|
||||
|
||||
$this->userDao = $userDao;
|
||||
$this->postDao = $postDao;
|
||||
$this->timeService = $timeService;
|
||||
}
|
||||
|
||||
public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
{
|
||||
$query = $this->fpdo->from($this->tableName)
|
||||
->where('userId', $user->getId())
|
||||
->where('postId', $post->getId());
|
||||
$arrayEntities = iterator_to_array($query);
|
||||
$entities = $this->arrayToEntities($arrayEntities);
|
||||
return array_shift($entities);
|
||||
}
|
||||
|
||||
public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post, $scoreValue)
|
||||
{
|
||||
$postScore = $this->getScore($user, $post);
|
||||
if (!$postScore)
|
||||
{
|
||||
$postScore = new \Szurubooru\Entities\PostScore();
|
||||
$postScore->setUser($user);
|
||||
$postScore->setPost($post);
|
||||
$postScore->setTime($this->timeService->getCurrentTime());
|
||||
}
|
||||
$postScore->setScore($scoreValue);
|
||||
$this->save($postScore);
|
||||
return $postScore;
|
||||
}
|
||||
|
||||
protected function afterLoad(\Szurubooru\Entities\Entity $postScore)
|
||||
{
|
||||
$postScore->setLazyLoader(
|
||||
\Szurubooru\Entities\PostScore::LAZY_LOADER_USER,
|
||||
function (\Szurubooru\Entities\PostScore $postScore)
|
||||
{
|
||||
return $this->userDao->findById($postScore->getUserId());
|
||||
});
|
||||
|
||||
$postScore->setLazyLoader(
|
||||
\Szurubooru\Entities\PostScore::LAZY_LOADER_POST,
|
||||
function (\Szurubooru\Entities\PostScore $postScore)
|
||||
{
|
||||
return $this->postDao->findById($postScore->getPostId());
|
||||
});
|
||||
}
|
||||
}
|
75
src/Dao/ScoreDao.php
Normal file
75
src/Dao/ScoreDao.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
namespace Szurubooru\Dao;
|
||||
|
||||
class ScoreDao extends AbstractDao implements ICrudDao
|
||||
{
|
||||
private $userDao;
|
||||
private $postDao;
|
||||
private $timeService;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\DatabaseConnection $databaseConnection,
|
||||
\Szurubooru\Dao\UserDao $userDao,
|
||||
\Szurubooru\Dao\PostDao $postDao,
|
||||
\Szurubooru\Services\TimeService $timeService)
|
||||
{
|
||||
parent::__construct(
|
||||
$databaseConnection,
|
||||
'scores',
|
||||
new \Szurubooru\Dao\EntityConverters\ScoreEntityConverter());
|
||||
|
||||
$this->userDao = $userDao;
|
||||
$this->postDao = $postDao;
|
||||
$this->timeService = $timeService;
|
||||
}
|
||||
|
||||
public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$query = $this->fpdo->from($this->tableName)->where('userId', $user->getId());
|
||||
|
||||
if ($entity instanceof \Szurubooru\Entities\Post)
|
||||
$query->where('postId', $entity->getId());
|
||||
else
|
||||
throw new \InvalidArgumentException();
|
||||
|
||||
$arrayEntities = iterator_to_array($query);
|
||||
$entities = $this->arrayToEntities($arrayEntities);
|
||||
return array_shift($entities);
|
||||
}
|
||||
|
||||
public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity, $scoreValue)
|
||||
{
|
||||
$score = $this->getScore($user, $entity);
|
||||
if (!$score)
|
||||
{
|
||||
$score = new \Szurubooru\Entities\Score();
|
||||
$score->setTime($this->timeService->getCurrentTime());
|
||||
$score->setUser($user);
|
||||
|
||||
if ($entity instanceof \Szurubooru\Entities\Post)
|
||||
$score->setPost($entity);
|
||||
else
|
||||
throw new \InvalidArgumentException();
|
||||
}
|
||||
$score->setScore($scoreValue);
|
||||
$this->save($score);
|
||||
return $score;
|
||||
}
|
||||
|
||||
protected function afterLoad(\Szurubooru\Entities\Entity $score)
|
||||
{
|
||||
$score->setLazyLoader(
|
||||
\Szurubooru\Entities\Score::LAZY_LOADER_USER,
|
||||
function (\Szurubooru\Entities\Score $score)
|
||||
{
|
||||
return $this->userDao->findById($score->getUserId());
|
||||
});
|
||||
|
||||
$score->setLazyLoader(
|
||||
\Szurubooru\Entities\Score::LAZY_LOADER_POST,
|
||||
function (\Szurubooru\Entities\Score $score)
|
||||
{
|
||||
return $this->postDao->findById($score->getPostId());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
namespace Szurubooru\Entities;
|
||||
|
||||
class PostScore extends Entity
|
||||
class Score extends Entity
|
||||
{
|
||||
private $postId;
|
||||
private $userId;
|
|
@ -4,30 +4,30 @@ namespace Szurubooru\Services;
|
|||
class FavoritesService
|
||||
{
|
||||
private $favoritesDao;
|
||||
private $postScoreDao;
|
||||
private $scoreDao;
|
||||
private $userDao;
|
||||
private $transactionManager;
|
||||
private $timeService;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Dao\FavoritesDao $favoritesDao,
|
||||
\Szurubooru\Dao\PostScoreDao $postScoreDao,
|
||||
\Szurubooru\Dao\ScoreDao $scoreDao,
|
||||
\Szurubooru\Dao\UserDao $userDao,
|
||||
\Szurubooru\Dao\TransactionManager $transactionManager,
|
||||
\Szurubooru\Services\TimeService $timeService)
|
||||
{
|
||||
$this->favoritesDao = $favoritesDao;
|
||||
$this->postScoreDao = $postScoreDao;
|
||||
$this->scoreDao = $scoreDao;
|
||||
$this->userDao = $userDao;
|
||||
$this->transactionManager = $transactionManager;
|
||||
$this->timeService = $timeService;
|
||||
}
|
||||
|
||||
public function getFavoriteUsers(\Szurubooru\Entities\Post $post)
|
||||
public function getFavoriteUsers(\Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$transactionFunc = function() use ($post)
|
||||
$transactionFunc = function() use ($entity)
|
||||
{
|
||||
$favorites = $this->favoritesDao->findByPost($post);
|
||||
$favorites = $this->favoritesDao->findByEntity($entity);
|
||||
$userIds = [];
|
||||
foreach ($favorites as $favorite)
|
||||
{
|
||||
|
@ -38,22 +38,22 @@ class FavoritesService
|
|||
return $this->transactionManager->rollback($transactionFunc);
|
||||
}
|
||||
|
||||
public function addFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
public function addFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$transactionFunc = function() use ($user, $post)
|
||||
$transactionFunc = function() use ($user, $entity)
|
||||
{
|
||||
$this->postScoreDao->setScore($user, $post, 1);
|
||||
$this->scoreDao->setScore($user, $entity, 1);
|
||||
|
||||
return $this->favoritesDao->set($user, $post);
|
||||
return $this->favoritesDao->set($user, $entity);
|
||||
};
|
||||
return $this->transactionManager->commit($transactionFunc);
|
||||
}
|
||||
|
||||
public function deleteFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
public function deleteFavorite(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$transactionFunc = function() use ($user, $post)
|
||||
$transactionFunc = function() use ($user, $entity)
|
||||
{
|
||||
$this->favoritesDao->delete($user, $post);
|
||||
$this->favoritesDao->delete($user, $entity);
|
||||
};
|
||||
$this->transactionManager->commit($transactionFunc);
|
||||
}
|
||||
|
|
|
@ -1,56 +1,56 @@
|
|||
<?php
|
||||
namespace Szurubooru\Services;
|
||||
|
||||
class PostScoreService
|
||||
class ScoreService
|
||||
{
|
||||
private $postScoreDao;
|
||||
private $scoreDao;
|
||||
private $favoritesDao;
|
||||
private $userDao;
|
||||
private $transactionManager;
|
||||
private $timeService;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Dao\PostScoreDao $postScoreDao,
|
||||
\Szurubooru\Dao\ScoreDao $scoreDao,
|
||||
\Szurubooru\Dao\FavoritesDao $favoritesDao,
|
||||
\Szurubooru\Dao\UserDao $userDao,
|
||||
\Szurubooru\Dao\TransactionManager $transactionManager,
|
||||
\Szurubooru\Services\TimeService $timeService)
|
||||
{
|
||||
$this->postScoreDao = $postScoreDao;
|
||||
$this->scoreDao = $scoreDao;
|
||||
$this->favoritesDao = $favoritesDao;
|
||||
$this->userDao = $userDao;
|
||||
$this->transactionManager = $transactionManager;
|
||||
$this->timeService = $timeService;
|
||||
}
|
||||
|
||||
public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
public function getScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$transactionFunc = function() use ($user, $post)
|
||||
$transactionFunc = function() use ($user, $entity)
|
||||
{
|
||||
return $this->postScoreDao->getScore($user, $post);
|
||||
return $this->scoreDao->getScore($user, $entity);
|
||||
};
|
||||
return $this->transactionManager->rollback($transactionFunc);
|
||||
}
|
||||
|
||||
public function getScoreValue(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post)
|
||||
public function getScoreValue(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity)
|
||||
{
|
||||
$score = $this->getScore($user, $post);
|
||||
$score = $this->getScore($user, $entity);
|
||||
if (!$score)
|
||||
return 0;
|
||||
return $score->getScore();
|
||||
}
|
||||
|
||||
public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post, $scoreValue)
|
||||
public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Entity $entity, $scoreValue)
|
||||
{
|
||||
if ($scoreValue !== 1 and $scoreValue !== 0 and $scoreValue !== -1)
|
||||
throw new \DomainException('Bad score');
|
||||
|
||||
$transactionFunc = function() use ($user, $post, $scoreValue)
|
||||
$transactionFunc = function() use ($user, $entity, $scoreValue)
|
||||
{
|
||||
if ($scoreValue !== 1)
|
||||
$this->favoritesDao->delete($user, $post);
|
||||
$this->favoritesDao->delete($user, $entity);
|
||||
|
||||
return $this->postScoreDao->setScore($user, $post, $scoreValue);
|
||||
return $this->scoreDao->setScore($user, $entity, $scoreValue);
|
||||
};
|
||||
return $this->transactionManager->commit($transactionFunc);
|
||||
}
|
60
src/Upgrades/Upgrade15.php
Normal file
60
src/Upgrades/Upgrade15.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
namespace Szurubooru\Upgrades;
|
||||
|
||||
class Upgrade15 implements IUpgrade
|
||||
{
|
||||
public function run(\Szurubooru\DatabaseConnection $databaseConnection)
|
||||
{
|
||||
$pdo = $databaseConnection->getPDO();
|
||||
$driver = $databaseConnection->getDriver();
|
||||
|
||||
$pdo->exec('DROP TABLE IF EXISTS scores');
|
||||
|
||||
$pdo->exec('CREATE TABLE scores
|
||||
(
|
||||
id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ',
|
||||
userId INTEGER NOT NULL,
|
||||
postId INTEGER,
|
||||
time DATETIME NOT NULL,
|
||||
score INTEGER NOT NULL
|
||||
)');
|
||||
|
||||
$pdo->exec('INSERT INTO scores (userId, postId, time, score) SELECT userId, postId, time, score FROM postScores');
|
||||
|
||||
$pdo->exec('DROP TABLE IF EXISTS postScores');
|
||||
$pdo->exec('DROP TRIGGER IF EXISTS postScoresDelete');
|
||||
$pdo->exec('DROP TRIGGER IF EXISTS postScoresInsert');
|
||||
$pdo->exec('DROP TRIGGER IF EXISTS postScoresUpdate');
|
||||
|
||||
$pdo->exec('DROP TRIGGER IF EXISTS scoresDelete');
|
||||
$pdo->exec('DROP TRIGGER IF EXISTS scoresInsert');
|
||||
$pdo->exec('DROP TRIGGER IF EXISTS scoresUpdate');
|
||||
|
||||
$pdo->exec('
|
||||
CREATE TRIGGER scoresDelete AFTER DELETE ON scores
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE posts SET
|
||||
score = (SELECT SUM(score) FROM scores WHERE scores.postId = posts.id)
|
||||
WHERE posts.id = OLD.postId;
|
||||
END');
|
||||
|
||||
$pdo->exec('
|
||||
CREATE TRIGGER scoresInsert AFTER INSERT ON scores
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE posts SET
|
||||
score = (SELECT SUM(score) FROM scores WHERE scores.postId = posts.id)
|
||||
WHERE posts.id = NEW.postId;
|
||||
END');
|
||||
|
||||
$pdo->exec('
|
||||
CREATE TRIGGER scoresUpdate AFTER UPDATE ON scores
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE posts SET
|
||||
score = (SELECT SUM(score) FROM scores WHERE scores.postId = posts.id)
|
||||
WHERE posts.id IN (OLD.postId, NEW.postId);
|
||||
END');
|
||||
}
|
||||
}
|
|
@ -30,6 +30,7 @@ return [
|
|||
$container->get(\Szurubooru\Upgrades\Upgrade12::class),
|
||||
$container->get(\Szurubooru\Upgrades\Upgrade13::class),
|
||||
$container->get(\Szurubooru\Upgrades\Upgrade14::class),
|
||||
$container->get(\Szurubooru\Upgrades\Upgrade15::class),
|
||||
];
|
||||
}),
|
||||
|
||||
|
@ -43,7 +44,7 @@ return [
|
|||
$container->get(\Szurubooru\Controllers\GlobalParamController::class),
|
||||
$container->get(\Szurubooru\Controllers\HistoryController::class),
|
||||
$container->get(\Szurubooru\Controllers\FavoritesController::class),
|
||||
$container->get(\Szurubooru\Controllers\PostScoreController::class),
|
||||
$container->get(\Szurubooru\Controllers\ScoreController::class),
|
||||
$container->get(\Szurubooru\Controllers\CommentController::class),
|
||||
];
|
||||
}),
|
||||
|
|
|
@ -41,11 +41,6 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
$this->assertEntitiesEqual($post, $savedFavorite->getPost());
|
||||
}
|
||||
|
||||
public function findByPost(\Szurubooru\Entities\Post $post)
|
||||
{
|
||||
return $this->findOneBy('postId', $post->getId());
|
||||
}
|
||||
|
||||
private function getFavoritesDao()
|
||||
{
|
||||
return new \Szurubooru\Dao\FavoritesDao(
|
||||
|
|
|
@ -1,94 +0,0 @@
|
|||
<?php
|
||||
namespace Szurubooru\Tests\Dao;
|
||||
|
||||
class PostScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
||||
{
|
||||
private $userDaoMock;
|
||||
private $postDaoMock;
|
||||
private $timeServiceMock;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
|
||||
$this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class);
|
||||
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
||||
}
|
||||
|
||||
public function testSaving()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User(1);
|
||||
$user->setName('olivia');
|
||||
|
||||
$post = new \Szurubooru\Entities\Post(2);
|
||||
$post->setName('sword');
|
||||
|
||||
$postScore = new \Szurubooru\Entities\PostScore();
|
||||
$postScore->setUser($user);
|
||||
$postScore->setPost($post);
|
||||
$postScore->setTime(date('c'));
|
||||
$postScore->setScore(1);
|
||||
$postScoreDao = $this->getPostScoreDao();
|
||||
$postScoreDao->save($postScore);
|
||||
|
||||
$this->userDaoMock->expects($this->once())->method('findById')->with(1)->willReturn($user);
|
||||
$this->postDaoMock->expects($this->once())->method('findById')->with(2)->willReturn($post);
|
||||
|
||||
$savedPostScore = $postScoreDao->findById($postScore->getId());
|
||||
$this->assertEquals(1, $savedPostScore->getUserId());
|
||||
$this->assertEquals(2, $savedPostScore->getPostId());
|
||||
$this->assertEquals($postScore->getTime(), $savedPostScore->getTime());
|
||||
$this->assertEntitiesEqual($user, $savedPostScore->getUser());
|
||||
$this->assertEntitiesEqual($post, $savedPostScore->getPost());
|
||||
}
|
||||
|
||||
public function testFindingByUserAndPost()
|
||||
{
|
||||
$post1 = new \Szurubooru\Entities\Post(1);
|
||||
$post2 = new \Szurubooru\Entities\Post(2);
|
||||
$user1 = new \Szurubooru\Entities\User(3);
|
||||
$user2 = new \Szurubooru\Entities\User(4);
|
||||
|
||||
$postScore1 = new \Szurubooru\Entities\PostScore();
|
||||
$postScore1->setUser($user1);
|
||||
$postScore1->setPost($post1);
|
||||
$postScore1->setTime(date('c', mktime(1)));
|
||||
$postScore1->setScore(1);
|
||||
|
||||
$postScore2 = new \Szurubooru\Entities\PostScore();
|
||||
$postScore2->setUser($user2);
|
||||
$postScore2->setPost($post2);
|
||||
$postScore2->setTime(date('c', mktime(2)));
|
||||
$postScore2->setScore(0);
|
||||
|
||||
$postScore3 = new \Szurubooru\Entities\PostScore();
|
||||
$postScore3->setUser($user1);
|
||||
$postScore3->setPost($post2);
|
||||
$postScore3->setTime(date('c', mktime(3)));
|
||||
$postScore3->setScore(-1);
|
||||
|
||||
$postScoreDao = $this->getPostScoreDao();
|
||||
$postScoreDao->save($postScore1);
|
||||
$postScoreDao->save($postScore2);
|
||||
$postScoreDao->save($postScore3);
|
||||
|
||||
$this->assertEntitiesEqual($postScore1, $postScoreDao->getScore($user1, $post1));
|
||||
$this->assertEntitiesEqual($postScore2, $postScoreDao->getScore($user2, $post2));
|
||||
$this->assertEntitiesEqual($postScore3, $postScoreDao->getScore($user1, $post2));
|
||||
$this->assertNull($postScoreDao->getScore($user2, $post1));
|
||||
}
|
||||
|
||||
public function findByPost(\Szurubooru\Entities\Post $post)
|
||||
{
|
||||
return $this->findOneBy('postId', $post->getId());
|
||||
}
|
||||
|
||||
private function getPostScoreDao()
|
||||
{
|
||||
return new \Szurubooru\Dao\PostScoreDao(
|
||||
$this->databaseConnection,
|
||||
$this->userDaoMock,
|
||||
$this->postDaoMock,
|
||||
$this->timeServiceMock);
|
||||
}
|
||||
}
|
94
tests/Dao/ScoreDaoTest.php
Normal file
94
tests/Dao/ScoreDaoTest.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
namespace Szurubooru\Tests\Dao;
|
||||
|
||||
class ScoreDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
||||
{
|
||||
private $userDaoMock;
|
||||
private $postDaoMock;
|
||||
private $timeServiceMock;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
|
||||
$this->postDaoMock = $this->mock(\Szurubooru\Dao\PostDao::class);
|
||||
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
||||
}
|
||||
|
||||
public function testSaving()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User(1);
|
||||
$user->setName('olivia');
|
||||
|
||||
$post = new \Szurubooru\Entities\Post(2);
|
||||
$post->setName('sword');
|
||||
|
||||
$score = new \Szurubooru\Entities\Score();
|
||||
$score->setUser($user);
|
||||
$score->setPost($post);
|
||||
$score->setTime(date('c'));
|
||||
$score->setScore(1);
|
||||
$scoreDao = $this->getScoreDao();
|
||||
$scoreDao->save($score);
|
||||
|
||||
$this->userDaoMock->expects($this->once())->method('findById')->with(1)->willReturn($user);
|
||||
$this->postDaoMock->expects($this->once())->method('findById')->with(2)->willReturn($post);
|
||||
|
||||
$savedScore = $scoreDao->findById($score->getId());
|
||||
$this->assertEquals(1, $savedScore->getUserId());
|
||||
$this->assertEquals(2, $savedScore->getPostId());
|
||||
$this->assertEquals($score->getTime(), $savedScore->getTime());
|
||||
$this->assertEntitiesEqual($user, $savedScore->getUser());
|
||||
$this->assertEntitiesEqual($post, $savedScore->getPost());
|
||||
}
|
||||
|
||||
public function testFindingByUserAndPost()
|
||||
{
|
||||
$post1 = new \Szurubooru\Entities\Post(1);
|
||||
$post2 = new \Szurubooru\Entities\Post(2);
|
||||
$user1 = new \Szurubooru\Entities\User(3);
|
||||
$user2 = new \Szurubooru\Entities\User(4);
|
||||
|
||||
$score1 = new \Szurubooru\Entities\Score();
|
||||
$score1->setUser($user1);
|
||||
$score1->setPost($post1);
|
||||
$score1->setTime(date('c', mktime(1)));
|
||||
$score1->setScore(1);
|
||||
|
||||
$score2 = new \Szurubooru\Entities\Score();
|
||||
$score2->setUser($user2);
|
||||
$score2->setPost($post2);
|
||||
$score2->setTime(date('c', mktime(2)));
|
||||
$score2->setScore(0);
|
||||
|
||||
$score3 = new \Szurubooru\Entities\Score();
|
||||
$score3->setUser($user1);
|
||||
$score3->setPost($post2);
|
||||
$score3->setTime(date('c', mktime(3)));
|
||||
$score3->setScore(-1);
|
||||
|
||||
$scoreDao = $this->getScoreDao();
|
||||
$scoreDao->save($score1);
|
||||
$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));
|
||||
}
|
||||
|
||||
public function findByPost(\Szurubooru\Entities\Post $post)
|
||||
{
|
||||
return $this->findOneBy('postId', $post->getId());
|
||||
}
|
||||
|
||||
private function getScoreDao()
|
||||
{
|
||||
return new \Szurubooru\Dao\ScoreDao(
|
||||
$this->databaseConnection,
|
||||
$this->userDaoMock,
|
||||
$this->postDaoMock,
|
||||
$this->timeServiceMock);
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ namespace Szurubooru\Tests\Services;
|
|||
final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
||||
{
|
||||
private $favoritesDaoMock;
|
||||
private $postScoreDaoMock;
|
||||
private $scoreDaoMock;
|
||||
private $userDaoMock;
|
||||
private $transactionManagerMock;
|
||||
private $timeServiceMock;
|
||||
|
@ -13,7 +13,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
{
|
||||
parent::setUp();
|
||||
$this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class);
|
||||
$this->postScoreDaoMock = $this->mock(\Szurubooru\Dao\PostScoreDao::class);
|
||||
$this->scoreDaoMock = $this->mock(\Szurubooru\Dao\ScoreDao::class);
|
||||
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
|
||||
$this->transactionManagerMock = $this->mockTransactionManager();
|
||||
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
||||
|
@ -51,7 +51,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
$fav1->setUser(new \Szurubooru\Entities\User(1));
|
||||
$fav2->setUser(new \Szurubooru\Entities\User(2));
|
||||
|
||||
$this->favoritesDaoMock->expects($this->once())->method('findByPost')->with($post)->willReturn([$fav1, $fav2]);
|
||||
$this->favoritesDaoMock->expects($this->once())->method('findByEntity')->with($post)->willReturn([$fav1, $fav2]);
|
||||
$this->userDaoMock->expects($this->once())->method('findByIds')->with([1, 2]);
|
||||
|
||||
$favoritesService = $this->getFavoritesService();
|
||||
|
@ -62,7 +62,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
|||
{
|
||||
return new \Szurubooru\Services\FavoritesService(
|
||||
$this->favoritesDaoMock,
|
||||
$this->postScoreDaoMock,
|
||||
$this->scoreDaoMock,
|
||||
$this->userDaoMock,
|
||||
$this->transactionManagerMock,
|
||||
$this->timeServiceMock);
|
||||
|
|
|
@ -1,66 +0,0 @@
|
|||
<?php
|
||||
namespace Szurubooru\Tests\Services;
|
||||
|
||||
final class PostScoreServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
||||
{
|
||||
private $postScoreDaoMock;
|
||||
private $favoritesDaoMock;
|
||||
private $userDaoMock;
|
||||
private $transactionManagerMock;
|
||||
private $timeServiceMock;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->postScoreDaoMock = $this->mock(\Szurubooru\Dao\PostScoreDao::class);
|
||||
$this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class);
|
||||
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
|
||||
$this->transactionManagerMock = $this->mockTransactionManager();
|
||||
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
||||
}
|
||||
|
||||
public function testSetting()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User(1);
|
||||
$post = new \Szurubooru\Entities\Post(2);
|
||||
$postScore = new \Szurubooru\Entities\PostScore();
|
||||
$postScore->setUserId($user->getId());
|
||||
$postScore->setPostId($post->getId());
|
||||
$postScore->setScore(1);
|
||||
$this->postScoreDaoMock->expects($this->once())->method('setScore')->with($user, $post)->willReturn(null);
|
||||
|
||||
$postScoreService = $this->getPostScoreService();
|
||||
$postScoreService->setScore($user, $post, 1);
|
||||
}
|
||||
|
||||
public function testSettingInvalid()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User(1);
|
||||
$post = new \Szurubooru\Entities\Post(2);
|
||||
$this->setExpectedException(\Exception::class);
|
||||
$postScoreService = $this->getPostScoreService();
|
||||
$postScoreService->setScore($user, $post, 2);
|
||||
}
|
||||
|
||||
public function testGetting()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User();
|
||||
$post = new \Szurubooru\Entities\Post();
|
||||
$postScore = new \Szurubooru\Entities\PostScore(3);
|
||||
$this->postScoreDaoMock->expects($this->once())->method('getScore')->with($user, $post)->willReturn($postScore);
|
||||
|
||||
$postScoreService = $this->getPostScoreService();
|
||||
$retrievedScore = $postScoreService->getScore($user, $post);
|
||||
$this->assertEquals($postScore, $retrievedScore);
|
||||
}
|
||||
|
||||
private function getPostScoreService()
|
||||
{
|
||||
return new \Szurubooru\Services\PostScoreService(
|
||||
$this->postScoreDaoMock,
|
||||
$this->favoritesDaoMock,
|
||||
$this->userDaoMock,
|
||||
$this->transactionManagerMock,
|
||||
$this->timeServiceMock);
|
||||
}
|
||||
}
|
66
tests/Services/ScoreServiceTest.php
Normal file
66
tests/Services/ScoreServiceTest.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
namespace Szurubooru\Tests\Services;
|
||||
|
||||
final class ScoreServiceTest extends \Szurubooru\Tests\AbstractTestCase
|
||||
{
|
||||
private $scoreDaoMock;
|
||||
private $favoritesDaoMock;
|
||||
private $userDaoMock;
|
||||
private $transactionManagerMock;
|
||||
private $timeServiceMock;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->scoreDaoMock = $this->mock(\Szurubooru\Dao\ScoreDao::class);
|
||||
$this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class);
|
||||
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
|
||||
$this->transactionManagerMock = $this->mockTransactionManager();
|
||||
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
|
||||
}
|
||||
|
||||
public function testSetting()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User(1);
|
||||
$post = new \Szurubooru\Entities\Post(2);
|
||||
$score = new \Szurubooru\Entities\Score();
|
||||
$score->setUserId($user->getId());
|
||||
$score->setPostId($post->getId());
|
||||
$score->setScore(1);
|
||||
$this->scoreDaoMock->expects($this->once())->method('setScore')->with($user, $post)->willReturn(null);
|
||||
|
||||
$scoreService = $this->getScoreService();
|
||||
$scoreService->setScore($user, $post, 1);
|
||||
}
|
||||
|
||||
public function testSettingInvalid()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User(1);
|
||||
$post = new \Szurubooru\Entities\Post(2);
|
||||
$this->setExpectedException(\Exception::class);
|
||||
$scoreService = $this->getScoreService();
|
||||
$scoreService->setScore($user, $post, 2);
|
||||
}
|
||||
|
||||
public function testGetting()
|
||||
{
|
||||
$user = new \Szurubooru\Entities\User();
|
||||
$post = new \Szurubooru\Entities\Post();
|
||||
$score = new \Szurubooru\Entities\Score(3);
|
||||
$this->scoreDaoMock->expects($this->once())->method('getScore')->with($user, $post)->willReturn($score);
|
||||
|
||||
$scoreService = $this->getScoreService();
|
||||
$retrievedScore = $scoreService->getScore($user, $post);
|
||||
$this->assertEquals($score, $retrievedScore);
|
||||
}
|
||||
|
||||
private function getScoreService()
|
||||
{
|
||||
return new \Szurubooru\Services\ScoreService(
|
||||
$this->scoreDaoMock,
|
||||
$this->favoritesDaoMock,
|
||||
$this->userDaoMock,
|
||||
$this->transactionManagerMock,
|
||||
$this->timeServiceMock);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue