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