kgadzhev
4 years ago

[Beginner] Logic and simplicity review of code

Posted 4 years ago by kgadzhev

Hi, this is my first application I am building with Laravel and am trying to adopt the best practice to work with the framework. Would appreciate it if you suggest me ways to improve the code below.

The code below is for vote buttons which increment and decrement on button click. I'm looking for insights about improving logic, architecture and the code itself.

This is in my routes.php file:

// Review votes...
Route::post('/votereview/{review_id}', 'VoteController@postVote');

This is my VoteController:

class VoteController extends Controller
{
    public function postVote(Votes $vote) {
        if (Auth::check()) {
            if (Request::ajax()) {
                
                if ($vote->approved == 0) {
                    $count = $this->addVote($vote);
                } else {
                    $count = $this->removeVote($vote);
                }
            }
        }
        return $count;
    }
    
    private function addVote($vote) {
        
        
        /*
         * Increments the current amount of votes for the particular review.
         */
        $review_id = $vote->review_id;
        $review = Review::find($review_id);
        $review->increment('score_cache', 1);
        
        /*
         * Makes the vote countable.
         */
        $vote->approved = 1;
        $vote->save();
        
        $count = $review->score_cache;
        
        return $count;
    }
    
    private function removeVote($vote) {
        
        /*
         * Decrements the current amount of votes for the particular review.
         */
        $review_id = $vote->review_id;
        $review = Review::find($review_id);
        $review->decrement('score_cache', 1);
        
        /*
         * Discards the vote.
         */
        $vote->approved = 0;
        $vote->save();
        
        $count = $review->score_cache;
        
        return $count;
    }
    
}

My Vote model:

class Vote extends Model
{
    protected $fillable = [
        
    ];
    
    protected $guarded = [
        'id'
    ];
    
    public function reviews() {
        $this->belongsTo('App\User');
    }
    
    public function user() {
        $this->belongsTo('App\User');
    }
    
    public function scopeCurrentUser() {
        $this->where('user_id', '=', Auth::user()->id);
    }
    
}

And this is in the route service provider:

$router->bind('review_id', function($review_id) {
           try {
                return \App\Vote::currentUser()->where('review_id', $review_id)->firstOrFail();
           }
           catch(ModelNotFoundException $e) {
                $vote = new \App\Vote;
                $vote->review_id = $review_id;
                $vote->user_id = \Auth::user()->id;
                $vote->approved = 0;
                $vote->save();
                
                return $vote;
           }

Please sign in or create an account to participate in this conversation.