pixelfed/app/Http/Controllers/BookmarkController.php

47 lines
1.1 KiB
PHP
Raw Normal View History

2018-05-31 21:56:46 +00:00
<?php
namespace App\Http\Controllers;
2018-08-28 03:07:36 +00:00
use App\Bookmark;
use App\Status;
2018-05-31 21:56:46 +00:00
use Auth;
use Illuminate\Http\Request;
2022-01-29 01:27:53 +00:00
use App\Services\BookmarkService;
2018-05-31 21:56:46 +00:00
class BookmarkController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request)
{
$this->validate($request, [
2018-08-28 03:07:36 +00:00
'item' => 'required|integer|min:1',
2018-05-31 21:56:46 +00:00
]);
$profile = Auth::user()->profile;
$status = Status::findOrFail($request->input('item'));
$bookmark = Bookmark::firstOrCreate(
2018-08-05 19:18:33 +00:00
['status_id' => $status->id], ['profile_id' => $profile->id]
2018-05-31 21:56:46 +00:00
);
2018-08-28 03:07:36 +00:00
if (!$bookmark->wasRecentlyCreated) {
2022-01-29 01:27:53 +00:00
BookmarkService::del($profile->id, $status->id);
2018-08-05 19:18:33 +00:00
$bookmark->delete();
2022-01-29 01:27:53 +00:00
} else {
BookmarkService::add($profile->id, $status->id);
2018-08-05 19:18:33 +00:00
}
2018-08-28 03:07:36 +00:00
if ($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Bookmark saved!'];
} else {
$response = redirect()->back();
}
2018-05-31 21:56:46 +00:00
2018-08-28 03:07:36 +00:00
return $response;
}
2018-05-31 21:56:46 +00:00
}