diff --git a/TODO b/TODO index c5b5e090..b1bb6ccd 100644 --- a/TODO +++ b/TODO @@ -48,10 +48,8 @@ everything related to posts: - order:time - order:score - order:comment_count - - order:fav_count - order:tag_count - order:comment_time - - order:fav_time - order:file_size - order:random (at least unstable version) diff --git a/src/Dao/EntityConverters/FavoriteEntityConverter.php b/src/Dao/EntityConverters/FavoriteEntityConverter.php index 41522ba4..eaf0b7d9 100644 --- a/src/Dao/EntityConverters/FavoriteEntityConverter.php +++ b/src/Dao/EntityConverters/FavoriteEntityConverter.php @@ -10,6 +10,7 @@ class FavoriteEntityConverter extends AbstractEntityConverter implements IEntity 'id' => $entity->getId(), 'userId' => $entity->getUserId(), 'postId' => $entity->getPostId(), + 'time' => $entity->getTime(), ]; } @@ -18,6 +19,7 @@ class FavoriteEntityConverter extends AbstractEntityConverter implements IEntity $entity = new \Szurubooru\Entities\Favorite($array['id']); $entity->setUserId($array['userId']); $entity->setPostId($array['postId']); + $entity->setTime($array['time']); return $entity; } } diff --git a/src/Entities/Favorite.php b/src/Entities/Favorite.php index af9f0421..918b8877 100644 --- a/src/Entities/Favorite.php +++ b/src/Entities/Favorite.php @@ -5,6 +5,7 @@ class Favorite extends Entity { private $postId; private $userId; + private $time; const LAZY_LOADER_USER = 'user'; const LAZY_LOADER_POST = 'post'; @@ -29,6 +30,16 @@ class Favorite extends Entity $this->postId = $postId; } + public function getTime() + { + return $this->time; + } + + public function setTime($time) + { + $this->time = $time; + } + public function getUser() { return $this->lazyLoad(self::LAZY_LOADER_USER, null); diff --git a/src/SearchServices/Filters/PostFilter.php b/src/SearchServices/Filters/PostFilter.php index e641f669..da5216e8 100644 --- a/src/SearchServices/Filters/PostFilter.php +++ b/src/SearchServices/Filters/PostFilter.php @@ -3,6 +3,9 @@ namespace Szurubooru\SearchServices\Filters; class PostFilter extends BasicFilter implements IFilter { + const ORDER_FAV_TIME = 'lastFavTime'; + const ORDER_FAV_COUNT = 'favCount'; + const REQUIREMENT_TAG = 'tag'; const REQUIREMENT_ID = 'id'; const REQUIREMENT_DATE = 'uploadTime'; diff --git a/src/SearchServices/Parsers/PostSearchParser.php b/src/SearchServices/Parsers/PostSearchParser.php index 418550af..00c44251 100644 --- a/src/SearchServices/Parsers/PostSearchParser.php +++ b/src/SearchServices/Parsers/PostSearchParser.php @@ -64,6 +64,12 @@ class PostSearchParser extends AbstractSearchParser 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'); } diff --git a/src/Services/FavoritesService.php b/src/Services/FavoritesService.php index c8ccdae1..773befc0 100644 --- a/src/Services/FavoritesService.php +++ b/src/Services/FavoritesService.php @@ -6,15 +6,18 @@ class FavoritesService private $favoritesDao; private $userDao; private $transactionManager; + private $timeService; public function __construct( \Szurubooru\Dao\FavoritesDao $favoritesDao, \Szurubooru\Dao\UserDao $userDao, - \Szurubooru\Dao\TransactionManager $transactionManager) + \Szurubooru\Dao\TransactionManager $transactionManager, + \Szurubooru\Services\TimeService $timeService) { $this->favoritesDao = $favoritesDao; $this->userDao = $userDao; $this->transactionManager = $transactionManager; + $this->timeService = $timeService; } public function getFavoriteUsers(\Szurubooru\Entities\Post $post) @@ -42,6 +45,7 @@ class FavoritesService $favorite = new \Szurubooru\Entities\Favorite(); $favorite->setUser($user); $favorite->setPost($post); + $favorite->setTime($this->timeService->getCurrentTime()); $this->favoritesDao->save($favorite); } }; diff --git a/src/Upgrades/Upgrade10.php b/src/Upgrades/Upgrade10.php index e1bca36b..1815f49a 100644 --- a/src/Upgrades/Upgrade10.php +++ b/src/Upgrades/Upgrade10.php @@ -8,12 +8,14 @@ class Upgrade10 implements IUpgrade $pdo = $databaseConnection->getPDO(); $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 ( id INTEGER PRIMARY KEY NOT NULL, userId INTEGER NOT NULL, postId INTEGER NOT NULL, + time TIMESTAMP NOT NULL, UNIQUE (userId, postId) )'); @@ -22,6 +24,11 @@ class Upgrade10 implements IUpgrade FOR EACH ROW BEGIN 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'); $pdo->exec(' @@ -29,6 +36,11 @@ class Upgrade10 implements IUpgrade FOR EACH ROW BEGIN 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'); $pdo->exec(' @@ -36,7 +48,12 @@ class Upgrade10 implements IUpgrade FOR EACH ROW 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 = 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'); } } diff --git a/tests/Dao/FavoritesDaoTest.php b/tests/Dao/FavoritesDaoTest.php index 1f8229c7..68bc57b3 100644 --- a/tests/Dao/FavoritesDaoTest.php +++ b/tests/Dao/FavoritesDaoTest.php @@ -24,6 +24,7 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $favorite = new \Szurubooru\Entities\Favorite(); $favorite->setUser($user); $favorite->setPost($post); + $favorite->setTime('whatever'); $favoritesDao = $this->getFavoritesDao(); $favoritesDao->save($favorite); @@ -33,6 +34,7 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $savedFavorite = $favoritesDao->findById($favorite->getId()); $this->assertEquals(1, $savedFavorite->getUserId()); $this->assertEquals(2, $savedFavorite->getPostId()); + $this->assertEquals('whatever', $savedFavorite->getTime()); $this->assertEntitiesEqual($user, $savedFavorite->getUser()); $this->assertEntitiesEqual($post, $savedFavorite->getPost()); } @@ -47,14 +49,17 @@ class FavoritesDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $fav1 = new \Szurubooru\Entities\Favorite(); $fav1->setUser($user1); $fav1->setPost($post1); + $fav1->setTime('time1'); $fav2 = new \Szurubooru\Entities\Favorite(); $fav2->setUser($user2); $fav2->setPost($post2); + $fav2->setTime('time2'); $fav3 = new \Szurubooru\Entities\Favorite(); $fav3->setUser($user1); $fav3->setPost($post2); + $fav3->setTime('time3'); $favoritesDao = $this->getFavoritesDao(); $favoritesDao->save($fav1); diff --git a/tests/Services/FavoritesServiceTest.php b/tests/Services/FavoritesServiceTest.php index 96ba936b..d4ac3300 100644 --- a/tests/Services/FavoritesServiceTest.php +++ b/tests/Services/FavoritesServiceTest.php @@ -6,6 +6,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase private $favoritesDaoMock; private $userDaoMock; private $transactionManagerMock; + private $timeServiceMock; public function setUp() { @@ -13,6 +14,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase $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 testAddingExisting() @@ -78,6 +80,7 @@ final class FavoritesServiceTest extends \Szurubooru\Tests\AbstractTestCase return new \Szurubooru\Services\FavoritesService( $this->favoritesDaoMock, $this->userDaoMock, - $this->transactionManagerMock); + $this->transactionManagerMock, + $this->timeServiceMock); } }