[api] reward + thanks

This commit is contained in:
xiaomlove
2022-03-30 15:37:11 +08:00
parent b613a46b8d
commit 3e4a5766c4
38 changed files with 983 additions and 64 deletions
+111
View File
@@ -0,0 +1,111 @@
<?php
namespace App\Repositories;
use App\Models\Comment;
use App\Models\Message;
use App\Models\NexusModel;
use App\Models\Setting;
use App\Models\Torrent;
use App\Models\User;
use Carbon\Carbon;
use Hamcrest\Core\Set;
use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
class CommentRepository extends BaseRepository
{
public function getList(array $params)
{
$query = Comment::query()->with(['create_user', 'update_user']);
if (!empty($params['torrent_id'])) {
$query->where('torrent', $params['torrent_id']);
}
if (!empty($params['offer_id'])) {
$query->where('offer', $params['offer_id']);
}
if (!empty($params['request_id'])) {
$query->where('request', $params['request_id']);
}
list($sortField, $sortType) = $this->getSortFieldAndType($params);
$query->orderBy($sortField, $sortType);
return $query->paginate();
}
public function store(array $params, User $user)
{
$type = $params['type'];
$modelName = Comment::TYPE_MAPS[$params['type']]['model'];
/**
* @var NexusModel $model
*/
$model = new $modelName;
$target = $model->newQuery()->with('user')->find($params[$type]);
return DB::transaction(function () use ($params, $user, $target) {
$comment = $user->comments()->create($params);
$commentCount = Comment::query()->type($params['type'])->count();
$target->comments = $commentCount;
$target->save();
$userUpdate = [
'seedbonus' => DB::raw('seedbonus + ' . Setting::get('bonus.addcomment')),
'last_comment' => Carbon::now(),
];
$user->update($userUpdate);
//message
if ($target->user->commentpm == 'yes' && $user->id != $target->user->id) {
$messageInfo = $this->getNoticeMessage($target, $params['type']);
$insert = [
'sender' => 0,
'receiver' => $target->user->id,
'subject' => $messageInfo['subject'],
'msg' => $messageInfo['body'],
'added' => Carbon::now()
];
Message::query()->insert($insert);
NexusDB::cache_del('user_'.$target->user->id.'_unread_message_count');
NexusDB::cache_del('user_'.$target->user->id.'_inbox_count');
}
return $comment;
});
}
public function update(array $params, $id)
{
$model = Comment::query()->findOrFail($id);
$model->update($params);
return $model;
}
public function getDetail($id)
{
$model = Comment::query()->findOrFail($id);
return $model;
}
public function delete($id)
{
$model = Comment::query()->findOrFail($id);
$result = $model->delete();
return $result;
}
private function getNoticeMessage($target, $type): array
{
$allTrans = require_once base_path('lang/_target/lang_comment.php');
$lang = $target->user->language->site_lang_folder ?? 'en';
$trans = $allTrans[$lang];
$subject = $trans['msg_new_comment'];
$targetScript = Comment::TYPE_MAPS[$type]['target_script'];
$targetNameField = Comment::TYPE_MAPS[$type]['target_name_field'];
$body = sprintf(
'%s [url="%s/%s"]%s[/url]',
$trans[$type]['msg_torrent_receive_comment'],
getSchemeAndHttpHost(),
sprintf($targetScript, $target->id),
$target->{$targetNameField}
);
return compact('subject', 'body');
}
}
+72
View File
@@ -0,0 +1,72 @@
<?php
namespace App\Repositories;
use App\Models\Poll;
use App\Models\Torrent;
use App\Models\User;
use Illuminate\Support\Facades\DB;
class PollRepository extends BaseRepository
{
public function getList(array $params)
{
$query = Poll::query();
list($sortField, $sortType) = $this->getSortFieldAndType($params);
$query->orderBy($sortField, $sortType);
return $query->paginate();
}
public function store($torrentId, $value, User $user)
{
if ($user->seedbonus < $value) {
throw new \LogicException("user bonus not enough.");
}
if ($user->reward_torrent_logs()->where('torrentid', $torrentId)->exists()) {
throw new \LogicException("user already reward this torrent.");
}
$torrent = Torrent::query()->findOrFail($torrentId, ['owner']);
$torrentOwner = User::query()->findOrFail($torrent->owner, ['id', 'seedbonus']);
return DB::transaction(function () use ($torrentId, $value, $user, $torrentOwner) {
$model = $user->reward_torrent_logs()->create([
'torrentid' => $torrentId,
'value' => $value,
]);
$affectedRows = $user->where('seedbonus', $user->seedbonus)->decrement('seedbonus', $value);
if ($affectedRows != 1) {
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
throw new \RuntimeException("decrement user bonus fail.");
}
$affectedRows = $torrentOwner->where('seedbonus', $torrentOwner->seedbonus)->increment('seedbonus', $value);
if ($affectedRows != 1) {
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
throw new \RuntimeException("increment owner bonus fail.");
}
return $model;
});
}
public function update(array $params, $id)
{
$model = Poll::query()->findOrFail($id);
$model->update($params);
return $model;
}
public function getDetail($id)
{
$model = Poll::query()->findOrFail($id);
return $model;
}
public function delete($id)
{
$model = Poll::query()->findOrFail($id);
$result = $model->delete();
return $result;
}
public function vote($selection, User $user)
{
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace App\Repositories;
use App\Models\Reward;
use App\Models\Torrent;
use App\Models\User;
use Google\Service\ToolResults\StepLabelsEntry;
use Illuminate\Support\Facades\DB;
class RewardRepository extends BaseRepository
{
public function getList(array $params)
{
$query = Reward::query()->with(['user']);
if (!empty($params['torrent_id'])) {
$query->where('torrentid', $params['torrent_id']);
}
list($sortField, $sortType) = $this->getSortFieldAndType($params);
$query->orderBy($sortField, $sortType);
return $query->paginate();
}
public function store($torrentId, $value, User $user)
{
if ($user->seedbonus < $value) {
throw new \LogicException("your bonus not enough.");
}
if ($user->reward_torrent_logs()->where('torrentid', $torrentId)->exists()) {
throw new \LogicException("you already reward this torrent.");
}
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
$torrent->checkIsNormal();
$torrentOwner = User::query()->findOrFail($torrent->owner, ['id', 'seedbonus']);
if ($user->id == $torrentOwner->id) {
throw new \LogicException("you can't reward to yourself.");
}
return DB::transaction(function () use ($torrentId, $value, $user, $torrentOwner) {
$model = $user->reward_torrent_logs()->create([
'torrentid' => $torrentId,
'value' => $value,
]);
$affectedRows = User::query()
->where('id', $user->id)
->where('seedbonus', $user->seedbonus)
->decrement('seedbonus', $value);
if ($affectedRows != 1) {
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
throw new \RuntimeException("decrement user bonus fail.");
}
$affectedRows = User::query()
->where('id', $torrentOwner->id)
->where('seedbonus', $torrentOwner->seedbonus)
->increment('seedbonus', $value);
if ($affectedRows != 1) {
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
throw new \RuntimeException("increment owner bonus fail.");
}
return $model;
});
}
public function update(array $params, $id)
{
$model = Reward::query()->findOrFail($id);
$model->update($params);
return $model;
}
public function getDetail($id)
{
$model = Reward::query()->findOrFail($id);
return $model;
}
public function delete($id)
{
$model = Reward::query()->findOrFail($id);
$result = $model->delete();
return $result;
}
}
+6 -2
View File
@@ -93,8 +93,12 @@ class TorrentRepository extends BaseRepository
public function getDetail($id, User $user)
{
$with = ['user', 'basic_audio_codec', 'basic_category', 'basic_codec', 'basic_media', 'basic_source', 'basic_standard', 'basic_team'];
$result = Torrent::query()->with($with)->withCount(['peers', 'thank_users'])->visible()->findOrFail($id);
$with = [
'user', 'basic_audio_codec', 'basic_category', 'basic_codec', 'basic_media', 'basic_source', 'basic_standard', 'basic_team',
'thanks' => function ($query) use ($user) {$query->where('userid', $user->id);},
'reward_logs' => function ($query) use ($user) {$query->where('userid', $user->id);},
];
$result = Torrent::query()->with($with)->withCount(['peers', 'thank_users', 'reward_logs'])->visible()->findOrFail($id);
$result->download_url = $this->getDownloadUrl($id, $user->toArray());
return $result;
}