Files
core/hwe/sammo/API/Vote/GetVoteDetail.php
T
Hide_D 78a55fb87a refac: 변경한 DTO에 따라 코드 수정
- Spatie/DataTransferObject 대비
  - MapFrom,MapTo -> RawName
  - Strict -> 항상
     - 심지어 raw type도 더 까다롭게
  - array에 MapConverter 적용
  - NullIsUndefined 적용
  - 빈 nullable arg자리에 null 강제 지정
- Vue3에 v-model.number 지정
  - input type="number" 기준
2022-05-22 22:39:28 +09:00

75 lines
1.9 KiB
PHP

<?php
namespace sammo\API\Vote;
use sammo\Session;
use DateTimeInterface;
use sammo\DB;
use sammo\DTO\VoteComment;
use sammo\DTO\VoteInfo;
use sammo\Json;
use sammo\KVStorage;
use sammo\Validator;
class GetVoteDetail extends \sammo\BaseAPI
{
public function validateArgs(): ?string
{
$v = new Validator($this->args);
$v->rule('required', 'voteID')
->rule('integer', 'voteID');
if (!$v->validate()) {
return $v->errorStr();
}
$this->args['voteID'] = (int)$this->args['voteID'];
return null;
}
public function getRequiredSessionMode(): int
{
return static::REQ_LOGIN | static::REQ_READ_ONLY;
}
public function launch(Session $session, ?DateTimeInterface $modifiedSince, ?string $reqEtag)
{
$voteID = $this->args['voteID'];
$db = DB::db();
$voteStor = KVStorage::getStorage($db, 'vote');
$rawVote = $voteStor->getValue("vote_{$voteID}");
if (!$rawVote) {
return '설문조사가 없습니다.';
}
$voteInfo = VoteInfo::fromArray($rawVote);
$votes = array_map(fn ($arr) => [Json::decode($arr[0]), $arr[1]], $db->queryAllLists(
'SELECT selection, count(*) AS cnt FROM vote WHERE vote_id = %i GROUP BY selection',
$voteID
));
$comments = array_map(fn ($arr) => VoteComment::fromArray($arr), $db->query('SELECT * FROM vote_comment WHERE vote_id = %i ORDER BY `id` ASC', $voteID));
$myVote = null;
if ($session->isGameLoggedIn()) {
$generalID = $session->generalID;
$rawMyVote = $db->queryFirstField('SELECT selection FROM vote WHERE vote_id = %i AND general_id = %i', $voteID, $generalID);
if ($rawMyVote) {
$myVote = Json::decode($rawMyVote);
}
}
$userCnt = $db->queryFirstField('SELECT count(*) FROM general WHERE npc < 2');
return [
'result' => true,
'voteInfo' => $voteInfo,
'votes' => $votes,
'comments' => $comments,
'myVote' => $myVote,
'userCnt' => $userCnt,
];
}
}