bug fixes / dbmapper fixes

This commit is contained in:
Dennis Eichhorn 2021-12-19 20:20:39 +01:00
parent 1cb4cdf2be
commit f74e885a53
11 changed files with 91 additions and 92 deletions

View File

@ -1,4 +1,16 @@
<?php declare(strict_types=1);
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Template
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
use phpOMS\Uri\UriFactory;

View File

@ -143,8 +143,6 @@ final class ApiController extends Controller
*/
public function createQAQuestionFromRequest(RequestAbstract $request, ResponseAbstract $response, $data = null) : QAQuestion
{
$mardkownParser = new Markdown();
$question = new QAQuestion();
$question->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();

View File

@ -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());

View File

@ -198,7 +198,7 @@ class QAAnswer implements \JsonSerializable
/**
* Get all votes
*
* @return QAVnswerVote[]
* @return QAAnswerVote[]
*
* @since 1.0.0
*/

View File

@ -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,

View File

@ -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);
}
}

View File

@ -98,7 +98,7 @@ class QAQuestion implements \JsonSerializable
/**
* Tags.
*
* @var array<int, int|Tag>
* @var array<int, Tag>
* @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
*/

View File

@ -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,

View File

@ -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);
}
}

View File

@ -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()
);
}
}

View File

@ -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()
);
}
}