ActivityController.php
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace App\Http\Container\AppSection\Controllers\User;
use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivityCreateFormEnum;
use App\Enums\ActivityStatusEnum;
use App\Helpers\JsonResource;
use App\Http\Request\BooleanStatusRequest;
use App\Models\Activity;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Support\Controller;
use Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ActivityController extends Controller
{
/**
* @return array
*/
public function getSelfActivityStatus(): array
{
return [
ActivityStatusEnum::PROCESS->value,
ActivityStatusEnum::UP->value,
ActivityStatusEnum::MATCH->value,
ActivityStatusEnum::FAIL->value,
ActivityStatusEnum::SEND->value
];
}
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request): JsonResponse
{
$size = $request->get('size', 10);
$filter = $request->mergeIfMissing([
'user_id' => Auth::id(),
'status' => $request->integer('user_id') === Auth::id() ? $this->getSelfActivityStatus() : ActivityStatusEnum::UP->value
])->except('page', 'size');
$data = Activity::filter($filter)
->select(['id', 'song_name', 'cover', 'user_id', 'status', 'publish_at'])
->selectRaw('if(u_top = 0, 0 , 1) as is_top')
->addSelect([
'is_collection' => UserActivityCollectionPivot::selectRaw('IF(COUNT(*) = 0, 0, 1)')->where('user_id', Auth::id())->whereColumn('activity_id', 'activitys.id'),
])
->with(['tags:id,name', 'user:id,nick_name,avatar'])
->where('created_form', ActivityCreateFormEnum::USER)
->latest('u_top')->latest('publish_at')
->paginate($size);
return JsonResource::success(JsonResource::SUCCESS, ['data' => $data->items(), 'count' => $data->total()]);
}
/**
* @param \App\Http\Request\BooleanStatusRequest $request
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
public function changeTop(BooleanStatusRequest $request, int $id): JsonResponse
{
$activity = Activity::query()
->where('user_id', Auth::id())
->where('created_form', ActivityCreateFormEnum::USER)
->find($id);
if ($activity?->getAttribute('user_id') !== Auth::id()) {
return JsonResource::fail(JsonResource::NO_PERMISSION);
}
$isTop = $request->integer('status') === 1;
if (
$isTop &&
Activity::query()->where('user_id', Auth::id())->where('created_form', ActivityCreateFormEnum::USER)
->where('song_type', $activity->getAttribute('song_type'))
->where("audit_status", ActivityAuditStatusEnum::SUCCESS)
->where('status', ActivityStatusEnum::UP)
->where('u_top', '!=', 0)->count() >= 3
) {
return JsonResource::fail('最多允许置顶3条内容');
}
$activity->update(['u_top' => $isTop ? now()->timestamp : 0]);
return JsonResource::success(JsonResource::OPERATION_SUCCESS);
}
}