diff --git a/TODO b/TODO index ab4704f6..ae278c4f 100644 --- a/TODO +++ b/TODO @@ -125,7 +125,6 @@ everything related to comments: - score (see notes about scoring) refactors: - - post view proxy should retrieve full user - centralize markdown prefix decorators - add enum validation in IValidatables (needs refactors of enums and possible disposal of EnumHelper in favor of something more subtle) diff --git a/src/Controllers/ViewProxies/PostViewProxy.php b/src/Controllers/ViewProxies/PostViewProxy.php index 3309f7c6..ee6f91bb 100644 --- a/src/Controllers/ViewProxies/PostViewProxy.php +++ b/src/Controllers/ViewProxies/PostViewProxy.php @@ -4,10 +4,14 @@ namespace Szurubooru\Controllers\ViewProxies; class PostViewProxy extends AbstractViewProxy { private $tagViewProxy; + private $userViewProxy; - public function __construct(TagViewProxy $tagViewProxy) + public function __construct( + TagViewProxy $tagViewProxy, + UserViewProxy $userViewProxy) { $this->tagViewProxy = $tagViewProxy; + $this->userViewProxy = $userViewProxy; } public function fromEntity($post) @@ -17,7 +21,6 @@ class PostViewProxy extends AbstractViewProxy { $result->id = $post->getId(); $result->name = $post->getName(); - $result->userId = $post->getUserId(); $result->uploadTime = $post->getUploadTime(); $result->lastEditTime = $post->getLastEditTime(); $result->safety = \Szurubooru\Helpers\EnumHelper::postSafetyToString($post->getSafety()); @@ -29,6 +32,7 @@ class PostViewProxy extends AbstractViewProxy $result->imageHeight = $post->getImageHeight(); $result->tags = $this->tagViewProxy->fromArray($post->getTags()); $result->originalFileSize = $post->getOriginalFileSize(); + $result->user = $this->userViewProxy->fromEntity($post->getUser()); } return $result; } diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index d258a156..69a6c5e1 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -4,12 +4,14 @@ namespace Szurubooru\Dao; class PostDao extends AbstractDao implements ICrudDao { private $tagDao; + private $userDao; private $fileService; private $thumbnailService; public function __construct( \Szurubooru\DatabaseConnection $databaseConnection, \Szurubooru\Dao\TagDao $tagDao, + \Szurubooru\Dao\UserDao $userDao, \Szurubooru\Services\FileService $fileService, \Szurubooru\Services\ThumbnailService $thumbnailService) { @@ -19,6 +21,7 @@ class PostDao extends AbstractDao implements ICrudDao new \Szurubooru\Dao\EntityConverters\PostEntityConverter()); $this->tagDao = $tagDao; + $this->userDao = $userDao; $this->fileService = $fileService; $this->thumbnailService = $thumbnailService; } @@ -49,6 +52,13 @@ class PostDao extends AbstractDao implements ICrudDao return $this->fileService->load($post->getThumbnailSourceContentPath()); }); + $post->setLazyLoader( + \Szurubooru\Entities\Post::LAZY_LOADER_USER, + function(\Szurubooru\Entities\Post $post) + { + return $this->getUser($post); + }); + $post->setLazyLoader( \Szurubooru\Entities\Post::LAZY_LOADER_TAGS, function(\Szurubooru\Entities\Post $post) @@ -69,6 +79,11 @@ class PostDao extends AbstractDao implements ICrudDao return $this->tagDao->findByPostId($post->getId()); } + private function getUser(\Szurubooru\Entities\Post $post) + { + return $this->userDao->findById($post->getUserId()); + } + private function syncContent(\Szurubooru\Entities\Post $post) { $targetPath = $post->getContentPath(); diff --git a/src/Entities/Post.php b/src/Entities/Post.php index 8b32379f..c4520e52 100644 --- a/src/Entities/Post.php +++ b/src/Entities/Post.php @@ -12,6 +12,7 @@ final class Post extends Entity const POST_TYPE_VIDEO = 3; const POST_TYPE_YOUTUBE = 4; + const LAZY_LOADER_USER = 'user'; const LAZY_LOADER_TAGS = 'tags'; const LAZY_LOADER_CONTENT = 'content'; const LAZY_LOADER_THUMBNAIL_SOURCE_CONTENT = 'thumbnailSourceContent'; @@ -57,18 +58,6 @@ final class Post extends Entity $this->userId = $userId; } - public function setUser(\Szurubooru\Entities\User $user = null) - { - if ($user) - { - $this->userId = $user->getId(); - } - else - { - $this->userId = null; - } - } - public function getSafety() { return $this->safety; @@ -190,6 +179,24 @@ final class Post extends Entity $this->setMeta(self::META_TAG_COUNT, count($tags)); } + public function getUser() + { + return $this->lazyLoad(self::LAZY_LOADER_USER, null); + } + + public function setUser(\Szurubooru\Entities\User $user = null) + { + $this->lazySave(self::LAZY_LOADER_USER, $user); + if ($user) + { + $this->userId = $user->getId(); + } + else + { + $this->userId = null; + } + } + public function getContent() { return $this->lazyLoad(self::LAZY_LOADER_CONTENT, null); diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index 31b2a7f7..e0e6ab1e 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -3,16 +3,21 @@ namespace Szurubooru\Tests\Dao; final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { - private $tagDao; private $fileServiceMock; private $thumbnailServiceMock; + private $tagDao; + private $userDao; public function setUp() { parent::setUp(); - $this->tagDao = new \Szurubooru\Dao\TagDao($this->databaseConnection); $this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class); $this->thumbnailServiceMock = $this->mock(\Szurubooru\Services\ThumbnailService::class); + $this->tagDao = new \Szurubooru\Dao\TagDao($this->databaseConnection); + $this->userDao = new \Szurubooru\Dao\UserDao( + $this->databaseConnection, + $this->fileServiceMock, + $this->thumbnailServiceMock); } public function testCreating() @@ -132,6 +137,21 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $this->assertEquals(2, count($tagDao->findAll())); } + public function testSavingUser() + { + $testUser = new \Szurubooru\Entities\User(5); + $testUser->setName('it\'s me'); + $postDao = $this->getPostDao(); + + $post = $this->getPost(); + $post->setUser($testUser); + $postDao->save($post); + + $savedPost = $postDao->findById($post->getId()); + $this->assertEntitiesEqual($testUser, $post->getUser()); + $this->assertEquals(5, $post->getUserId()); + } + public function testNotLoadingContentForNewPosts() { $postDao = $this->getPostDao(); @@ -206,6 +226,7 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase return new \Szurubooru\Dao\PostDao( $this->databaseConnection, $this->tagDao, + $this->userDao, $this->fileServiceMock, $this->thumbnailServiceMock); }