Forgotten code and some other fixes.

This commit is contained in:
Alexey Skobkin 2015-10-26 06:31:24 +03:00
parent 6e32fec5c1
commit 1034ce4692
5 changed files with 69 additions and 12 deletions

View file

@ -55,7 +55,7 @@ class Comment
* @var Post * @var Post
* *
* @ORM\Id * @ORM\Id
* @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post") * @ORM\ManyToOne(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post", inversedBy="comments")
* @ORM\JoinColumn(name="post_id") * @ORM\JoinColumn(name="post_id")
*/ */
private $post; private $post;

View file

@ -73,6 +73,8 @@ class Post
/** /**
* @var Comment[]|ArrayCollection * @var Comment[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity="Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment", mappedBy="post", fetch="EXTRA_LAZY")
*/ */
private $comments; private $comments;
@ -212,7 +214,7 @@ class Post
/** /**
* Get tags * Get tags
* *
* @return ArrayCollection * @return Tag[]|ArrayCollection
*/ */
public function getTags() public function getTags()
{ {
@ -251,4 +253,37 @@ class Post
{ {
return $this->deleted; return $this->deleted;
} }
/**
* Add comments
*
* @param Comment $comment
* @return Post
*/
public function addComment(Comment $comment)
{
$this->comments[] = $comment;
return $this;
}
/**
* Remove comments
*
* @param Comment $comment
*/
public function removeComment(Comment $comment)
{
$this->comments->removeElement($comment);
}
/**
* Get comments
*
* @return Comment[]|ArrayCollection
*/
public function getComments()
{
return $this->comments;
}
} }

View file

@ -6,10 +6,8 @@ use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\CommentFactory;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\TagFactory;
class PostFactory class PostFactory
@ -51,6 +49,13 @@ class PostFactory
$this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post'); $this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post');
} }
/**
* @param array $data
*
* @return Post
* @throws ApiException
* @throws InvalidResponseException
*/
public function createFromArray(array $data) public function createFromArray(array $data)
{ {
$this->validateData($data); $this->validateData($data);
@ -82,14 +87,36 @@ class PostFactory
} }
} }
// Flushing post before linking comments
try {
$this->em->flush($post);
} catch (\Exception $e) {
throw new ApiException(sprintf('Error while flushing changes for #%s: %s', $data['post']['id'], $e->getMessage()), 0, $e);
}
// Comments // Comments
$comments = $this->commentFactory->createFromListArray($data['comments']); $comments = $this->commentFactory->createFromListArray($data['comments']);
// Marking removed comments // Marking removed comments
foreach ($post->getComments() as $comment) {
if (false === in_array($comment, $comments, true)) {
$comment->setDeleted(true);
}
}
// Adding comments
foreach ($comments as $comment) {
$post->addComment($comment);
}
return $post; return $post;
} }
/**
* @param array $data
*
* @throws InvalidResponseException
*/
private function validateData(array $data) private function validateData(array $data)
{ {
if (!array_key_exists('post', $data)) { if (!array_key_exists('post', $data)) {

View file

@ -5,7 +5,6 @@ namespace Skobkin\Bundle\PointToolsBundle\Service\Factory;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Entity\User; use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException; use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;

View file

@ -2,12 +2,8 @@
namespace Skobkin\Bundle\PointToolsBundle\Service; namespace Skobkin\Bundle\PointToolsBundle\Service;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Guzzle\Service\Client; use Guzzle\Service\Client;
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post; use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
use Skobkin\Bundle\PointToolsBundle\Entity\User;
use Skobkin\Bundle\PointToolsBundle\Service\Factory\PostFactory; use Skobkin\Bundle\PointToolsBundle\Service\Factory\PostFactory;
/** /**
@ -34,11 +30,11 @@ class PostApi extends AbstractApi
} }
/** /**
* Get post with comments by id * Get post with tags and comments by id
* *
* @param $id * @param $id
* *
* @return User[] * @return Post[]
*/ */
public function getPostById($id) public function getPostById($id)
{ {