Added Create and Delete tag actions

This commit is contained in:
Alejandro Celaya
2017-07-15 09:00:53 +02:00
parent 6717102dd2
commit b2d9f2fc01
10 changed files with 286 additions and 11 deletions

View File

@@ -3,13 +3,14 @@ namespace Shlinkio\Shlink\Core\Entity;
use Doctrine\ORM\Mapping as ORM;
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
use Shlinkio\Shlink\Core\Repository\TagRepository;
/**
* Class Tag
* @author
* @link
*
* @ORM\Entity()
* @ORM\Entity(repositoryClass=TagRepository::class)
* @ORM\Table(name="tags")
*/
class Tag extends AbstractEntity implements \JsonSerializable

View File

@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Repository;
use Doctrine\ORM\EntityRepository;
use Shlinkio\Shlink\Core\Entity\Tag;
class TagRepository extends EntityRepository implements TagRepositoryInterface
{
/**
* Delete the tags identified by provided names
*
* @param array $names
* @return int The number of affected entries
*/
public function deleteByName(array $names)
{
if (empty($names)) {
return 0;
}
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->delete(Tag::class, 't')
->where($qb->expr()->in('t.name', $names));
return $qb->getQuery()->execute();
}
}

View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Repository;
use Doctrine\Common\Persistence\ObjectRepository;
interface TagRepositoryInterface extends ObjectRepository
{
/**
* Delete the tags identified by provided names
*
* @param array $names
* @return int The number of affected entries
*/
public function deleteByName(array $names);
}

View File

@@ -2,11 +2,16 @@
namespace Shlinkio\Shlink\Core\Service\Tag;
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Shlinkio\Shlink\Core\Entity\Tag;
use Shlinkio\Shlink\Core\Repository\TagRepository;
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
class TagService implements TagServiceInterface
{
use TagManagerTrait;
/**
* @var EntityManagerInterface
*/
@@ -31,4 +36,29 @@ class TagService implements TagServiceInterface
{
return $this->em->getRepository(Tag::class)->findBy([], ['name' => 'ASC']);
}
/**
* @param array $tagNames
* @return void
*/
public function deleteTags(array $tagNames)
{
/** @var TagRepository $repo */
$repo = $this->em->getRepository(Tag::class);
$repo->deleteByName($tagNames);
}
/**
* Provided a list of tag names, creates all that do not exist yet
*
* @param string[] $tagNames
* @return Collection|Tag[]
*/
public function createTags(array $tagNames)
{
$tags = $this->tagNamesToEntities($this->em, $tagNames);
$this->em->flush();
return $tags;
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Shlinkio\Shlink\Core\Service\Tag;
use Doctrine\Common\Collections\Collection;
use Shlinkio\Shlink\Core\Entity\Tag;
interface TagServiceInterface
@@ -9,4 +10,18 @@ interface TagServiceInterface
* @return Tag[]
*/
public function listTags();
/**
* @param string[] $tagNames
* @return void
*/
public function deleteTags(array $tagNames);
/**
* Provided a list of tag names, creates all that do not exist yet
*
* @param string[] $tagNames
* @return Collection|Tag[]
*/
public function createTags(array $tagNames);
}