Added user lazy loading

This commit is contained in:
Marcin Kurczewski 2014-09-21 18:21:54 +02:00
parent fa72060502
commit 1d72cec84b
5 changed files with 63 additions and 17 deletions

1
TODO
View file

@ -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)

View file

@ -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;
}

View file

@ -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();

View file

@ -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);

View file

@ -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);
}