bug fixes

This commit is contained in:
Dennis Eichhorn 2021-06-05 11:01:03 +02:00
parent b2f28c1962
commit aadb600397
7 changed files with 330 additions and 7 deletions

View File

@ -126,23 +126,28 @@
"primary": true,
"autoincrement": true
},
"comments_comment_vote_src": {
"name": "comments_comment_vote_src",
"comments_comment_vote_score": {
"name": "comments_comment_vote_score",
"type": "TINYINT",
"null": false
},
"comments_comment_vote_comment": {
"name": "comments_comment_vote_comment",
"type": "INT",
"null": false,
"foreignTable": "comments_comment",
"foreignKey": "comments_comment_id"
},
"comments_comment_vote_dst": {
"name": "comments_comment_vote_dst",
"comments_comment_vote_created_by": {
"name": "comments_comment_vote_created_by",
"type": "INT",
"null": false,
"foreignTable": "account",
"foreignKey": "account_id"
},
"comments_comment_vote_score": {
"name": "comments_comment_vote_score",
"type": "TINYINT",
"comments_comment_vote_created_at": {
"name": "comments_comment_vote_created_at",
"type": "DATETIME",
"null": false
}
}

View File

@ -1,4 +1,40 @@
<?php declare(strict_types=1);
use Modules\Comments\Controller\ApiController;
use Modules\Comments\Models\PermissionState;
use phpOMS\Account\PermissionType;
use phpOMS\Router\RouteVerb;
return [
'^.*/comment(\?.*|$)' => [
[
'dest' => '\Modules\Comments\Controller\ApiController:apiCommentCreate',
'verb' => RouteVerb::PUT,
'permission' => [
'module' => ApiController::MODULE_NAME,
'type' => PermissionType::CREATE,
'state' => PermissionState::COMMENT,
],
],
[
'dest' => '\Modules\Comments\Controller\ApiController:apiCommentUpdate',
'verb' => RouteVerb::SET,
'permission' => [
'module' => ApiController::MODULE_NAME,
'type' => PermissionType::CREATE,
'state' => PermissionState::COMMENT,
],
],
],
'^.*/comment/vote(\?.*|$)' => [
[
'dest' => '\Modules\Comments\Controller\ApiController:apiChangeCommentVote',
'verb' => RouteVerb::PUT | RouteVerb::SET,
'permission' => [
'module' => ApiController::MODULE_NAME,
'type' => PermissionType::CREATE,
'state' => PermissionState::VOTE,
],
],
],
];

View File

@ -18,6 +18,9 @@ namespace Modules\Comments\Controller;
use Modules\Admin\Models\NullAccount;
use Modules\Comments\Models\Comment;
use Modules\Comments\Models\CommentVote;
use Modules\Comments\Models\NullCommentVote;
use Modules\Comments\Models\CommentVoteMapper;
use Modules\Comments\Models\CommentList;
use Modules\Comments\Models\CommentListMapper;
use Modules\Comments\Models\CommentMapper;
@ -261,4 +264,65 @@ final class ApiController extends Controller
$this->deleteModel($request->header->account, $comment, CommentMapper::class, 'comment', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully deleted', $comment);
}
/**
* Api method to change vote
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param mixed $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
private function apiChangeCommentVote(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{
if (!empty($val = $this->validateAnswerVote($request))) {
$response->set('qa_answer_vote', new FormValidation($val));
$response->header->status = RequestStatusCode::R_400;
return;
}
$vote = CommentVoteMapper::findVote((int) $reqeust->getData('id'), $request->header->account);
if ($vote instanceof NullCommentVote) {
$new = new CommentVote();
$new->score = (int) $request->getData('type');
$new->createdBy = $request->header->account;
$this->createModel($request->header->account, $new, CommentVoteMapper::class, 'comment_vote', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Vote', 'Sucessfully voted.', $new);
} else {
$new = clone $vote;
$new->score = (int) $request->getData('type');
$this->updateModel($request->header->account, $vote, $new, CommentVoteMapper::class, 'comment_vote', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Vote', 'Vote successfully changed.', $new);
}
}
/**
* Validate answer vote request
*
* @param RequestAbstract $request Request
*
* @return array<string, bool> Returns the validation array of the request
*
* @since 1.0.0
*/
private function validateCommentVote(RequestAbstract $request) : array
{
$val = [];
if (($val['id'] = ($request->getData('id') === null))
|| ($val['type'] = ($request->getData('type', 'int') < -1 || $request->getData('type') > 1))
) {
return $val;
}
return [];
}
}

80
Models/CommentVote.php Normal file
View File

@ -0,0 +1,80 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @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 Modules\Admin\Models\Account;
use Modules\Admin\Models\NullAccount;
/**
* Task class.
*
* @package Modules\Comments\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class CommentVote
{
/**
* ID.
*
* @var int
* @since 1.0.0
*/
protected int $id = 0;
/**
* Account.
*
* @var Account
* @since 1.0.0
*/
public Account $createdBy;
/**
* Created at
*
* @var \DateTimeImmutable
* @since 1.0.0
*/
public \DateTimeImmutable $createdAt;
/**
* Comment
*
* @var int
* @since 1.0.0
*/
public int $comment = 0;
/**
* Score
*
* @var int
* @since 1.0.0
*/
public int $score = 0;
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
$this->createdBy = new NullAccount();
$this->createdAt = new \DateTimeImmutable();
}
}

View File

@ -0,0 +1,66 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @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\DataStorage\Database\DataMapperAbstract;
/**
* Mapper class.
*
* @package Modules\Comments\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
final class CommentVoteMapper extends DataMapperAbstract
{
/**
* Columns.
*
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0
*/
protected static array $columns = [
'comments_comment_vote_id' => ['name' => 'comments_comment_vote_id', 'type' => 'int', 'internal' => 'id'],
'comments_comment_vote_score' => ['name' => 'comments_comment_vote_score', 'type' => 'int', 'internal' => 'score'],
'comments_comment_vote_comment' => ['name' => 'comments_comment_vote_comment', 'type' => 'int', 'internal' => 'comment', 'readonly' => true],
'comments_comment_vote_created_by' => ['name' => 'comments_comment_vote_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true],
'comments_comment_vote_created_at' => ['name' => 'comments_comment_vote_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true],
];
/**
* Primary table.
*
* @var string
* @since 1.0.0
*/
protected static string $table = 'comments_comment_vote';
/**
* Created at.
*
* @var string
* @since 1.0.0
*/
protected static string $createdAt = 'comments_comment_vote_created_at';
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'comments_comment_vote_id';
}

View File

@ -0,0 +1,38 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @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 NullCommentVote extends CommentVote
{
/**
* Constructor
*
* @param int $id Model id
*
* @since 1.0.0
*/
public function __construct(int $id = 0)
{
$this->id = $id;
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @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;
/**
* Permision state enum.
*
* @package Modules\Comments\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
abstract class PermissionState extends Enum
{
public const COMMENT = 1;
public const LIST = 2;
public const VOTE = 3;
}