mirror of
https://github.com/Karaka-Management/oms-Comments.git
synced 2026-01-11 16:18:41 +00:00
continue with comments impl.
This commit is contained in:
parent
005aa1b1fe
commit
f5cae0976e
212
Controller/ApiController.php
Normal file
212
Controller/ApiController.php
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package Modules\Comments
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Modules\Comments\Controller;
|
||||
|
||||
use Modules\Admin\Models\NullAccount;
|
||||
use Modules\Comments\Models\CommentList;
|
||||
use Modules\Comments\Models\Comment;
|
||||
use phpOMS\Message\RequestAbstract;
|
||||
use phpOMS\Message\ResponseAbstract;
|
||||
use phpOMS\Message\NotificationLevel;
|
||||
use phpOMS\Model\Message\FormValidation;
|
||||
use phpOMS\Utils\Parser\Markdown\Markdown;
|
||||
use Modules\Comments\Models\CommentMapper;
|
||||
|
||||
/**
|
||||
* Comments controller class.
|
||||
*
|
||||
* @package Modules\Comments
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
final class ApiController extends Controller
|
||||
{
|
||||
/**
|
||||
* Api method to create comment list
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
* @param mixed $data Generic data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @api
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function apiCommentListCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
|
||||
{
|
||||
$commentList = $this->createCommentList();
|
||||
$this->createModel($request->getHeader()->getAccount(), $commentList, CommentListMapper::class, 'comment_list', $request->getOrigin());
|
||||
}
|
||||
|
||||
public function createCommentList() : CommentList
|
||||
{
|
||||
$list = new CommentList();
|
||||
// @todo: allow config
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Api method to create comment
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
* @param mixed $data Generic data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @api
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function apiCommentCreate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
|
||||
{
|
||||
if (!empty($val = $this->validateCommentCreate($request))) {
|
||||
$response->set('news_create', new FormValidation($val));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$comment = $this->createCommentFromRequest($request);
|
||||
$this->createModel($request->getHeader()->getAccount(), $comment, CommentMapper::class, 'comment', $request->getOrigin());
|
||||
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully created', $comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate comment create request
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
*
|
||||
* @return array<string, bool>
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function validateCommentCreate(RequestAbstract $request) : array
|
||||
{
|
||||
$val = [];
|
||||
if (($val['title'] = empty($request->getData('title')))
|
||||
|| ($val['plain'] = empty($request->getData('plain')))
|
||||
) {
|
||||
return $val;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to create comment from request.
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
*
|
||||
* @return Comment
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function createCommentFromRequest(RequestAbstract $request) : Comment
|
||||
{
|
||||
$comment = new Comment();
|
||||
$comment->setCreatedBy(new NullAccount($request->getHeader()->getAccount()));
|
||||
$comment->setTitle((string) ($request->getData('title') ?? ''));
|
||||
$comment->setContentRaw($request->getData('plain') ?? '');
|
||||
$comment->setContent(Markdown::parse((string) ($request->getData('plain') ?? '')));
|
||||
$comment->setRef($request->getData('ref') !== null ? (int) $request->getData('ref') : null);
|
||||
$comment->setList((int) ($request->getData('list') ?? 0));
|
||||
|
||||
return $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Api method to create comment
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
* @param mixed $data Generic data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @api
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function apiCommentUpdate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
|
||||
{
|
||||
$old = clone CommentMapper::get((int) $request->getData('id'));
|
||||
$new = $this->updateCommentFromRequest($request);
|
||||
$this->updateModel($request->getHeader()->getAccount(), $old, $new, CommentMapper::class, 'comment', $request->getOrigin());
|
||||
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully updated', $new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to update comment from request.
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
*
|
||||
* @return Comment
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private function updateCommentFromRequest(RequestAbstract $request) : Comment
|
||||
{
|
||||
$comment = CommentMapper::get((int) $request->getData('id'));
|
||||
$comment->setTitle($request->getData('title') ?? $comment->getTitle());
|
||||
$comment->setContentRaw($request->getData('plain') ?? $comment->getContentRaw());
|
||||
$comment->setContent(Markdown::parse((string) ($request->getData('plain') ?? $comment->getPlain())));
|
||||
$comment->setRef($request->getData('ref') ?? $comment->getRef());
|
||||
|
||||
return $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Api method to get a comment
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
* @param mixed $data Generic data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @api
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function apiCommentGet(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
|
||||
{
|
||||
$comment = CommentMapper::get((int) $request->getData('id'));
|
||||
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully returned', $comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Api method to delete comment
|
||||
*
|
||||
* @param RequestAbstract $request Request
|
||||
* @param ResponseAbstract $response Response
|
||||
* @param mixed $data Generic data
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @api
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function apiCommentDelete(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
|
||||
{
|
||||
$comment = CommentMapper::get((int) $request->getData('id'));
|
||||
$this->deleteModel($request->getHeader()->getAccount(), $comment, CommentMapper::class, 'comment', $request->getOrigin());
|
||||
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully deleted', $comment);
|
||||
}
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ class Comment
|
|||
/**
|
||||
* Comment list this comment belongs to
|
||||
*
|
||||
* @var int
|
||||
* @var int|CommentList
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private int $list = 0;
|
||||
|
|
@ -73,7 +73,7 @@ class Comment
|
|||
* @var int
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private int $status = 0;
|
||||
private int $status = CommentStatus::VISIBLE;
|
||||
|
||||
/**
|
||||
* Content
|
||||
|
|
@ -94,10 +94,10 @@ class Comment
|
|||
/**
|
||||
* Comment this is refering to
|
||||
*
|
||||
* @var null|self
|
||||
* @var null|int|self
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private ?Comment $ref = null;
|
||||
private $ref = null;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
|
|
@ -149,15 +149,41 @@ class Comment
|
|||
}
|
||||
|
||||
/**
|
||||
* Set the list this comment belongs to
|
||||
* Set the status
|
||||
*
|
||||
* @param int $list List
|
||||
* @param int $status Status
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function setList(int $list) : void
|
||||
public function setStatus(int $status) : void
|
||||
{
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the status
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function getStatus() : int
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the list this comment belongs to
|
||||
*
|
||||
* @param int|CommentList $list List
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function setList($list) : void
|
||||
{
|
||||
$this->list = $list;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,12 +33,21 @@ class CommentList
|
|||
protected int $id = 0;
|
||||
|
||||
/**
|
||||
* Comments
|
||||
*
|
||||
* @var array
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private array $comments = [];
|
||||
|
||||
/**
|
||||
* Is active
|
||||
*
|
||||
* @var bool
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected bool $isActive = true;
|
||||
|
||||
/**
|
||||
* Get id.
|
||||
*
|
||||
|
|
@ -54,7 +63,7 @@ class CommentList
|
|||
/**
|
||||
* Get the comments
|
||||
*
|
||||
* @return array
|
||||
* @return int[]|Comment[]
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
|
@ -76,4 +85,30 @@ class CommentList
|
|||
{
|
||||
$this->comments[] = $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is active
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function isActive() : bool
|
||||
{
|
||||
return $this->isActive;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set list activity
|
||||
*
|
||||
* @param bool $active Is active
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function setActive(bool $active) : void
|
||||
{
|
||||
$this->isActive = $active;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,10 @@ final class CommentMapper extends DataMapperAbstract
|
|||
'mapper' => AccountMapper::class,
|
||||
'self' => 'comments_comment_created_by',
|
||||
],
|
||||
'list' => [
|
||||
'mapper' => CommentListMapper::class,
|
||||
'self' => 'comments_comment_list',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
31
Models/CommentStatus.php
Normal file
31
Models/CommentStatus.php
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package Modules\Comments\Models
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Modules\Comments\Models;
|
||||
|
||||
use phpOMS\Stdlib\Base\Enum;
|
||||
|
||||
/**
|
||||
* Comment Status enum.
|
||||
*
|
||||
* @package Modules\Comments\Models
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract class CommentStatus extends Enum
|
||||
{
|
||||
public const VISIBLE = 1;
|
||||
public const BLOCKED = 2;
|
||||
}
|
||||
39
Models/NullCommentList.php
Normal file
39
Models/NullCommentList.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.4
|
||||
*
|
||||
* @package Modules\Comment\Models
|
||||
* @copyright Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Modules\Comments\Models;
|
||||
|
||||
/**
|
||||
* Null model
|
||||
*
|
||||
* @package Modules\Comment\Models
|
||||
* @license OMS License 1.0
|
||||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
final class NullCommentList extends CommentList
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $id Model id
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function __construct(int $id = 0)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->isActive = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -34,8 +34,12 @@ class CommentListTest extends \PHPUnit\Framework\TestCase
|
|||
$list = new CommentList();
|
||||
$comment = new Comment();
|
||||
$comment->setTitle('Test Comment');
|
||||
$comment->setContentRaw('TestRaw');
|
||||
$comment->setContent('TestContent');
|
||||
|
||||
$list->addComment($comment);
|
||||
self::assertEquals('Test Comment', $list->getComments()[0]->getTitle());
|
||||
self::assertEquals('Test Comment', $list->getComments()[0]->getContentRaw());
|
||||
self::assertEquals('Test Comment', $list->getComments()[0]->getcontent());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ namespace Modules\Comments\tests\Models;
|
|||
use Modules\Admin\Models\NullAccount;
|
||||
use Modules\Comments\Models\Comment;
|
||||
use Modules\Comments\Models\CommentMapper;
|
||||
use Modules\Comments\Models\CommentList;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
|
@ -30,7 +31,7 @@ class CommentMapperTest extends \PHPUnit\Framework\TestCase
|
|||
$comment->setTitle('Test Title');
|
||||
$comment->setContent('Test Content');
|
||||
$comment->setRef(null);
|
||||
$comment->setList(1);
|
||||
$comment->setList(new CommentList());
|
||||
|
||||
$id = CommentMapper::create($comment);
|
||||
self::assertGreaterThan(0, $comment->getId());
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user