Added order:fav_date and order:fav_count support

This commit is contained in:
Marcin Kurczewski 2014-09-28 11:58:07 +02:00
parent e811b1a876
commit 8a9bf259e4
9 changed files with 54 additions and 5 deletions

2
TODO
View file

@ -48,10 +48,8 @@ everything related to posts:
- order:time - order:time
- order:score - order:score
- order:comment_count - order:comment_count
- order:fav_count
- order:tag_count - order:tag_count
- order:comment_time - order:comment_time
- order:fav_time
- order:file_size - order:file_size
- order:random (at least unstable version) - order:random (at least unstable version)

View file

@ -10,6 +10,7 @@ class FavoriteEntityConverter extends AbstractEntityConverter implements IEntity
'id' => $entity->getId(), 'id' => $entity->getId(),
'userId' => $entity->getUserId(), 'userId' => $entity->getUserId(),
'postId' => $entity->getPostId(), 'postId' => $entity->getPostId(),
'time' => $entity->getTime(),
]; ];
} }
@ -18,6 +19,7 @@ class FavoriteEntityConverter extends AbstractEntityConverter implements IEntity
$entity = new \Szurubooru\Entities\Favorite($array['id']); $entity = new \Szurubooru\Entities\Favorite($array['id']);
$entity->setUserId($array['userId']); $entity->setUserId($array['userId']);
$entity->setPostId($array['postId']); $entity->setPostId($array['postId']);
$entity->setTime($array['time']);
return $entity; return $entity;
} }
} }

View file

@ -5,6 +5,7 @@ class Favorite extends Entity
{ {
private $postId; private $postId;
private $userId; private $userId;
private $time;
const LAZY_LOADER_USER = 'user'; const LAZY_LOADER_USER = 'user';
const LAZY_LOADER_POST = 'post'; const LAZY_LOADER_POST = 'post';
@ -29,6 +30,16 @@ class Favorite extends Entity
$this->postId = $postId; $this->postId = $postId;
} }
public function getTime()
{
return $this->time;
}
public function setTime($time)
{
$this->time = $time;
}
public function getUser() public function getUser()
{ {
return $this->lazyLoad(self::LAZY_LOADER_USER, null); return $this->lazyLoad(self::LAZY_LOADER_USER, null);

View file

@ -3,6 +3,9 @@ namespace Szurubooru\SearchServices\Filters;
class PostFilter extends BasicFilter implements IFilter class PostFilter extends BasicFilter implements IFilter
{ {
const ORDER_FAV_TIME = 'lastFavTime';
const ORDER_FAV_COUNT = 'favCount';
const REQUIREMENT_TAG = 'tag'; const REQUIREMENT_TAG = 'tag';
const REQUIREMENT_ID = 'id'; const REQUIREMENT_ID = 'id';
const REQUIREMENT_DATE = 'uploadTime'; const REQUIREMENT_DATE = 'uploadTime';

View file

@ -64,6 +64,12 @@ class PostSearchParser extends AbstractSearchParser
protected function getOrderColumn($token) protected function getOrderColumn($token)
{ {
if ($token === 'fav_time')
return \Szurubooru\SearchServices\Filters\PostFilter::ORDER_FAV_TIME;
elseif ($token === 'fav_count')
return \Szurubooru\SearchServices\Filters\PostFilter::ORDER_FAV_COUNT;
throw new \BadMethodCallException('Not supported'); throw new \BadMethodCallException('Not supported');
} }

View file

@ -6,15 +6,18 @@ class FavoritesService
private $favoritesDao; private $favoritesDao;
private $userDao; private $userDao;
private $transactionManager; private $transactionManager;
private $timeService;
public function __construct( public function __construct(
\Szurubooru\Dao\FavoritesDao $favoritesDao, \Szurubooru\Dao\FavoritesDao $favoritesDao,
\Szurubooru\Dao\UserDao $userDao, \Szurubooru\Dao\UserDao $userDao,
\Szurubooru\Dao\TransactionManager $transactionManager) \Szurubooru\Dao\TransactionManager $transactionManager,
\Szurubooru\Services\TimeService $timeService)
{ {
$this->favoritesDao = $favoritesDao; $this->favoritesDao = $favoritesDao;
$this->userDao = $userDao; $this->userDao = $userDao;
$this->transactionManager = $transactionManager; $this->transactionManager = $transactionManager;
$this->timeService = $timeService;
} }
public function getFavoriteUsers(\Szurubooru\Entities\Post $post) public function getFavoriteUsers(\Szurubooru\Entities\Post $post)
@ -42,6 +45,7 @@ class FavoritesService
$favorite = new \Szurubooru\Entities\Favorite(); $favorite = new \Szurubooru\Entities\Favorite();
$favorite->setUser($user); $favorite->setUser($user);
$favorite->setPost($post); $favorite->setPost($post);
$favorite->setTime($this->timeService->getCurrentTime());
$this->favoritesDao->save($favorite); $this->favoritesDao->save($favorite);
} }
}; };

View file

@ -8,12 +8,14 @@ class Upgrade10 implements IUpgrade
$pdo = $databaseConnection->getPDO(); $pdo = $databaseConnection->getPDO();
$pdo->exec('ALTER TABLE posts ADD COLUMN favCount INTEGER NOT NULL DEFAULT 0'); $pdo->exec('ALTER TABLE posts ADD COLUMN favCount INTEGER NOT NULL DEFAULT 0');
$pdo->exec('ALTER TABLE posts ADD COLUMN lastFavTime TIMESTAMP');
$pdo->exec('CREATE TABLE favorites $pdo->exec('CREATE TABLE favorites
( (
id INTEGER PRIMARY KEY NOT NULL, id INTEGER PRIMARY KEY NOT NULL,
userId INTEGER NOT NULL, userId INTEGER NOT NULL,
postId INTEGER NOT NULL, postId INTEGER NOT NULL,
time TIMESTAMP NOT NULL,
UNIQUE (userId, postId) UNIQUE (userId, postId)
)'); )');
@ -22,6 +24,11 @@ class Upgrade10 implements IUpgrade
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
UPDATE posts SET favCount = favCount - 1 WHERE posts.id = OLD.postId; UPDATE posts SET favCount = favCount - 1 WHERE posts.id = OLD.postId;
UPDATE posts SET lastFavTime = (
SELECT MAX(time) FROM favorites
WHERE favorites.postId = posts.id)
WHERE posts.id = OLD.postId;
END'); END');
$pdo->exec(' $pdo->exec('
@ -29,6 +36,11 @@ class Upgrade10 implements IUpgrade
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
UPDATE posts SET favCount = favCount + 1 WHERE posts.id = NEW.postId; UPDATE posts SET favCount = favCount + 1 WHERE posts.id = NEW.postId;
UPDATE posts SET lastFavTime = (
SELECT MAX(time) FROM favorites
WHERE favorites.postId = posts.id)
WHERE posts.id = NEW.postId;
END'); END');
$pdo->exec(' $pdo->exec('
@ -36,7 +48,12 @@ class Upgrade10 implements IUpgrade
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
UPDATE posts SET favCount = favCount + 1 WHERE posts.id = NEW.postId; UPDATE posts SET favCount = favCount + 1 WHERE posts.id = NEW.postId;
UPDATE posts SET favCount = favCount - 1 WHERE posts.id = NEW.postId; UPDATE posts SET favCount = favCount - 1 WHERE posts.id = OLD.postId;
UPDATE posts SET lastFavTime = (
SELECT MAX(time) FROM favorites
WHERE favorites.postId = posts.id)
WHERE posts.id IN (OLD.postId, NEW.postId);
END'); END');
} }
} }

View file

@ -24,6 +24,7 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$favorite = new \Szurubooru\Entities\Favorite(); $favorite = new \Szurubooru\Entities\Favorite();
$favorite->setUser($user); $favorite->setUser($user);
$favorite->setPost($post); $favorite->setPost($post);
$favorite->setTime('whatever');
$favoritesDao = $this->getFavoritesDao(); $favoritesDao = $this->getFavoritesDao();
$favoritesDao->save($favorite); $favoritesDao->save($favorite);
@ -33,6 +34,7 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$savedFavorite = $favoritesDao->findById($favorite->getId()); $savedFavorite = $favoritesDao->findById($favorite->getId());
$this->assertEquals(1, $savedFavorite->getUserId()); $this->assertEquals(1, $savedFavorite->getUserId());
$this->assertEquals(2, $savedFavorite->getPostId()); $this->assertEquals(2, $savedFavorite->getPostId());
$this->assertEquals('whatever', $savedFavorite->getTime());
$this->assertEntitiesEqual($user, $savedFavorite->getUser()); $this->assertEntitiesEqual($user, $savedFavorite->getUser());
$this->assertEntitiesEqual($post, $savedFavorite->getPost()); $this->assertEntitiesEqual($post, $savedFavorite->getPost());
} }
@ -47,14 +49,17 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
$fav1 = new \Szurubooru\Entities\Favorite(); $fav1 = new \Szurubooru\Entities\Favorite();
$fav1->setUser($user1); $fav1->setUser($user1);
$fav1->setPost($post1); $fav1->setPost($post1);
$fav1->setTime('time1');
$fav2 = new \Szurubooru\Entities\Favorite(); $fav2 = new \Szurubooru\Entities\Favorite();
$fav2->setUser($user2); $fav2->setUser($user2);
$fav2->setPost($post2); $fav2->setPost($post2);
$fav2->setTime('time2');
$fav3 = new \Szurubooru\Entities\Favorite(); $fav3 = new \Szurubooru\Entities\Favorite();
$fav3->setUser($user1); $fav3->setUser($user1);
$fav3->setPost($post2); $fav3->setPost($post2);
$fav3->setTime('time3');
$favoritesDao = $this->getFavoritesDao(); $favoritesDao = $this->getFavoritesDao();
$favoritesDao->save($fav1); $favoritesDao->save($fav1);

View file

@ -6,6 +6,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase
private $favoritesDaoMock; private $favoritesDaoMock;
private $userDaoMock; private $userDaoMock;
private $transactionManagerMock; private $transactionManagerMock;
private $timeServiceMock;
public function setUp() public function setUp()
{ {
@ -13,6 +14,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase
$this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class); $this->favoritesDaoMock = $this->mock(\Szurubooru\Dao\FavoritesDao::class);
$this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class); $this->userDaoMock = $this->mock(\Szurubooru\Dao\UserDao::class);
$this->transactionManagerMock = $this->mockTransactionManager(); $this->transactionManagerMock = $this->mockTransactionManager();
$this->timeServiceMock = $this->mock(\Szurubooru\Services\TimeService::class);
} }
public function testAddingExisting() public function testAddingExisting()
@ -78,6 +80,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase
return new \Szurubooru\Services\FavoritesService( return new \Szurubooru\Services\FavoritesService(
$this->favoritesDaoMock, $this->favoritesDaoMock,
$this->userDaoMock, $this->userDaoMock,
$this->transactionManagerMock); $this->transactionManagerMock,
$this->timeServiceMock);
} }
} }