From 731eb6561b8f6c9992c4b787d95a90c01e98804b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 6 Oct 2014 21:16:11 +0200 Subject: [PATCH] Added creation time to tags --- src/Dao/EntityConverters/TagEntityConverter.php | 2 ++ src/Entities/Tag.php | 12 ++++++++++++ src/Services/TagService.php | 1 + src/Upgrades/Upgrade18.php | 12 ++++++++++++ src/di.php | 1 + tests/Dao/PostDaoTest.php | 2 ++ tests/Dao/TagDaoTest.php | 10 ++++++++-- tests/Services/TagServiceTest.php | 8 ++++---- 8 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 src/Upgrades/Upgrade18.php diff --git a/src/Dao/EntityConverters/TagEntityConverter.php b/src/Dao/EntityConverters/TagEntityConverter.php index 276b9b9d..9856ece3 100644 --- a/src/Dao/EntityConverters/TagEntityConverter.php +++ b/src/Dao/EntityConverters/TagEntityConverter.php @@ -9,6 +9,7 @@ class TagEntityConverter extends AbstractEntityConverter implements IEntityConve [ 'id' => $entity->getId(), 'name' => $entity->getName(), + 'creationTime' => $this->entityTimeToDbTime($entity->getCreationTime()), ]; } @@ -16,6 +17,7 @@ class TagEntityConverter extends AbstractEntityConverter implements IEntityConve { $entity = new \Szurubooru\Entities\Tag($array['id']); $entity->setName($array['name']); + $entity->setCreationTime($this->dbTimeToEntityTime($array['creationTime'])); $entity->setMeta(\Szurubooru\Entities\Tag::META_USAGES, intval($array['usages'])); return $entity; } diff --git a/src/Entities/Tag.php b/src/Entities/Tag.php index 6c070d55..178b8b25 100644 --- a/src/Entities/Tag.php +++ b/src/Entities/Tag.php @@ -4,6 +4,7 @@ namespace Szurubooru\Entities; final class Tag extends Entity { private $name; + private $creationTime; const META_USAGES = 'usages'; @@ -17,8 +18,19 @@ final class Tag extends Entity $this->name = $name; } + public function getCreationTime() + { + return $this->creationTime; + } + + public function setCreationTime($creationTime) + { + $this->creationTime = $creationTime; + } + public function getUsages() { return $this->getMeta(self::META_USAGES); } + } diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 08503e11..97754b1c 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -38,6 +38,7 @@ class TagService { $tag = new \Szurubooru\Entities\Tag; $tag->setName($tagName); + $tag->setCreationTime($this->timeService->getCurrentTime()); $tagsToCreate[] = $tag; } $createdTags = $this->tagDao->batchSave($tagsToCreate); diff --git a/src/Upgrades/Upgrade18.php b/src/Upgrades/Upgrade18.php new file mode 100644 index 00000000..425721b7 --- /dev/null +++ b/src/Upgrades/Upgrade18.php @@ -0,0 +1,12 @@ +getPDO(); + + $pdo->exec('ALTER TABLE tags ADD COLUMN creationTime TIMESTAMP NOT NULL DEFAULT 0'); + } +} diff --git a/src/di.php b/src/di.php index 20989485..740f0b6e 100644 --- a/src/di.php +++ b/src/di.php @@ -33,6 +33,7 @@ return [ $container->get(\Szurubooru\Upgrades\Upgrade15::class), $container->get(\Szurubooru\Upgrades\Upgrade16::class), $container->get(\Szurubooru\Upgrades\Upgrade17::class), + $container->get(\Szurubooru\Upgrades\Upgrade18::class), ]; }), diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index 03d25477..95362d47 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -143,8 +143,10 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { $tag1 = new \Szurubooru\Entities\Tag(); $tag1->setName('tag1'); + $tag1->setCreationTime(date('c')); $tag2 = new \Szurubooru\Entities\Tag(); $tag2->setName('tag2'); + $tag2->setCreationTime(date('c')); $this->tagDao->save($tag1); $this->tagDao->save($tag2); $testTags = ['tag1' => $tag1, 'tag2' => $tag2]; diff --git a/tests/Dao/TagDaoTest.php b/tests/Dao/TagDaoTest.php index 3355738f..f7c09030 100644 --- a/tests/Dao/TagDaoTest.php +++ b/tests/Dao/TagDaoTest.php @@ -15,8 +15,8 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { $pdo = $this->databaseConnection->getPDO(); - $pdo->exec('INSERT INTO tags(id, name) VALUES (1, \'test1\')'); - $pdo->exec('INSERT INTO tags(id, name) VALUES (2, \'test2\')'); + $pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')'); + $pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')'); $pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (5, 1)'); $pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (6, 1)'); $pdo->exec('INSERT INTO postTags(postId, tagId) VALUES (5, 2)'); @@ -24,8 +24,11 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $tag1 = new \Szurubooru\Entities\Tag(1); $tag1->setName('test1'); + $tag1->setCreationTime(date('c', mktime(0, 0, 0, 10, 1, 2014))); $tag2 = new \Szurubooru\Entities\Tag(2); $tag2->setName('test2'); + $tag2->setCreationTime(date('c', mktime(0, 0, 0, 10, 1, 2014))); + $expected = [ $tag1->getId() => $tag1, $tag2->getId() => $tag2, @@ -39,6 +42,7 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { $tag1 = new \Szurubooru\Entities\Tag(); $tag1->setName('test'); + $tag1->setCreationTime(date('c')); $this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test":0}'); $tagDao = $this->getTagDao(); $tagDao->save($tag1); @@ -48,8 +52,10 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { $tag1 = new \Szurubooru\Entities\Tag(); $tag1->setName('test1'); + $tag1->setCreationTime(date('c')); $tag2 = new \Szurubooru\Entities\Tag(); $tag2->setName('test2'); + $tag2->setCreationTime(date('c')); $this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test1":0,"test2":0}'); $tagDao = $this->getTagDao(); $tagDao->batchSave([$tag1, $tag2]); diff --git a/tests/Services/TagServiceTest.php b/tests/Services/TagServiceTest.php index d171bed9..e06863db 100644 --- a/tests/Services/TagServiceTest.php +++ b/tests/Services/TagServiceTest.php @@ -27,8 +27,8 @@ class TagServiceTest extends \Szurubooru\Tests\AbstractDatabaseTestCase public function testCreatingTagsWhenAllExist() { $pdo = $this->databaseConnection->getPDO(); - $pdo->exec('INSERT INTO tags(id, name) VALUES (1, \'test1\')'); - $pdo->exec('INSERT INTO tags(id, name) VALUES (2, \'test2\')'); + $pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')'); + $pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')'); $tag1 = new \Szurubooru\Entities\Tag(); $tag1->setName('test1'); @@ -47,8 +47,8 @@ class TagServiceTest extends \Szurubooru\Tests\AbstractDatabaseTestCase public function testCreatingTagsWhenSomeExist() { $pdo = $this->databaseConnection->getPDO(); - $pdo->exec('INSERT INTO tags(id, name) VALUES (1, \'test1\')'); - $pdo->exec('INSERT INTO tags(id, name) VALUES (2, \'test2\')'); + $pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (1, \'test1\', \'2014-10-01 00:00:00\')'); + $pdo->exec('INSERT INTO tags(id, name, creationTime) VALUES (2, \'test2\', \'2014-10-01 00:00:00\')'); $tag1 = new \Szurubooru\Entities\Tag(); $tag1->setName('test1');