pixelfed/app/Http/Controllers/LikeController.php

51 lines
1.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
2018-05-26 22:59:31 +00:00
use Auth, Hashids;
use App\{Like, Profile, Status, User};
use App\Jobs\LikePipeline\LikePipeline;
class LikeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
2018-05-26 22:59:31 +00:00
public function store(Request $request)
{
$this->validate($request, [
'item' => 'required|integer',
]);
$profile = Auth::user()->profile;
2018-06-04 02:46:55 +00:00
$status = Status::withCount('likes')->findOrFail($request->input('item'));
$count = $status->likes_count;
2018-05-26 22:59:31 +00:00
if($status->likes()->whereProfileId($profile->id)->count() !== 0) {
$like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail();
$like->delete();
2018-06-04 02:46:55 +00:00
$count--;
} else {
$like = new Like;
$like->profile_id = $profile->id;
$like->status_id = $status->id;
$like->save();
$count++;
2018-05-26 22:59:31 +00:00
}
LikePipeline::dispatch($like);
2018-06-04 02:46:55 +00:00
if($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Like saved', 'count' => $count];
} else {
$response = redirect($status->url());
}
return $response;
2018-05-26 22:59:31 +00:00
}
}