From 049d585be10ea933e992f816d90c840cdf6f960c Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 11 Dec 2021 11:54:16 +0100 Subject: [PATCH] new datamapper mostly implemented --- Controller/ApiController.php | 12 ++++++------ Controller/BackendController.php | 2 +- Models/CommentListMapper.php | 12 ++++++------ Models/CommentMapper.php | 16 ++++++++-------- Models/CommentVoteMapper.php | 19 +++++++------------ tests/Bootstrap.php | 4 ++-- tests/Models/CommentListMapperTest.php | 4 ++-- tests/Models/CommentMapperTest.php | 4 ++-- tests/Models/CommentVoteMapperTest.php | 6 +++--- 9 files changed, 37 insertions(+), 42 deletions(-) diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 42e803d..841bca9 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -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); } diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 1df0eff..1f483a6 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -142,7 +142,7 @@ final class BackendController extends Controller $comment = new Comment(); - CommentMapper::create($comment); + CommentMapper::create()->execute($comment); $response->set('comment', $comment->jsonSerialize()); } diff --git a/Models/CommentListMapper.php b/Models/CommentListMapper.php index cb20043..8a1dd59 100755 --- a/Models/CommentListMapper.php +++ b/Models/CommentListMapper.php @@ -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 * @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 * @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'; } diff --git a/Models/CommentMapper.php b/Models/CommentMapper.php index f789b61..4c242ec 100755 --- a/Models/CommentMapper.php +++ b/Models/CommentMapper.php @@ -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 * @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 * @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 * @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'; } diff --git a/Models/CommentVoteMapper.php b/Models/CommentVoteMapper.php index e4d653e..fd12782 100755 --- a/Models/CommentVoteMapper.php +++ b/Models/CommentVoteMapper.php @@ -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 * @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); } diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index f5fc99c..252a62f 100755 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -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/'; diff --git a/tests/Models/CommentListMapperTest.php b/tests/Models/CommentListMapperTest.php index 37657e7..f6a3d6c 100755 --- a/tests/Models/CommentListMapperTest.php +++ b/tests/Models/CommentListMapperTest.php @@ -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(); diff --git a/tests/Models/CommentMapperTest.php b/tests/Models/CommentMapperTest.php index 0fe9df3..36e0b9a 100755 --- a/tests/Models/CommentMapperTest.php +++ b/tests/Models/CommentMapperTest.php @@ -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); diff --git a/tests/Models/CommentVoteMapperTest.php b/tests/Models/CommentVoteMapperTest.php index 2e875dd..0f9c0b3 100644 --- a/tests/Models/CommentVoteMapperTest.php +++ b/tests/Models/CommentVoteMapperTest.php @@ -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);