mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
[api] reward + thanks
This commit is contained in:
@@ -4,10 +4,20 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\CommentResource;
|
||||
use App\Models\Comment;
|
||||
use App\Repositories\CommentRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CommentController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(CommentRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@@ -20,7 +30,6 @@ class CommentController extends Controller
|
||||
$comments = Comment::query()
|
||||
->with($with)
|
||||
->where('torrent', $torrentId)
|
||||
->whereHas('create_user')
|
||||
->paginate();
|
||||
$resource = CommentResource::collection($comments);
|
||||
$resource->additional([
|
||||
@@ -30,6 +39,35 @@ class CommentController extends Controller
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
private function prepareData(Request $request)
|
||||
{
|
||||
$allTypes = array_keys(Comment::TYPE_MAPS);
|
||||
$request->validate([
|
||||
'type' => ['required', Rule::in($allTypes)],
|
||||
'torrent_id' => 'nullable|integer',
|
||||
'text' => 'required',
|
||||
'offer_id' => 'nullable|integer',
|
||||
'request_id' => 'nullable|integer',
|
||||
'anonymous' => 'nullable',
|
||||
]);
|
||||
$data = [
|
||||
'type' => $request->type,
|
||||
'torrent' => $request->torrent_id,
|
||||
'text' => $request->text,
|
||||
'ori_text' => $request->text,
|
||||
'offer' => $request->offer_id,
|
||||
'request' => $request->request_id,
|
||||
'anonymous' => $request->anonymous,
|
||||
];
|
||||
$data = array_filter($data);
|
||||
foreach ($allTypes as $type) {
|
||||
if ($data['type'] == $type && empty($data[$type . "_id"])) {
|
||||
throw new \InvalidArgumentException("require {$type}_id");
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
@@ -38,7 +76,10 @@ class CommentController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
$user = Auth::user();
|
||||
$comment = $this->repository->store($this->prepareData($request), $user);
|
||||
$resource = new CommentResource($comment);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,11 +18,14 @@ class MessageController extends Controller
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$messages = Message::query()
|
||||
->where('receiver', $user->id)
|
||||
$query = $user->receive_messages()
|
||||
->with(['send_user'])
|
||||
->orderBy('id', 'desc')
|
||||
->paginate();
|
||||
->orderBy('id', 'desc');
|
||||
|
||||
if ($request->unread) {
|
||||
$query->where('unread', 'yes');
|
||||
}
|
||||
$messages = $query->paginate();
|
||||
$resource = MessageResource::collection($messages);
|
||||
return $this->success($resource);
|
||||
|
||||
@@ -49,6 +52,9 @@ class MessageController extends Controller
|
||||
{
|
||||
$message = Message::query()->with(['send_user'])->findOrFail($id);
|
||||
$resource = new MessageResource($message);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('message.show.page_title'),
|
||||
]);
|
||||
|
||||
return $this->success($resource);
|
||||
}
|
||||
@@ -75,4 +81,27 @@ class MessageController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function listUnread(Request $request): array
|
||||
{
|
||||
$user = Auth::user();
|
||||
$query = $user->receive_messages()
|
||||
->with(['send_user'])
|
||||
->orderBy('id', 'desc')
|
||||
->where('unread', 'yes');
|
||||
|
||||
$messages = $query->paginate();
|
||||
$resource = MessageResource::collection($messages);
|
||||
$resource->additional([
|
||||
'site_info' => site_info(),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
public function countUnread()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$count = $user->receive_messages()->where('unread', 'yes')->count();
|
||||
return $this->success(['unread' => $count]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,4 +93,20 @@ class NewsController extends Controller
|
||||
$result = $this->repository->delete($id);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo update the unread cache
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function latest()
|
||||
{
|
||||
$result = News::query()->orderBy('id', 'desc')->first();
|
||||
$resource = new NewsResource($result);
|
||||
$resource->additional([
|
||||
'site_info' => site_info(),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\PollResource;
|
||||
use App\Models\Poll;
|
||||
use App\Repositories\PollRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class PollController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private function getRules(): array
|
||||
{
|
||||
return [
|
||||
'family_id' => 'required|numeric',
|
||||
'name' => 'required|string',
|
||||
'peer_id' => 'required|string',
|
||||
'agent' => 'required|string',
|
||||
'comment' => 'required|string',
|
||||
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = PollResource::collection($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate($this->getRules());
|
||||
$result = $this->repository->store($request->all());
|
||||
$resource = new PollResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$result = Poll::query()->findOrFail($id);
|
||||
$resource = new PollResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->validate($this->getRules());
|
||||
$result = $this->repository->update($request->all(), $id);
|
||||
$resource = new PollResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$result = $this->repository->delete($id);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo 弃权选项
|
||||
* @return array
|
||||
*/
|
||||
public function latest()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$poll = Poll::query()->orderBy('id', 'desc')->first();
|
||||
$selection = null;
|
||||
$answerStats = [];
|
||||
if ($poll) {
|
||||
$baseAnswerQuery = $poll->answers()->where('selection', '<=', Poll::MAX_OPTION_INDEX);
|
||||
$poll->answers_count = (clone $baseAnswerQuery)->count();
|
||||
$answer = $poll->answers()->where('userid', $user->id)->first();
|
||||
$options = [];
|
||||
for ($i = 0; $i <= Poll::MAX_OPTION_INDEX; $i++) {
|
||||
$field = "option{$i}";
|
||||
$value = $poll->{$field};
|
||||
if ($value !== '') {
|
||||
$options[$i] = $value;
|
||||
}
|
||||
}
|
||||
if ($answer) {
|
||||
$selection = $answer->selection;
|
||||
} else {
|
||||
$options["255"] = "弃权(我想偷看结果!)";
|
||||
}
|
||||
$poll->options = $options;
|
||||
|
||||
$answerStats = (clone $baseAnswerQuery)
|
||||
->selectRaw("selection, count(*) as count")->groupBy("selection")
|
||||
->get()->pluck('count', 'selection')->toArray();
|
||||
foreach ($answerStats as $index => &$value) {
|
||||
$value = number_format(($value / $poll->answers_count) * 100, 2) . '%';
|
||||
}
|
||||
}
|
||||
|
||||
$resource = new PollResource($poll);
|
||||
$resource->additional([
|
||||
'selection' => $selection,
|
||||
'answer_stats' => $answerStats,
|
||||
'site_info' => site_info(),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
public function vote(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'poll_id' => 'required',
|
||||
'selection' => 'required|integer|min:0|max:255',
|
||||
]);
|
||||
$pollId = $request->poll_id;
|
||||
$selection = $request->selection;
|
||||
$user = Auth::user();
|
||||
$poll = Poll::query()->findOrFail($pollId);
|
||||
$data = [
|
||||
'userid' => $user->id,
|
||||
'selection' => $selection,
|
||||
];
|
||||
$answer = $poll->answers()->create($data);
|
||||
return $this->success($answer->toArray());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\RewardResource;
|
||||
use App\Http\Resources\PeerResource;
|
||||
use App\Http\Resources\SnatchResource;
|
||||
use App\Models\Peer;
|
||||
use App\Models\Snatch;
|
||||
use App\Repositories\RewardRepository;
|
||||
use App\Repositories\TorrentRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class RewardController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(RewardRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'torrent_id' => 'required',
|
||||
]);
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = RewardResource::collection($result);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('reward.index.page_title'),
|
||||
]);
|
||||
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'torrent_id' => 'required',
|
||||
'value' => 'required',
|
||||
]);
|
||||
$result = $this->repository->store($request->torrent_id, $request->value, Auth::user());
|
||||
$resource = new RewardResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\ThankResource;
|
||||
use App\Models\Thank;
|
||||
use App\Models\Torrent;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ThankController extends Controller
|
||||
{
|
||||
@@ -37,7 +39,17 @@ class ThankController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
$request->validate(['torrent_id' => 'required']);
|
||||
$torrentId = $request->torrent_id;
|
||||
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
|
||||
$torrent->checkIsNormal();
|
||||
$user = Auth::user();
|
||||
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]);
|
||||
$resource = new ThankResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\RewardResource;
|
||||
use App\Http\Resources\TorrentResource;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use App\Repositories\TorrentRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -51,16 +53,19 @@ class TorrentController extends Controller
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
|
||||
$result = $this->repository->getDetail($id, Auth::user());
|
||||
|
||||
$isBookmarked = Auth::user()->bookmarks()->where('torrentid', $id)->exists();
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
$user = Auth::user();
|
||||
$result = $this->repository->getDetail($id, $user);
|
||||
$isBookmarked = $user->bookmarks()->where('torrentid', $id)->exists();
|
||||
|
||||
$resource = new TorrentResource($result);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('torrent.show.page_title'),
|
||||
'field_labels' => Torrent::getFieldLabels(),
|
||||
'is_bookmarked' => (int)$isBookmarked,
|
||||
'bonus_reward_values' => Torrent::BONUS_REWARD_VALUES,
|
||||
]);
|
||||
|
||||
return $this->success($resource);
|
||||
|
||||
@@ -19,6 +19,7 @@ class MessageResource extends JsonResource
|
||||
'subject' => $this->subject,
|
||||
'msg' => strip_all_tags($this->msg),
|
||||
'added_human' => $this->added->diffForHumans(),
|
||||
'added' => format_datetime($this->added),
|
||||
'send_user' => new UserResource($this->whenLoaded('send_user')),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -14,11 +14,13 @@ class NewsResource extends JsonResource
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$descriptionArr = format_description($this->body);
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'body' => $this->body,
|
||||
'added' => $this->added,
|
||||
'body' => $descriptionArr,
|
||||
'images' => get_image_from_description($descriptionArr),
|
||||
'added' => format_datetime($this->added, 'Y.m.d'),
|
||||
'user' => new UserResource($this->whenLoaded('user'))
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class PollResource extends JsonResource
|
||||
{
|
||||
public $preserveKeys = true;
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$out = [
|
||||
'id' => $this->id,
|
||||
'added' => format_datetime($this->added),
|
||||
'question' => $this->question,
|
||||
'answers_count' => $this->answers_count,
|
||||
'options' => $this->options,
|
||||
];
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class RewardResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'user_id' => $this->userid,
|
||||
'torrent_id' => $this->torrentid,
|
||||
'value' => $this->value,
|
||||
'created_at' => format_datetime($this->created_at),
|
||||
'updated_at' => format_datetime($this->updated_at),
|
||||
'user' => new UserResource($this->whenLoaded('user'))
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ class ThankResource extends JsonResource
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'torrent_id' => $this->torrentid,
|
||||
'user_id' => $this->userid,
|
||||
'user' => new UserResource($this->whenLoaded('user')),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ class TorrentResource extends JsonResource
|
||||
'user' => new UserResource($this->whenLoaded('user')),
|
||||
'basic_category' => new CategoryResource($this->whenLoaded('basic_category')),
|
||||
'tags' => TagResource::collection($this->whenLoaded('tags')),
|
||||
'thanks' => ThankResource::collection($this->whenLoaded('thanks')),
|
||||
'reward_logs' => RewardResource::collection($this->whenLoaded('reward_logs')),
|
||||
];
|
||||
$descriptionArr = format_description($this->descr);
|
||||
$out['cover'] = get_image_from_description($descriptionArr, true);
|
||||
@@ -63,6 +65,7 @@ class TorrentResource extends JsonResource
|
||||
|
||||
$out['thank_users_count'] = $this->thank_users_count;
|
||||
$out['peers_count'] = $this->peers_count;
|
||||
$out['reward_logs_count'] = $this->reward_logs_count;
|
||||
}
|
||||
// $out['upload_peers_count'] = $this->upload_peers_count;
|
||||
// $out['download_peers_count'] = $this->download_peers_count;
|
||||
|
||||
Reference in New Issue
Block a user