JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "VoteController.php"

Full Path: /home/markqprx/iniasli.pro/common-20260222054425/Votes/VoteController.php
File size: 977 bytes
MIME-type: text/x-php
Charset: utf-8

<?php

namespace Common\Votes;

use Common\Core\BaseController;

class VoteController extends BaseController
{
    public function store()
    {
        $data = $this->validate(request(), [
            'vote_type' => 'required|in:upvote,downvote',
            'model_type' => 'required|string',
            'model_id' => 'required|integer',
        ]);

        $namespace = modelTypeToNamespace($data['model_type']);
        $model = app($namespace)::findOrFail($data['model_id']);

        $this->authorize('vote', $model);

        $model = app(StoreVote::class)->execute(
            $model,
            $data['vote_type'],
            getIp(),
        );

        $model->unsetRelation('votes');
        $model->load([
            'votes' => fn($builder) => $builder->withCurrentUserVotes(),
        ]);
        $model->current_vote = $model->votes->first()?->vote_type;
        $model->unsetRelation('votes');

        return $this->success(['model' => $model]);
    }
}