new datamapper mostly implemented

This commit is contained in:
Dennis Eichhorn 2021-12-11 11:54:16 +01:00
parent e981436e94
commit 049d585be1
9 changed files with 37 additions and 42 deletions

View File

@ -91,7 +91,7 @@ final class ApiController extends Controller
*/
public function apiCommentListUpdate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{
$old = clone CommentListMapper::get((int) $request->getData('id'));
$old = clone CommentListMapper::get()->where('id', (int) $request->getData('id'))->execute();
$new = $this->updateCommentListFromRequest($request);
$this->updateModel($request->header->account, $old, $new, CommentListMapper::class, 'comment_list', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment List', 'Comment list successfully updated', $new);
@ -108,7 +108,7 @@ final class ApiController extends Controller
*/
private function updateCommentListFromRequest(RequestAbstract $request) : CommentList
{
$list = CommentListMapper::get((int) $request->getData('id'));
$list = CommentListMapper::get()->where('id', (int) $request->getData('id'))->execute();
$list->allowEdit = (bool) ($request->getData('allow_edit') ?? $list->allowEdit);
$list->allowVoting = (bool) ($request->getData('allow_voting') ?? $list->allowVoting);
$list->allowFiles = (bool) ($request->getData('allow_upload') ?? $list->allowFiles);
@ -223,7 +223,7 @@ final class ApiController extends Controller
*/
public function apiCommentUpdate(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{
$old = clone CommentMapper::get((int) $request->getData('id'));
$old = clone CommentMapper::get()->where('id', (int) $request->getData('id'))->execute();
$new = $this->updateCommentFromRequest($request);
$this->updateModel($request->header->account, $old, $new, CommentMapper::class, 'comment', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully updated', $new);
@ -240,7 +240,7 @@ final class ApiController extends Controller
*/
private function updateCommentFromRequest(RequestAbstract $request) : Comment
{
$comment = CommentMapper::get((int) $request->getData('id'));
$comment = CommentMapper::get()->where('id', (int) $request->getData('id'))->execute();
$comment->title = $request->getData('title') ?? $comment->getTitle();
$comment->contentRaw = $request->getData('plain') ?? $comment->getContentRaw();
$comment->content = Markdown::parse((string) ($request->getData('plain') ?? $comment->getPlain()));
@ -264,7 +264,7 @@ final class ApiController extends Controller
*/
public function apiCommentGet(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{
$comment = CommentMapper::get((int) $request->getData('id'));
$comment = CommentMapper::get()->where('id', (int) $request->getData('id'))->execute();
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully returned', $comment);
}
@ -283,7 +283,7 @@ final class ApiController extends Controller
*/
public function apiCommentDelete(RequestAbstract $request, ResponseAbstract $response, $data = null) : void
{
$comment = CommentMapper::get((int) $request->getData('id'));
$comment = CommentMapper::get()->where('id', (int) $request->getData('id'))->execute();
$this->deleteModel($request->header->account, $comment, CommentMapper::class, 'comment', $request->getOrigin());
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Comment', 'Comment successfully deleted', $comment);
}

View File

@ -142,7 +142,7 @@ final class BackendController extends Controller
$comment = new Comment();
CommentMapper::create($comment);
CommentMapper::create()->execute($comment);
$response->set('comment', $comment->jsonSerialize());
}

View File

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Modules\Comments\Models;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/**
* Mapper class.
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org
* @since 1.0.0
*/
final class CommentListMapper extends DataMapperAbstract
final class CommentListMapper extends DataMapperFactory
{
/**
* Columns.
@ -32,7 +32,7 @@ final class CommentListMapper extends DataMapperAbstract
* @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 = [
public const COLUMNS = [
'comments_list_id' => ['name' => 'comments_list_id', 'type' => 'int', 'internal' => 'id'],
'comments_list_status' => ['name' => 'comments_list_status', 'type' => 'int', 'internal' => 'status'],
'comments_list_allow_voting' => ['name' => 'comments_list_allow_voting', 'type' => 'bool', 'internal' => 'allowVoting'],
@ -46,7 +46,7 @@ final class CommentListMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0
*/
protected static array $hasMany = [
public const HAS_MANY = [
'comments' => [
'mapper' => CommentMapper::class,
'table' => 'comments_comment',
@ -61,7 +61,7 @@ final class CommentListMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $table = 'comments_list';
public const TABLE = 'comments_list';
/**
* Primary field name.
@ -69,5 +69,5 @@ final class CommentListMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'comments_list_id';
public const PRIMARYFIELD ='comments_list_id';
}

View File

@ -16,7 +16,7 @@ namespace Modules\Comments\Models;
use Modules\Admin\Models\AccountMapper;
use Modules\Media\Models\MediaMapper;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/**
* Mapper class.
@ -26,7 +26,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org
* @since 1.0.0
*/
final class CommentMapper extends DataMapperAbstract
final class CommentMapper extends DataMapperFactory
{
/**
* Columns.
@ -34,7 +34,7 @@ final class CommentMapper extends DataMapperAbstract
* @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 = [
public const COLUMNS = [
'comments_comment_id' => ['name' => 'comments_comment_id', 'type' => 'int', 'internal' => 'id'],
'comments_comment_title' => ['name' => 'comments_comment_title', 'type' => 'string', 'internal' => 'title'],
'comments_comment_status' => ['name' => 'comments_comment_status', 'type' => 'int', 'internal' => 'status'],
@ -52,7 +52,7 @@ final class CommentMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, external:string}>
* @since 1.0.0
*/
protected static array $belongsTo = [
public const BELONGS_TO = [
'createdBy' => [
'mapper' => AccountMapper::class,
'external' => 'comments_comment_created_by',
@ -69,7 +69,7 @@ final class CommentMapper extends DataMapperAbstract
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
* @since 1.0.0
*/
protected static array $hasMany = [
public const HAS_MANY = [
'media' => [
'mapper' => MediaMapper::class,
'table' => 'comments_comment_media',
@ -84,7 +84,7 @@ final class CommentMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $table = 'comments_comment';
public const TABLE = 'comments_comment';
/**
* Created at.
@ -92,7 +92,7 @@ final class CommentMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $createdAt = 'comments_comment_created_at';
public const CREATED_AT = 'comments_comment_created_at';
/**
* Primary field name.
@ -100,5 +100,5 @@ final class CommentMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'comments_comment_id';
public const PRIMARYFIELD ='comments_comment_id';
}

View File

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace Modules\Comments\Models;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
/**
* Mapper class.
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
* @link https://orange-management.org
* @since 1.0.0
*/
final class CommentVoteMapper extends DataMapperAbstract
final class CommentVoteMapper extends DataMapperFactory
{
/**
* Columns.
@ -32,7 +32,7 @@ final class CommentVoteMapper extends DataMapperAbstract
* @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 = [
public const 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],
@ -46,7 +46,7 @@ final class CommentVoteMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $table = 'comments_comment_vote';
public const TABLE = 'comments_comment_vote';
/**
* Created at.
@ -54,7 +54,7 @@ final class CommentVoteMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $createdAt = 'comments_comment_vote_created_at';
public const CREATED_AT = 'comments_comment_vote_created_at';
/**
* Primary field name.
@ -62,7 +62,7 @@ final class CommentVoteMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static string $primaryField = 'comments_comment_vote_id';
public const PRIMARYFIELD ='comments_comment_vote_id';
/**
* Find vote for comment from user
@ -76,12 +76,7 @@ final class CommentVoteMapper extends DataMapperAbstract
*/
public static function findVote(int $comment, int $account) : CommentVote
{
$depth = 3;
$query = self::getQuery();
$query->where(self::$table . '_d' . $depth . '.comments_comment_vote_created_by', '=', $account)
->andWhere(self::$table . '_d' . $depth . '.comments_comment_vote_comment', '=', $comment);
$results = self::getAllByQuery($query);
$results = self::getAll()->where('comment', $comment)->where('createdBy', $account)->execute();
return empty($results) ? new NullCommentVote() : \reset($results);
}

View File

@ -12,7 +12,7 @@ require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/Autoloader.php';
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
use phpOMS\DataStorage\Session\HttpSession;
$CONFIG = [
@ -329,7 +329,7 @@ $GLOBALS['dbpool']->create('delete', $CONFIG['db']['core']['masters']['delete'])
$GLOBALS['dbpool']->create('insert', $CONFIG['db']['core']['masters']['insert']);
$GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']);
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
DataMapperFactory::db($GLOBALS['dbpool']->get());
$GLOBALS['frameworkpath'] = '/phpOMS/';

View File

@ -38,11 +38,11 @@ final class CommentListMapperTest extends \PHPUnit\Framework\TestCase
$list->addComment($comment);
$id = CommentListMapper::create($list);
$id = CommentListMapper::create()->execute($list);
self::assertGreaterThan(0, $list->getId());
self::assertEquals($id, $list->getId());
$listR = CommentListMapper::get($list->getId());
$listR = CommentListMapper::get()->with('comments')->where('id', $list->getId())->execute();
self::assertEquals($id, $listR->getId());
$actual = $listR->getComments();

View File

@ -37,11 +37,11 @@ final class CommentMapperTest extends \PHPUnit\Framework\TestCase
$comment->ref = null;
$comment->list = new CommentList();
$id = CommentMapper::create($comment);
$id = CommentMapper::create()->execute($comment);
self::assertGreaterThan(0, $comment->getId());
self::assertEquals($id, $comment->getId());
$commentR = CommentMapper::get($comment->getId());
$commentR = CommentMapper::get()->where('id', $comment->getId())->execute();
self::assertEquals($id, $commentR->getId());
self::assertEquals($comment->createdBy->getId(), $commentR->createdBy->getId());
self::assertEquals($comment->title, $commentR->title);

View File

@ -34,21 +34,21 @@ final class CommentVoteMapperTest extends \PHPUnit\Framework\TestCase
public function testCR() : void
{
$list = new CommentList();
$lId = CommentListMapper::create($list);
$lId = CommentListMapper::create()->execute($list);
$comment = new Comment();
$comment->title = 'TestComment';
$comment->createdBy = new NullAccount(1);
$comment->list = $lId;
$cId = CommentMapper::create($comment);
$cId = CommentMapper::create()->execute($comment);
$vote = new CommentVote();
$vote->comment = $cId;
$vote->score = 1;
$vote->createdBy = 1;
CommentVoteMapper::create($vote);
CommentVoteMapper::create()->execute($vote);
$voteR = CommentvoteMapper::findVote($cId, 1);
self::assertEquals($vote->comment, $voteR->comment);