<commit_before><?php

namespace app\modules\rest\versions\v1\controllers;

use Yii;
use app\models\Poll;
use app\models\Code;
use app\models\Vote;
use app\components\controllers\VotingRestController;
use yii\helpers\ArrayHelper;

class VoteController extends VotingRestController
{
    public $modelClass = 'app\models\Vote';

    public function actionSubmit() {
        $token = Yii::$app->request->get('token'); // Better way to get this?
        $code = Code::findCodeByToken($token);
        if(!$code || !$code->isValid()) {
            return ['success' => false, 'error' => ['message' => 'Invalid voting code']];
        }
        else if ($code->isUsed()) {
            return ['success' => false, 'error' => ['message' => 'This voting code has already been used']];
        }
        $poll = $code->getPoll()->with('options')->one();
        $data = Yii::$app->request->getBodyParams();
        $optionIDs = $data['options'];
        if ($optionIDs === null || !is_array($optionIDs)) {
            return ['success' => false, 'error' => ['message' => 'Bad Request']];
        }

        if(count($optionIDs) < $poll->select_min) {
            return ['success' => false, 'error' => ['message' => 'Too few options selected']];
        }
        if(count($optionIDs) > $poll->select_max) {
            return ['success' => false, 'error' => ['message' => 'Too many options selected']];
        }

        $transaction = Yii::$app->db->beginTransaction();
        $vote = new Vote();
        $vote->code_id = $code->id;

        if(!$vote->save()) {
            return ['success' => false, 'error' => ['message' => 'Something went wrong']];
        }

        foreach($optionIDs as $optionId) {
            $option = $poll->getOptions()->where(['id' => $optionId])->one();
            if(!$option) {
                $transaction->rollBack();
                return ['success' => false, 'error' => ['message' => 'Invalid option']];
            }

            try {
                $vote->link('options', $option);
            } catch (Exception $e) {
                $transaction->rollBack();
                return ['success' => false, 'error' => ['message' => 'Something went wrong']];
            }
        }
        $transaction->commit();
        return ['success' => true, 'data' => $data];
    }
}
<commit_msg>Update code status on vote submit<commit_after><?php

namespace app\modules\rest\versions\v1\controllers;

use Yii;
use app\models\Poll;
use app\models\Code;
use app\models\Vote;
use app\components\controllers\VotingRestController;
use yii\helpers\ArrayHelper;

class VoteController extends VotingRestController
{
    public $modelClass = 'app\models\Vote';

    public function actionSubmit() {
        $token = Yii::$app->request->get('token'); // Better way to get this?
        $code = Code::findCodeByToken($token);
        if(!$code || !$code->isValid()) {
            return ['success' => false, 'error' => ['message' => 'Invalid voting code']];
        }
        else if ($code->isUsed()) {
            return ['success' => false, 'error' => ['message' => 'This voting code has already been used']];
        }
        $poll = $code->getPoll()->with('options')->one();
        $data = Yii::$app->request->getBodyParams();
        $optionIDs = $data['options'];
        if ($optionIDs === null || !is_array($optionIDs)) {
            return ['success' => false, 'error' => ['message' => 'Bad Request']];
        }

        if(count($optionIDs) < $poll->select_min) {
            return ['success' => false, 'error' => ['message' => 'Too few options selected']];
        }
        if(count($optionIDs) > $poll->select_max) {
            return ['success' => false, 'error' => ['message' => 'Too many options selected']];
        }

        $transaction = Yii::$app->db->beginTransaction();
        $vote = new Vote();
        $vote->code_id = $code->id;

        if(!$vote->save()) {
            return ['success' => false, 'error' => ['message' => 'Something went wrong']];
        }

        foreach($optionIDs as $optionId) {
            $option = $poll->getOptions()->where(['id' => $optionId])->one();
            if(!$option) {
                $transaction->rollBack();
                return ['success' => false, 'error' => ['message' => 'Invalid option']];
            }

            try {
                $vote->link('options', $option);
            } catch (Exception $e) {
                $transaction->rollBack();
                return ['success' => false, 'error' => ['message' => 'Something went wrong']];
            }
        }
        $code->code_status = Code::CODE_STATUS_USED;
        if (!$code->save()) {
            $transaction->rollBack();
            return ['success' => false, 'error' => ['message' => 'Something went wrong']];
        }
        $transaction->commit();
        return ['success' => true, 'data' => $data];
    }
}

{'fraction_non_alphanumeric': 0.18923895253682488, 'fraction_numerical': 0.0004091653027823241, 'mean_word_length': 2.3258503401360544, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5897035', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1386, 'n_words': 318}