mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-18 15:50:50 +08:00
[api] add notifications
This commit is contained in:
@@ -3,10 +3,13 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\ThankResource;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Thank;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ThankController extends Controller
|
||||
{
|
||||
@@ -39,17 +42,48 @@ class ThankController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$request->validate(['torrent_id' => 'required']);
|
||||
$torrentId = $request->torrent_id;
|
||||
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
|
||||
$torrent->checkIsNormal();
|
||||
$user = Auth::user();
|
||||
$torrentOwner = User::query()->findOrFail($torrent->owner);
|
||||
if ($user->id == $torrentOwner->id) {
|
||||
throw new \LogicException("you can't thank to yourself");
|
||||
}
|
||||
$torrentOwner->checkIsNormal();
|
||||
if ($user->thank_torrent_logs()->where('torrentid', $torrentId)->exists()) {
|
||||
throw new \LogicException("you already thank this torrent");
|
||||
}
|
||||
$result = $user->thank_torrent_logs()->create(['torrentid' => $torrentId]);
|
||||
|
||||
$result = DB::transaction(function () use ($user, $torrentOwner, $torrent) {
|
||||
$thank = $user->thank_torrent_logs()->create(['torrentid' => $torrent->id]);
|
||||
$sayThanksBonus = Setting::get('bonus.saythanks');
|
||||
$receiveThanksBonus = Setting::get('bonus.receivethanks');
|
||||
if ($sayThanksBonus > 0) {
|
||||
$affectedRows = User::query()
|
||||
->where('id', $user->id)
|
||||
->where('seedbonus', $user->seedbonus)
|
||||
->increment('seedbonus', $sayThanksBonus);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("increment user bonus fail.");
|
||||
}
|
||||
}
|
||||
if ($receiveThanksBonus > 0) {
|
||||
$affectedRows = User::query()
|
||||
->where('id', $torrentOwner->id)
|
||||
->where('seedbonus', $torrentOwner->seedbonus)
|
||||
->increment('seedbonus', $receiveThanksBonus);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("increment owner bonus fail.");
|
||||
}
|
||||
}
|
||||
return $thank;
|
||||
});
|
||||
$resource = new ThankResource($result);
|
||||
return $this->success($resource);
|
||||
return $this->success($resource, '说谢谢成功!');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user