From f74e885a5397b450136fdf9f82dcedfc948caea8 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 19 Dec 2021 20:20:39 +0100 Subject: [PATCH] bug fixes / dbmapper fixes --- .../QA/Themes/Default/tpl/footer.tpl.php | 14 +++++++- Controller/ApiController.php | 12 ++++--- Controller/BackendController.php | 4 +++ Models/QAAnswer.php | 2 +- Models/QAAnswerMapper.php | 34 +++++++++---------- Models/QAAnswerVoteMapper.php | 31 ++++------------- Models/QAQuestion.php | 4 +-- Models/QAQuestionMapper.php | 34 +++++++++---------- Models/QAQuestionVoteMapper.php | 31 ++++------------- tests/Models/QAAnswerVoteMapperTest.php | 9 ++++- tests/Models/QAQuestionVoteMapperTest.php | 8 ++++- 11 files changed, 91 insertions(+), 92 deletions(-) diff --git a/Admin/Install/Application/QA/Themes/Default/tpl/footer.tpl.php b/Admin/Install/Application/QA/Themes/Default/tpl/footer.tpl.php index 128005a..060e45f 100755 --- a/Admin/Install/Application/QA/Themes/Default/tpl/footer.tpl.php +++ b/Admin/Install/Application/QA/Themes/Default/tpl/footer.tpl.php @@ -1,4 +1,16 @@ -name = (string) $request->getData('title'); $question->questionRaw = (string) $request->getData('plain'); @@ -442,7 +440,10 @@ final class ApiController extends Controller return; } - $questionVote = QAQuestionVoteMapper::findVote((int) $request->getData('id'), $request->header->account); + $questionVote = QAQuestionVoteMapper::get() + ->where('question', (int) $request->getData('id')) + ->where('createdBy', $request->header->account) + ->execute(); if ($questionVote === false || $questionVote instanceof NullQAQuestionVote || $questionVote === null) { $new = new QAQuestionVote(); @@ -505,7 +506,10 @@ final class ApiController extends Controller return; } - $answerVote = QAAnswerVoteMapper::findVote((int) $request->getData('id'), $request->header->account); + $answerVote = QAAnswerVoteMapper::get() + ->where('answer', (int) $request->getData('id')) + ->where('createdBy', $request->header->account) + ->execute(); if ($answerVote === false || $answerVote instanceof NullQAAnswerVote || $answerVote === null) { $new = new QAAnswerVote(); diff --git a/Controller/BackendController.php b/Controller/BackendController.php index a6110de..ac0960e 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -77,11 +77,13 @@ final class BackendController extends Controller ->with('createdBy/account') ->with('votes') ->with('answers') + ->with('answers/votes') ->with('tags') ->with('tags/title') ->where('tags/title/language', $response->getLanguage()) ->where('language', $response->getLanguage()) ->limit(50)->execute(); + $view->setData('questions', $list); $apps = QAAppMapper::getAll()->execute(); @@ -112,6 +114,7 @@ final class BackendController extends Controller ->with('answers') ->with('answers/createdBy') ->with('answers/createdBy/account') + ->with('answers/votes') ->with('createdBy') ->with('createdBy/account') ->with('votes') @@ -121,6 +124,7 @@ final class BackendController extends Controller ->where('id', (int) $request->getData('id')) ->where('tags/title/language', $response->getLanguage()) ->execute(); + $view->addData('question', $question); $scores = QAHelperMapper::getAccountScore($question->getAccounts()); diff --git a/Models/QAAnswer.php b/Models/QAAnswer.php index 080d316..63f606b 100755 --- a/Models/QAAnswer.php +++ b/Models/QAAnswer.php @@ -198,7 +198,7 @@ class QAAnswer implements \JsonSerializable /** * Get all votes * - * @return QAVnswerVote[] + * @return QAAnswerVote[] * * @since 1.0.0 */ diff --git a/Models/QAAnswerMapper.php b/Models/QAAnswerMapper.php index 80daebd..31eec01 100755 --- a/Models/QAAnswerMapper.php +++ b/Models/QAAnswerMapper.php @@ -35,14 +35,14 @@ final class QAAnswerMapper extends DataMapperFactory * @since 1.0.0 */ public const COLUMNS = [ - 'qa_answer_id' => ['name' => 'qa_answer_id', 'type' => 'int', 'internal' => 'id'], - 'qa_answer_answer_raw' => ['name' => 'qa_answer_answer_raw', 'type' => 'string', 'internal' => 'answerRaw'], - 'qa_answer_answer' => ['name' => 'qa_answer_answer', 'type' => 'string', 'internal' => 'answer'], - 'qa_answer_question' => ['name' => 'qa_answer_question', 'type' => 'int', 'internal' => 'question'], - 'qa_answer_status' => ['name' => 'qa_answer_status', 'type' => 'int', 'internal' => 'status'], - 'qa_answer_accepted' => ['name' => 'qa_answer_accepted', 'type' => 'bool', 'internal' => 'isAccepted'], - 'qa_answer_created_by' => ['name' => 'qa_answer_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], - 'qa_answer_created_at' => ['name' => 'qa_answer_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], + 'qa_answer_id' => ['name' => 'qa_answer_id', 'type' => 'int', 'internal' => 'id'], + 'qa_answer_answer_raw' => ['name' => 'qa_answer_answer_raw', 'type' => 'string', 'internal' => 'answerRaw'], + 'qa_answer_answer' => ['name' => 'qa_answer_answer', 'type' => 'string', 'internal' => 'answer'], + 'qa_answer_question' => ['name' => 'qa_answer_question', 'type' => 'int', 'internal' => 'question'], + 'qa_answer_status' => ['name' => 'qa_answer_status', 'type' => 'int', 'internal' => 'status'], + 'qa_answer_accepted' => ['name' => 'qa_answer_accepted', 'type' => 'bool', 'internal' => 'isAccepted'], + 'qa_answer_created_by' => ['name' => 'qa_answer_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], + 'qa_answer_created_at' => ['name' => 'qa_answer_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], ]; /** @@ -53,13 +53,13 @@ final class QAAnswerMapper extends DataMapperFactory */ public const BELONGS_TO = [ 'createdBy' => [ - 'mapper' => ProfileMapper::class, - 'external' => 'qa_answer_created_by', - 'by' => 'account', + 'mapper' => ProfileMapper::class, + 'external' => 'qa_answer_created_by', + 'by' => 'account', ], 'question' => [ - 'mapper' => QAQuestionMapper::class, - 'external' => 'qa_answer_question', + 'mapper' => QAQuestionMapper::class, + 'external' => 'qa_answer_question', ], ]; @@ -71,10 +71,10 @@ final class QAAnswerMapper extends DataMapperFactory */ public const HAS_MANY = [ 'votes' => [ - 'mapper' => QAAnswerVoteMapper::class, - 'table' => 'qa_answer_vote', - 'self' => 'qa_answer_vote_answer', - 'external' => null, + 'mapper' => QAAnswerVoteMapper::class, + 'table' => 'qa_answer_vote', + 'self' => 'qa_answer_vote_answer', + 'external' => null, ], 'media' => [ 'mapper' => MediaMapper::class, diff --git a/Models/QAAnswerVoteMapper.php b/Models/QAAnswerVoteMapper.php index 84b0134..d61ecb3 100755 --- a/Models/QAAnswerVoteMapper.php +++ b/Models/QAAnswerVoteMapper.php @@ -34,11 +34,11 @@ final class QAAnswerVoteMapper extends DataMapperFactory * @since 1.0.0 */ public const COLUMNS = [ - 'qa_answer_vote_id' => ['name' => 'qa_answer_vote_id', 'type' => 'int', 'internal' => 'id'], - 'qa_answer_vote_score' => ['name' => 'qa_answer_vote_score', 'type' => 'int', 'internal' => 'score'], - 'qa_answer_vote_answer' => ['name' => 'qa_answer_vote_answer', 'type' => 'int', 'internal' => 'answer', 'readonly' => true], - 'qa_answer_vote_created_by' => ['name' => 'qa_answer_vote_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], - 'qa_answer_vote_created_at' => ['name' => 'qa_answer_vote_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], + 'qa_answer_vote_id' => ['name' => 'qa_answer_vote_id', 'type' => 'int', 'internal' => 'id'], + 'qa_answer_vote_score' => ['name' => 'qa_answer_vote_score', 'type' => 'int', 'internal' => 'score'], + 'qa_answer_vote_answer' => ['name' => 'qa_answer_vote_answer', 'type' => 'int', 'internal' => 'answer', 'readonly' => true], + 'qa_answer_vote_created_by' => ['name' => 'qa_answer_vote_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], + 'qa_answer_vote_created_at' => ['name' => 'qa_answer_vote_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], ]; /** @@ -49,8 +49,8 @@ final class QAAnswerVoteMapper extends DataMapperFactory */ public const BELONGS_TO = [ 'createdBy' => [ - 'mapper' => AccountMapper::class, - 'external' => 'qa_answer_vote_created_by', + 'mapper' => AccountMapper::class, + 'external' => 'qa_answer_vote_created_by', ], ]; @@ -77,21 +77,4 @@ final class QAAnswerVoteMapper extends DataMapperFactory * @since 1.0.0 */ public const PRIMARYFIELD ='qa_answer_vote_id'; - - /** - * Find vote for answer from user - * - * @param int $answer Answer id - * @param int $account Account id - * - * @return bool|QAAnswerVote - * - * @since 1.0.0 - */ - public static function findVote(int $answer, int $account) : bool | QAAnswerVote - { - $results = self::getAll()->where('comment', $answer)->where('createdBy', $account)->execute(); - - return empty($results) ? new NullQAAnswerVote() : \reset($results); - } } diff --git a/Models/QAQuestion.php b/Models/QAQuestion.php index 058fb1e..b89f26c 100755 --- a/Models/QAQuestion.php +++ b/Models/QAQuestion.php @@ -98,7 +98,7 @@ class QAQuestion implements \JsonSerializable /** * Tags. * - * @var array + * @var array * @since 1.0.0 */ private array $tags = []; @@ -343,7 +343,7 @@ class QAQuestion implements \JsonSerializable /** * Get all votes * - * @return QAVnswerVote[] + * @return QAQuestionVote[] * * @since 1.0.0 */ diff --git a/Models/QAQuestionMapper.php b/Models/QAQuestionMapper.php index 1a3eb86..13483a1 100755 --- a/Models/QAQuestionMapper.php +++ b/Models/QAQuestionMapper.php @@ -36,15 +36,15 @@ final class QAQuestionMapper extends DataMapperFactory * @since 1.0.0 */ public const COLUMNS = [ - 'qa_question_id' => ['name' => 'qa_question_id', 'type' => 'int', 'internal' => 'id'], - 'qa_question_title' => ['name' => 'qa_question_title', 'type' => 'string', 'internal' => 'name'], - 'qa_question_language' => ['name' => 'qa_question_language', 'type' => 'string', 'internal' => 'language'], - 'qa_question_question' => ['name' => 'qa_question_question', 'type' => 'string', 'internal' => 'question'], - 'qa_question_question_raw' => ['name' => 'qa_question_question_raw', 'type' => 'string', 'internal' => 'questionRaw'], - 'qa_question_status' => ['name' => 'qa_question_status', 'type' => 'int', 'internal' => 'status'], - 'qa_question_created_by' => ['name' => 'qa_question_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], - 'qa_question_created_at' => ['name' => 'qa_question_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], - 'qa_question_app' => ['name' => 'qa_question_app', 'type' => 'int', 'internal' => 'app'], + 'qa_question_id' => ['name' => 'qa_question_id', 'type' => 'int', 'internal' => 'id'], + 'qa_question_title' => ['name' => 'qa_question_title', 'type' => 'string', 'internal' => 'name'], + 'qa_question_language' => ['name' => 'qa_question_language', 'type' => 'string', 'internal' => 'language'], + 'qa_question_question' => ['name' => 'qa_question_question', 'type' => 'string', 'internal' => 'question'], + 'qa_question_question_raw' => ['name' => 'qa_question_question_raw', 'type' => 'string', 'internal' => 'questionRaw'], + 'qa_question_status' => ['name' => 'qa_question_status', 'type' => 'int', 'internal' => 'status'], + 'qa_question_created_by' => ['name' => 'qa_question_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], + 'qa_question_created_at' => ['name' => 'qa_question_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], + 'qa_question_app' => ['name' => 'qa_question_app', 'type' => 'int', 'internal' => 'app'], ]; /** @@ -61,16 +61,16 @@ final class QAQuestionMapper extends DataMapperFactory 'external' => 'qa_tag_src', ], 'answers' => [ - 'mapper' => QAAnswerMapper::class, - 'table' => 'qa_answer', - 'self' => 'qa_answer_question', - 'external' => null, + 'mapper' => QAAnswerMapper::class, + 'table' => 'qa_answer', + 'self' => 'qa_answer_question', + 'external' => null, ], 'votes' => [ - 'mapper' => QAQuestionVoteMapper::class, - 'table' => 'qa_question_vote', - 'self' => 'qa_question_vote_question', - 'external' => null, + 'mapper' => QAQuestionVoteMapper::class, + 'table' => 'qa_question_vote', + 'self' => 'qa_question_vote_question', + 'external' => null, ], 'media' => [ 'mapper' => MediaMapper::class, diff --git a/Models/QAQuestionVoteMapper.php b/Models/QAQuestionVoteMapper.php index 0291b9b..54955bd 100755 --- a/Models/QAQuestionVoteMapper.php +++ b/Models/QAQuestionVoteMapper.php @@ -34,11 +34,11 @@ final class QAQuestionVoteMapper extends DataMapperFactory * @since 1.0.0 */ public const COLUMNS = [ - 'qa_question_vote_id' => ['name' => 'qa_question_vote_id', 'type' => 'int', 'internal' => 'id'], - 'qa_question_vote_score' => ['name' => 'qa_question_vote_score', 'type' => 'int', 'internal' => 'score'], - 'qa_question_vote_question' => ['name' => 'qa_question_vote_question', 'type' => 'int', 'internal' => 'question', 'readonly' => true], - 'qa_question_vote_created_by' => ['name' => 'qa_question_vote_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], - 'qa_question_vote_created_at' => ['name' => 'qa_question_vote_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], + 'qa_question_vote_id' => ['name' => 'qa_question_vote_id', 'type' => 'int', 'internal' => 'id'], + 'qa_question_vote_score' => ['name' => 'qa_question_vote_score', 'type' => 'int', 'internal' => 'score'], + 'qa_question_vote_question' => ['name' => 'qa_question_vote_question', 'type' => 'int', 'internal' => 'question', 'readonly' => true], + 'qa_question_vote_created_by' => ['name' => 'qa_question_vote_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], + 'qa_question_vote_created_at' => ['name' => 'qa_question_vote_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], ]; /** @@ -49,8 +49,8 @@ final class QAQuestionVoteMapper extends DataMapperFactory */ public const BELONGS_TO = [ 'createdBy' => [ - 'mapper' => AccountMapper::class, - 'external' => 'qa_question_vote_created_by', + 'mapper' => AccountMapper::class, + 'external' => 'qa_question_vote_created_by', ], ]; @@ -77,21 +77,4 @@ final class QAQuestionVoteMapper extends DataMapperFactory * @since 1.0.0 */ public const PRIMARYFIELD ='qa_question_vote_id'; - - /** - * Find vote for question from user - * - * @param int $question Question id - * @param int $account Account id - * - * @return bool|QAQuestionVote - * - * @since 1.0.0 - */ - public static function findVote(int $question, int $account) : bool | QAQuestionVote - { - $results = self::getAll()->where('comment', $question)->where('createdBy', $account)->execute(); - - return empty($results) ? new NullQAQuestionVote() : \reset($results); - } } diff --git a/tests/Models/QAAnswerVoteMapperTest.php b/tests/Models/QAAnswerVoteMapperTest.php index ba6dcc8..3a3d61a 100644 --- a/tests/Models/QAAnswerVoteMapperTest.php +++ b/tests/Models/QAAnswerVoteMapperTest.php @@ -43,6 +43,13 @@ final class QAAnswerVoteMapperTest extends \PHPUnit\Framework\TestCase self::assertEquals($vote->answer, $voteR->answer); self::assertEquals($vote->score, $voteR->score); - self::assertEquals(1, QAAnswerVoteMapper::findVote(1, 1)->getId()); + self::assertEquals(1, + QAAnswerVoteMapper::get() + ->where('answer', 1) + ->where('createdBy', 1) + ->limit(1) + ->execute() + ->getId() + ); } } diff --git a/tests/Models/QAQuestionVoteMapperTest.php b/tests/Models/QAQuestionVoteMapperTest.php index 5e142ff..545684e 100644 --- a/tests/Models/QAQuestionVoteMapperTest.php +++ b/tests/Models/QAQuestionVoteMapperTest.php @@ -43,6 +43,12 @@ final class QAQuestionVoteMapperTest extends \PHPUnit\Framework\TestCase self::assertEquals($vote->question, $voteR->question); self::assertEquals($vote->score, $voteR->score); - self::assertEquals(1, QAQuestionVoteMapper::findVote(1, 1)->getId()); + self::assertEquals(1, + QAQuestionVoteMapper::get() + ->where('question', 1) + ->where('createdBy', 1) + ->limit(1) + ->execute()->getId() + ); } }