<commit_before><?php

namespace App\Http\Controllers;

use App\Election;
use App\Elector;
use App\Candidate;
use App\CandidateRank;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CandidateRankController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    public function index(Election $election, Candidate $candidate)
    {
        // TODO: authorize

        return $candidate->ranks;
    }

    public function store(Request $request, Election $election, Candidate $candidate)
    {
        // TODO: send batch of ranks
        
        // TODO: need to authorize?  Or just let it fail when we can't
        // find an elector with the current election ID and user ID?

        $user = Auth::user();
        $elector = Elector::where('election_id', '=', $election->id)->where('user_id', '=', $user->id)->firstOrFail();

        $rank = CandidateRank::firstOrNew(['elector_id' => $elector->id, 'candidate_id' => $candidate->id]);
        $rank->elector_id = $elector->id;
        $rank->candidate_id = $candidate->id;
        $rank->rank = $request->json()->get('rank');
        $rank->save();

        return redirect()->route(
            'election.candidate.rank.show',
            [
                'election' => $election,
                'candidate' => $candidate,
                'rank' => $rank
            ]
        );
    }

    public function show(Election $election, Candidate $candidate, CandidateRank $rank)
    {
        // TODO: authorize

        return $rank;
    }

    public function batchvote(Request $request, $election_id)
    {
        $user = Auth::user();
        $elector = Elector::where('election_id', '=', $election_id)->where('user_id', '=', $user->id)->firstOrFail();
        $election = Election::where('id', '=', $election_id)->firstOrFail();

        $ranks = array();
        
        // iterate over list of rankings
        foreach($request->json()->get('votes') as $vote) {
            // TODO: check this is a valid candidate?
            $candidate_id = $vote['candidate_id'];
            $rank_num = $vote['rank'];

            $rank = CandidateRank::firstOrNew(['elector_id' => $elector->id, 'candidate_id' => $candidate_id]);
            $rank->elector_id = $elector->id;
            $rank->candidate_id = $candidate_id;
            $rank->rank = $rank_num;
            $rank->save();

            array_push($ranks, $rank);
        }
        
        return $rank;
    }
}<commit_msg>minor batchvote fix<commit_after><?php

namespace App\Http\Controllers;

use App\Election;
use App\Elector;
use App\Candidate;
use App\CandidateRank;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CandidateRankController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api');
    }

    public function index(Election $election, Candidate $candidate)
    {
        // TODO: authorize

        return $candidate->ranks;
    }

    public function store(Request $request, Election $election, Candidate $candidate)
    {
        // TODO: send batch of ranks
        
        // TODO: need to authorize?  Or just let it fail when we can't
        // find an elector with the current election ID and user ID?

        $user = Auth::user();
        $elector = Elector::where('election_id', '=', $election->id)->where('user_id', '=', $user->id)->firstOrFail();

        $rank = CandidateRank::firstOrNew(['elector_id' => $elector->id, 'candidate_id' => $candidate->id]);
        $rank->elector_id = $elector->id;
        $rank->candidate_id = $candidate->id;
        $rank->rank = $request->json()->get('rank');
        $rank->save();

        return redirect()->route(
            'election.candidate.rank.show',
            [
                'election' => $election,
                'candidate' => $candidate,
                'rank' => $rank
            ]
        );
    }

    public function show(Election $election, Candidate $candidate, CandidateRank $rank)
    {
        // TODO: authorize

        return $rank;
    }

    public function batchvote(Request $request, $election_id)
    {
        $user = Auth::user();
        $elector = Elector::where('election_id', '=', $election_id)->where('user_id', '=', $user->id)->firstOrFail();
        $election = Election::where('id', '=', $election_id)->firstOrFail();

        $ranks = array();
        
        // iterate over list of rankings
        foreach($request->json()->get('votes') as $vote) {
            // TODO: check this is a valid candidate?
            $candidate_id = $vote['candidate_id'];
            $rank_num = $vote['rank'];

            $rank = CandidateRank::firstOrNew(['elector_id' => $elector->id, 'candidate_id' => $candidate_id]);
            $rank->elector_id = $elector->id;
            $rank->candidate_id = $candidate_id;
            $rank->rank = $rank_num;
            $rank->save();

            array_push($ranks, $rank);
        }
        
        return $ranks;
    }
}
{'fraction_non_alphanumeric': 0.15348007138607972, 'fraction_numerical': 0.0, 'mean_word_length': 2.303208906352325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, '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': '6021266', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1495, 'n_words': 320}