ShowController.php
3.65 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
95
<?php
namespace App\Http\Container\AppSection\Controllers\Activity;
use App\Enums\ActivityAuditStatusEnum;
use App\Enums\ActivityCreateFormEnum;
use App\Enums\ActivityWorkTypeEnum;
use App\Enums\UserScopeEnum;
use App\Helpers\ConfigHelper;
use App\Helpers\JsonResource;
use App\Models\Activity;
use App\Models\Pivots\UserActivityCollectionPivot;
use App\Models\Pivots\UserActivityViewPivot;
use App\Models\UserShare;
use App\Models\Views\ActivityWork;
use App\Support\Controller;
use App\Traits\UserTrait;
use Arr;
use Auth;
use Illuminate\Http\JsonResponse;
class ShowController extends Controller
{
use UserTrait;
/**
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke(int $id): JsonResponse
{
$this->user = Auth::user();
$activity = tap($this->getActivity($id), static fn($activity) => ConfigHelper::formatActivityDictionary($activity));
$isSystemUser = $this->isScopeEquals(UserScopeEnum::SYSTEM);
$isProjectManager = $this->isProjectManager($activity->getAttribute('project_id'));
$isDemoCreate = $activity->getAttribute('created_form') === ActivityCreateFormEnum::USER->value && Auth::id() === $activity->getAttribute('user_id');
return JsonResource::success(JsonResource::SUCCESS, Arr::except($activity->toArray(), ['expand', 'project_id', 'user_id', 'link_arranger']) + [
'is_collection' => (int)UserActivityCollectionPivot::query()->where('activity_id', $activity->getKey())->where('user_id', Auth::id())->exists(),
'is_can_share' => (int)($isSystemUser || $this->isScopeEquals(UserScopeEnum::PROJECT) || $isDemoCreate),
'is_can_download' => (int)($isSystemUser || $isProjectManager || $isDemoCreate || $this->hasSingPermission()),
'public_audio_count' => $this->getPublicAudioCount($activity->getKey()),
'share_count' => $this->getShareCount($activity->getKey()),
'listen_count' => $this->getListenCount($activity->getKey())
]);
}
/**
* @param int $activityId
* @return \App\Models\Activity
*/
public function getActivity(int $activityId): Activity
{
return Activity::query()
->with(['tags:id,name', 'project:id,name,cover', 'linkArranger:id,nick_name', 'user:id,nick_name,avatar'])
->where('audit_status', ActivityAuditStatusEnum::SUCCESS)
->findOrFail($activityId, [
'id', 'song_name', 'song_type', 'cover', 'is_lyric', 'lyric', 'clip_lyric', 'expand', 'user_id',
'guide', 'guide_duration', 'guide_clip', 'guide_clip_duration', 'karaoke', 'karaoke_clip',
'status', 'estimate_release_at', 'publish_at', 'sex', 'mark', 'speed', 'lang', 'project_id',
'created_form'
]);
}
/**
* @param int $activityId
* @return int
*/
public function getListenCount(int $activityId): int
{
return UserActivityViewPivot::query()->where('activity_id', $activityId)->whereHas('user')->distinct()->count('user_id');
}
/**
* @param int $activityId
* @return int
*/
public function getShareCount(int $activityId): int
{
return UserShare::query()->where('subject_type', Activity::class)->where('subject_id', $activityId)->whereHas('toUser')->distinct()->count('to_id');
}
/**
* @param int $activityId
* @return int
*/
public function getPublicAudioCount(int $activityId): int
{
return ActivityWork::query()->where('type', ActivityWorkTypeEnum::SUBMIT)->where('activity_id', $activityId)->where('is_hide', 0)->count();
}
}