Skip to content

Commit 2c857c7

Browse files
dev4dev4
dev4
authored and
dev4
committed
1508-blog-graph-ql, add Tag, Author, Comment Repositories [Implemented]
1 parent 2603cdd commit 2c857c7

10 files changed

+665
-3
lines changed

Api/AuthorRepositoryInterface.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Api;
10+
11+
use Magefan\Blog\Model\Author;
12+
13+
/**
14+
* Interface AuthorRepositoryInterface
15+
* @package Magefan\Blog\Api
16+
*/
17+
interface AuthorRepositoryInterface
18+
{
19+
/**
20+
* @param Author $author
21+
* @return mixed
22+
*/
23+
public function save(Author $author);
24+
25+
/**
26+
* @param $authorId
27+
* @return mixed
28+
*/
29+
public function getById($authorId);
30+
31+
/**
32+
* Retrieve Author matching the specified criteria.
33+
*
34+
* @param \Magento\Framework\Api\SearchCriteriaInterface
35+
$searchCriteria
36+
* @return \Magento\Framework\Api\SearchResults
37+
* @throws \Magento\Framework\Exception\LocalizedException
38+
*/
39+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
40+
41+
/**
42+
* @param Author $author
43+
* @return mixed
44+
*/
45+
public function delete(Author $author);
46+
47+
/**
48+
* Delete Author by ID.
49+
*
50+
* @param int $authorId
51+
* @return bool true on success
52+
* @throws \Magento\Framework\Exception\NoSuchEntityException
53+
* @throws \Magento\Framework\Exception\LocalizedException
54+
*/
55+
public function deleteById($authorId);
56+
}

Api/CategoryRepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function delete(Category $category);
4747
/**
4848
* Delete Category by ID.
4949
*
50-
* @param int $$postId
50+
* @param int $categoryId
5151
* @return bool true on success
5252
* @throws \Magento\Framework\Exception\NoSuchEntityException
5353
* @throws \Magento\Framework\Exception\LocalizedException

Api/CommentRepositoryInterface.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Api;
10+
11+
use Magefan\Blog\Model\Comment;
12+
13+
/**
14+
* Interface CommentRepositoryInterface
15+
* @package Magefan\Blog\Api
16+
*/
17+
interface CommentRepositoryInterface
18+
{
19+
/**
20+
* @param Comment $comment
21+
* @return mixed
22+
*/
23+
public function save(Comment $comment);
24+
25+
/**
26+
* @param $commentId
27+
* @return mixed
28+
*/
29+
public function getById($commentId);
30+
31+
/**
32+
* Retrieve Comment matching the specified criteria.
33+
*
34+
* @param \Magento\Framework\Api\SearchCriteriaInterface
35+
$searchCriteria
36+
* @return \Magento\Framework\Api\SearchResults
37+
* @throws \Magento\Framework\Exception\LocalizedException
38+
*/
39+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
40+
41+
/**
42+
* @param Comment $comment
43+
* @return mixed
44+
*/
45+
public function delete(Comment $comment);
46+
47+
/**
48+
* Delete Comment by ID.
49+
*
50+
* @param int $commentId
51+
* @return bool true on success
52+
* @throws \Magento\Framework\Exception\NoSuchEntityException
53+
* @throws \Magento\Framework\Exception\LocalizedException
54+
*/
55+
public function deleteById($commentId);
56+
}

Api/PostRepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function delete(Post $post);
4747
/**
4848
* Delete Post by ID.
4949
*
50-
* @param int $$postId
50+
* @param int $postId
5151
* @return bool true on success
5252
* @throws \Magento\Framework\Exception\NoSuchEntityException
5353
* @throws \Magento\Framework\Exception\LocalizedException

Api/TagRepositoryInterface.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
9+
namespace Magefan\Blog\Api;
10+
11+
use Magefan\Blog\Model\Tag;
12+
13+
/**
14+
* Interface TagRepositoryInterface
15+
* @package Magefan\Blog\Api
16+
*/
17+
interface TagRepositoryInterface
18+
{
19+
/**
20+
* @param Tag $tag
21+
* @return mixed
22+
*/
23+
public function save(Tag $tag);
24+
25+
/**
26+
* @param $tagId
27+
* @return mixed
28+
*/
29+
public function getById($tagId);
30+
31+
/**
32+
* Retrieve Tag matching the specified criteria.
33+
*
34+
* @param \Magento\Framework\Api\SearchCriteriaInterface
35+
$searchCriteria
36+
* @return \Magento\Framework\Api\SearchResults
37+
* @throws \Magento\Framework\Exception\LocalizedException
38+
*/
39+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
40+
41+
/**
42+
* @param Tag $tag
43+
* @return mixed
44+
*/
45+
public function delete(Tag $tag);
46+
47+
/**
48+
* Delete Tag by ID.
49+
*
50+
* @param int $tagId
51+
* @return bool true on success
52+
* @throws \Magento\Framework\Exception\NoSuchEntityException
53+
* @throws \Magento\Framework\Exception\LocalizedException
54+
*/
55+
public function deleteById($tagId);
56+
}

Model/AuthorRepository.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan (support@magefan.com). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
namespace Magefan\Blog\Model;
7+
8+
use Magefan\Blog\Api\AuthorRepositoryInterface;
9+
use Magefan\Blog\Model\AuthorFactory;
10+
use Magefan\Blog\Model\ResourceModel\Author as AuthorResourceModel;
11+
use Magefan\Blog\Model\ResourceModel\Author\CollectionFactory;
12+
use Magento\Framework\Api\SearchResultsFactory;
13+
use Magento\Framework\Api\SearchCriteriaInterface;
14+
use Magento\Framework\DB\Adapter\ConnectionException;
15+
use Magento\Framework\Exception\ValidatorException;
16+
use Magento\Framework\Exception\CouldNotSaveException;
17+
use Magento\Framework\Exception\CouldNotDeleteException;
18+
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\Framework\Exception\StateException;
20+
21+
/**
22+
* Class AuthorRepository
23+
* @package Magefan\Blog\Model
24+
*/
25+
class AuthorRepository implements AuthorRepositoryInterface
26+
{
27+
/**
28+
* @var AuthorFactory
29+
*/
30+
private $authorFactory;
31+
/**
32+
* @var AuthorResourceModel
33+
*/
34+
private $authorResourceModel;
35+
/**
36+
* @var CollectionFactory
37+
*/
38+
private $collectionFactory;
39+
/**
40+
* @var SearchResultsFactory
41+
*/
42+
private $searchResultsFactory;
43+
44+
/**
45+
* AuthorRepository constructor.
46+
* @param \Magefan\Blog\Model\AuthorFactory $authorFactory
47+
* @param AuthorResourceModel $authorResourceModel
48+
* @param CollectionFactory $collectionFactory
49+
* @param SearchResultsFactory $searchResultsFactory
50+
*/
51+
public function __construct(
52+
AuthorFactory $authorFactory,
53+
AuthorResourceModel $authorResourceModel,
54+
CollectionFactory $collectionFactory,
55+
SearchResultsFactory $searchResultsFactory
56+
) {
57+
$this->authorFactory = $authorFactory;
58+
$this->authorResourceModel = $authorResourceModel;
59+
$this->collectionFactory = $collectionFactory;
60+
$this->searchResultsFactory = $searchResultsFactory;
61+
}
62+
63+
/**
64+
* @param Author $author
65+
* @return bool|mixed
66+
* @throws CouldNotSaveException
67+
* @throws NoSuchEntityException
68+
* @throws \Magento\Framework\Exception\AlreadyExistsException
69+
*/
70+
public function save(Author $author)
71+
{
72+
if ($author) {
73+
try {
74+
$this->authorResourceModel->save($author);
75+
} catch (ConnectionException $exception) {
76+
throw new CouldNotSaveException(
77+
__('Database connection error'),
78+
$exception,
79+
$exception->getCode()
80+
);
81+
} catch (CouldNotSaveException $e) {
82+
throw new CouldNotSaveException(__('Unable to save item'), $e);
83+
} catch (ValidatorException $e) {
84+
throw new CouldNotSaveException(__($e->getMessage()));
85+
}
86+
return $this->getById($author->getId());
87+
}
88+
return false;
89+
}
90+
91+
/**
92+
* @param $authorId
93+
* @param bool $editMode
94+
* @param null $storeId
95+
* @param bool $forceReload
96+
* @return Author|mixed
97+
* @throws NoSuchEntityException
98+
*/
99+
public function getById($authorId, $editMode = false, $storeId = null, $forceReload = false)
100+
{
101+
$author = $this->authorFactory->create();
102+
$this->authorResourceModel->load($author, $authorId);
103+
if (!$author->getId()) {
104+
throw new NoSuchEntityException(__('Requested item doesn\'t exist'));
105+
}
106+
return $author;
107+
}
108+
109+
/**
110+
* @param Author $author
111+
* @return bool|mixed
112+
* @throws CouldNotDeleteException
113+
* @throws StateException
114+
*/
115+
public function delete(Author $author)
116+
{
117+
try {
118+
$this->authorResourceModel->delete($author);
119+
} catch (ValidatorException $e) {
120+
throw new CouldNotDeleteException(__($e->getMessage()));
121+
} catch (\Exception $e) {
122+
throw new StateException(
123+
__('Unable to remove item')
124+
);
125+
}
126+
return true;
127+
}
128+
129+
/**
130+
* @param int $authorId
131+
* @return bool|mixed
132+
* @throws CouldNotDeleteException
133+
* @throws NoSuchEntityException
134+
* @throws StateException
135+
*/
136+
public function deleteById($authorId)
137+
{
138+
return $this->delete($this->getById($authorId));
139+
}
140+
141+
/**
142+
* {@inheritdoc}
143+
*/
144+
public function getList(SearchCriteriaInterface $searchCriteria)
145+
{
146+
/** @var \Magefan\Blog\Model\ResourceModel\Author\Collection $collection */
147+
$collection = $this->collectionFactory->create();
148+
149+
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
150+
foreach ($filterGroup->getFilters() as $filter) {
151+
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
152+
$collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
153+
}
154+
}
155+
156+
/** @var \Magento\Framework\Api\searchResultsInterface $searchResult */
157+
$searchResult = $this->searchResultsFactory->create();
158+
$searchResult->setSearchCriteria($searchCriteria);
159+
$searchResult->setTotalCount($collection->getSize());
160+
$searchResult->setItems($collection->getData());
161+
162+
return $searchResult;
163+
}
164+
}

0 commit comments

Comments
 (0)