Compare commits
10
Commits
c9d442223c
...
b63fdc5439
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b63fdc5439 | ||
|
|
676522d70d | ||
|
|
4d0823ef86 | ||
|
|
40f3db00ba | ||
|
|
29005f00da | ||
|
|
be69e5cfda | ||
|
|
23cd9c7b38 | ||
|
|
834c09025f | ||
|
|
12a0500520 | ||
|
|
6912059469 |
@@ -5,7 +5,7 @@
|
|||||||
use App\Models\Media;
|
use App\Models\Media;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
use App\Models\Tag;
|
use App\Models\Tag;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ public function store(Request $request)
|
|||||||
'category_name' => 'nullable|string|max:255',
|
'category_name' => 'nullable|string|max:255',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
'duration' => 'nullable|integer',
|
'duration' => 'nullable|integer',
|
||||||
|
'is_premium' => 'nullable|boolean',
|
||||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
||||||
'external_url' => 'nullable|string',
|
'external_url' => 'nullable|string',
|
||||||
'visibility' => 'nullable|in:public,private',
|
'visibility' => 'nullable|in:public,private',
|
||||||
@@ -55,6 +55,7 @@ public function store(Request $request)
|
|||||||
'category_id' => $categoryId,
|
'category_id' => $categoryId,
|
||||||
'duration' => $data['duration'] ?? null,
|
'duration' => $data['duration'] ?? null,
|
||||||
'visibility' => $data['visibility'] ?? 'public',
|
'visibility' => $data['visibility'] ?? 'public',
|
||||||
|
'is_premium'=> $data['is_premium'] ?? false
|
||||||
]);
|
]);
|
||||||
if (!empty($data['tags'])) {
|
if (!empty($data['tags'])) {
|
||||||
$tagIds = [];
|
$tagIds = [];
|
||||||
@@ -73,14 +74,74 @@ public function store(Request $request)
|
|||||||
}
|
}
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$query = Media::with(['image', 'category', 'myNote', 'tags'])
|
$query = Media::with(['image', 'category', 'myNote', 'tags','comments'])
|
||||||
->where(function ($q) {
|
->where(function ($q) {
|
||||||
$q->where('visibility', 'public')
|
$q->where('visibility', 'public')
|
||||||
->orWhere('user_id', auth()->id());
|
->orWhere('user_id', auth()->id());
|
||||||
});
|
});
|
||||||
if ($request->filled('category')) {
|
|
||||||
$query->where('category_id', $request->category);
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 1️⃣ Multi Category
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ($request->filled('categories')) {
|
||||||
|
$categories = explode(',', $request->categories);
|
||||||
|
$query->whereIn('category_id', $categories);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 2️⃣ Multi Duration Ranges
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| duration stored in minutes (integer)
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ($request->filled('durations')) {
|
||||||
|
|
||||||
|
$ranges = explode(',', $request->durations);
|
||||||
|
|
||||||
|
$query->where(function ($q) use ($ranges) {
|
||||||
|
|
||||||
|
foreach ($ranges as $range) {
|
||||||
|
|
||||||
|
if ($range === '1-2') {
|
||||||
|
$q->orWhereBetween('duration', [1, 2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($range === '2-5') {
|
||||||
|
$q->orWhereBetween('duration', [3, 5]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($range === '5-10') {
|
||||||
|
$q->orWhereBetween('duration', [6, 10]);
|
||||||
|
}
|
||||||
|
if ($range === '10-30') {
|
||||||
|
$q->orWhereBetween('duration', [10, 30]);
|
||||||
|
}
|
||||||
|
if ($range === '30-60') {
|
||||||
|
$q->orWhereBetween('duration', [30, 60]);
|
||||||
|
}
|
||||||
|
if ($range === '60-120') {
|
||||||
|
$q->orWhereBetween('duration', [60, 120]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($range === 'other') {
|
||||||
|
$q->orWhere('duration', '>', 120);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 3️⃣ Tags
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
if ($request->filled('tags')) {
|
if ($request->filled('tags')) {
|
||||||
$tags = explode(',', $request->tags);
|
$tags = explode(',', $request->tags);
|
||||||
|
|
||||||
@@ -88,34 +149,179 @@ public function index(Request $request)
|
|||||||
$q->whereIn('name', $tags);
|
$q->whereIn('name', $tags);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 4️⃣ Search
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
if ($request->filled('search')) {
|
if ($request->filled('search')) {
|
||||||
|
|
||||||
$search = $request->search;
|
$search = $request->search;
|
||||||
|
|
||||||
$query->where(function ($q) use ($search) {
|
$query->where(function ($q) use ($search) {
|
||||||
|
|
||||||
// title & caption
|
|
||||||
$q->where('title', 'LIKE', "%$search%")
|
$q->where('title', 'LIKE', "%$search%")
|
||||||
->orWhere('caption', 'LIKE', "%$search%")
|
->orWhere('caption', 'LIKE', "%$search%")
|
||||||
|
|
||||||
// category (join)
|
|
||||||
->orWhereHas('category', function ($c) use ($search) {
|
->orWhereHas('category', function ($c) use ($search) {
|
||||||
$c->where('name', 'LIKE', "%$search%");
|
$c->where('name', 'LIKE', "%$search%");
|
||||||
})
|
})
|
||||||
|
|
||||||
// tags
|
|
||||||
->orWhereHas('tags', function ($t) use ($search) {
|
->orWhereHas('tags', function ($t) use ($search) {
|
||||||
$t->where('name', 'LIKE', "%$search%");
|
$t->where('name', 'LIKE', "%$search%");
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(
|
return response()->json(
|
||||||
$query->orderBy('created_at', 'desc')->get()
|
$query->orderBy('created_at', 'desc')->get()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function submitFeedback(Request $request, $mediaId)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'stars' => 'nullable|integer|min:1|max:5',
|
||||||
|
'content' => 'nullable|string|max:2000',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// حداقل یکی باید ارسال شود
|
||||||
|
if (!$request->filled('stars') && !$request->filled('content')) {
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Stars or comment is required'
|
||||||
|
], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
$media = Media::where('id', $mediaId)
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->where('visibility', 'public')
|
||||||
|
->orWhere('user_id', auth()->id());
|
||||||
|
})
|
||||||
|
->firstOrFail();
|
||||||
|
|
||||||
|
DB::beginTransaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$rating = null;
|
||||||
|
$comment = null;
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| ⭐ Handle Rating (update or create)
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ($request->filled('stars')) {
|
||||||
|
$rating = $media->ratings()->updateOrCreate(
|
||||||
|
['user_id' => auth()->id()],
|
||||||
|
['stars' => $request->stars]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 💬 Handle Comment (create only if exists)
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ($request->filled('content')) {
|
||||||
|
$comment = $media->comments()->create([
|
||||||
|
'user_id' => auth()->id(),
|
||||||
|
'content' => $request->content,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::commit();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Feedback submitted successfully',
|
||||||
|
'average_rating' => $media->fresh()->average_rating,
|
||||||
|
'your_rating' => optional($rating)->stars,
|
||||||
|
'comment' => $comment ? $comment->load('user') : null,
|
||||||
|
]);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
|
||||||
|
DB::rollBack();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Something went wrong',
|
||||||
|
'error' => $e->getMessage()
|
||||||
|
], 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function filters(Request $request)
|
||||||
|
{
|
||||||
|
$baseQuery = Media::query()
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->where('visibility', 'public')
|
||||||
|
->orWhere('user_id', auth()->id());
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 1️⃣ Categories with media count
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
$categories = Category::select(
|
||||||
|
'categories.id',
|
||||||
|
'categories.name',
|
||||||
|
DB::raw('COUNT(media.id) as media_count')
|
||||||
|
)
|
||||||
|
->leftJoin('media', function ($join) {
|
||||||
|
$join->on('categories.id', '=', 'media.category_id')
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->where('media.visibility', 'public')
|
||||||
|
->orWhere('media.user_id', auth()->id());
|
||||||
|
});
|
||||||
|
})
|
||||||
|
->groupBy('categories.id', 'categories.name')
|
||||||
|
->orderByDesc('media_count')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| 2️⃣ Duration ranges
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| duration is stored in seconds
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
$durations = Media::select(
|
||||||
|
DB::raw("
|
||||||
|
CASE
|
||||||
|
WHEN duration BETWEEN 1 AND 2 THEN '1-2'
|
||||||
|
WHEN duration BETWEEN 3 AND 5 THEN '2-5'
|
||||||
|
WHEN duration BETWEEN 5 AND 10 THEN '5-10'
|
||||||
|
WHEN duration BETWEEN 10 AND 20 THEN '10-20'
|
||||||
|
WHEN duration BETWEEN 20 AND 30 THEN '20-30'
|
||||||
|
WHEN duration BETWEEN 60 AND 120 THEN '60-120'
|
||||||
|
ELSE 'other'
|
||||||
|
END as duration_range
|
||||||
|
"),
|
||||||
|
DB::raw('COUNT(*) as total')
|
||||||
|
)
|
||||||
|
->whereNotNull('duration')
|
||||||
|
->where(function ($q) {
|
||||||
|
$q->where('visibility', 'public')
|
||||||
|
->orWhere('user_id', auth()->id());
|
||||||
|
})
|
||||||
|
->groupBy('duration_range')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'categories' => $categories,
|
||||||
|
'durations' => $durations,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
$media = Media::with(['image', 'category', 'myNote', 'tags'])
|
$media = Media::with(['image', 'category', 'myNote', 'tags' ,'comments'])
|
||||||
->where('id', $id)
|
->where('id', $id)
|
||||||
->where(function($query) {
|
->where(function($query) {
|
||||||
$query->where('visibility', 'public')
|
$query->where('visibility', 'public')
|
||||||
@@ -139,9 +345,63 @@ public function show($id)
|
|||||||
'tags' => $media->tags,
|
'tags' => $media->tags,
|
||||||
'myNote' => $media->myNote,
|
'myNote' => $media->myNote,
|
||||||
'is_saved' => $media->is_saved,
|
'is_saved' => $media->is_saved,
|
||||||
|
'is_premium' => $media->is_premium,
|
||||||
|
'comments' => $media->comments,
|
||||||
|
'average_rating' => $media->average_rating,
|
||||||
|
'your_rating' => $media->user_rating,
|
||||||
|
'comments_count' => $media->comments()->count(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
public function rate(Request $request, $mediaId)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'stars' => 'required|integer|min:1|max:5'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$media = Media::findOrFail($mediaId);
|
||||||
|
|
||||||
|
$rating = $media->ratings()->updateOrCreate(
|
||||||
|
['user_id' => auth()->id()],
|
||||||
|
['stars' => $request->stars]
|
||||||
|
);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Rating saved',
|
||||||
|
'average_rating' => $media->fresh()->average_rating,
|
||||||
|
'your_rating' => $rating->stars
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function storeComment(Request $request, $mediaId)
|
||||||
|
{
|
||||||
|
$request->validate([
|
||||||
|
'content' => 'required|string|max:1000'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$media = Media::findOrFail($mediaId);
|
||||||
|
|
||||||
|
$comment = $media->comments()->create([
|
||||||
|
'user_id' => auth()->id(),
|
||||||
|
'content' => $request->content
|
||||||
|
]);
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'message' => 'Comment added',
|
||||||
|
'comment' => $comment->load('user')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getComments($mediaId)
|
||||||
|
{
|
||||||
|
$media = Media::findOrFail($mediaId);
|
||||||
|
|
||||||
|
return response()->json(
|
||||||
|
$media->comments()
|
||||||
|
->with('user')
|
||||||
|
->latest()
|
||||||
|
->paginate(10)
|
||||||
|
);
|
||||||
|
}
|
||||||
// UPDATE media
|
// UPDATE media
|
||||||
public function update(Request $request, $id)
|
public function update(Request $request, $id)
|
||||||
{
|
{
|
||||||
@@ -157,7 +417,7 @@ public function update(Request $request, $id)
|
|||||||
// support both: category_id or category_name
|
// support both: category_id or category_name
|
||||||
'category_id' => 'nullable|exists:categories,id',
|
'category_id' => 'nullable|exists:categories,id',
|
||||||
'category_name' => 'nullable|string|max:255',
|
'category_name' => 'nullable|string|max:255',
|
||||||
|
'is_premium' => 'nullable|boolean',
|
||||||
'image_id' => 'nullable|exists:images,id',
|
'image_id' => 'nullable|exists:images,id',
|
||||||
'duration' => 'nullable|integer',
|
'duration' => 'nullable|integer',
|
||||||
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
'file' => 'nullable|mimes:mp3,wav,mp4,mov|max:512000',
|
||||||
@@ -189,6 +449,7 @@ public function update(Request $request, $id)
|
|||||||
'caption' => $data['caption'] ?? $media->caption,
|
'caption' => $data['caption'] ?? $media->caption,
|
||||||
'type' => $data['type'] ?? $media->type,
|
'type' => $data['type'] ?? $media->type,
|
||||||
'category_id' => $categoryId,
|
'category_id' => $categoryId,
|
||||||
|
'is_premium' => $data['is_premium'] ?? $media->is_premium,
|
||||||
'duration' => $data['duration'] ?? $media->duration,
|
'duration' => $data['duration'] ?? $media->duration,
|
||||||
'external_url' => $data['external_url'] ?? $media->external_url,
|
'external_url' => $data['external_url'] ?? $media->external_url,
|
||||||
'visibility' => $data['visibility'] ?? $media->visibility,
|
'visibility' => $data['visibility'] ?? $media->visibility,
|
||||||
@@ -239,12 +500,20 @@ public function destroy($id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SAVE media
|
// SAVE media
|
||||||
public function saveMedia($id)
|
public function toggleSaveMedia($id)
|
||||||
{
|
{
|
||||||
auth()->user()->savedMedia()->syncWithoutDetaching([$id]);
|
$user = auth()->user();
|
||||||
|
|
||||||
|
if ($user->savedMedia()->where('media_id', $id)->exists()) {
|
||||||
|
// already saved → unsave
|
||||||
|
$user->savedMedia()->detach($id);
|
||||||
|
return response()->json(['message' => 'Unsaved!']);
|
||||||
|
} else {
|
||||||
|
// not saved → save
|
||||||
|
$user->savedMedia()->attach($id);
|
||||||
return response()->json(['message' => 'Saved!']);
|
return response()->json(['message' => 'Saved!']);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GET saved
|
// GET saved
|
||||||
public function saved()
|
public function saved()
|
||||||
@@ -254,17 +523,38 @@ public function saved()
|
|||||||
|
|
||||||
public function storeNote(Request $request, $mediaId)
|
public function storeNote(Request $request, $mediaId)
|
||||||
{
|
{
|
||||||
$data = $request->validate([
|
|
||||||
'content' => 'required|string'
|
|
||||||
]);
|
|
||||||
|
|
||||||
$media = Media::findOrFail($mediaId);
|
$media = Media::findOrFail($mediaId);
|
||||||
|
$userId = auth()->id();
|
||||||
|
|
||||||
|
$content = $request->input('content');
|
||||||
|
|
||||||
|
// Check if note already exists for this user
|
||||||
|
$note = $media->notes()->where('user_id', $userId)->first();
|
||||||
|
|
||||||
|
if (empty($content)) {
|
||||||
|
// If content is empty, delete the note if it exists
|
||||||
|
if ($note) {
|
||||||
|
$note->delete();
|
||||||
|
return response()->json(['message' => 'Note deleted']);
|
||||||
|
}
|
||||||
|
return response()->json(['message' => 'No note to delete']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($note) {
|
||||||
|
// Update existing note
|
||||||
|
$note->update(['content' => $content]);
|
||||||
|
} else {
|
||||||
|
// Create new note
|
||||||
$note = $media->notes()->create([
|
$note = $media->notes()->create([
|
||||||
'user_id' => auth()->id(),
|
'user_id' => $userId,
|
||||||
'content' => $data['content'],
|
'content' => $content,
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json(['message' => 'Note added', 'note' => $note]);
|
return response()->json([
|
||||||
|
'message' => 'Note saved successfully',
|
||||||
|
'note' => $note
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -486,9 +486,22 @@ public function profile(Request $request)
|
|||||||
if ($response->successful()) {
|
if ($response->successful()) {
|
||||||
$user = $this->syncUserFromStatus($response->json(), $user);
|
$user = $this->syncUserFromStatus($response->json(), $user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$remindersResponse = Http::acceptJson()
|
||||||
|
->withToken($data['token'])
|
||||||
|
->get('https://api.approagency.ir/api/reminders', [
|
||||||
|
'type' => 'reminder',
|
||||||
|
'package_name' => $data['package_name'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$reminders = $remindersResponse->successful()
|
||||||
|
? $remindersResponse->json()
|
||||||
|
: [];
|
||||||
|
|
||||||
$user->load(['breathingSessions.template']);
|
$user->load(['breathingSessions.template']);
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'user' => $user
|
'user' => $user,
|
||||||
|
'reminders' => $reminders,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
public function status(Request $request)
|
public function status(Request $request)
|
||||||
@@ -616,7 +629,7 @@ public function googleLogin(Request $request)
|
|||||||
|
|
||||||
public function leaderBoard(){
|
public function leaderBoard(){
|
||||||
$users = User::with(['breathingSessions.template'])->where('xp', '>', 0)
|
$users = User::with(['breathingSessions.template'])->where('xp', '>', 0)
|
||||||
->orderBy('xp', 'desc')
|
->orderBy('xp', 'desc')->limit(20)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return $users;
|
return $users;
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Comment extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'media_id',
|
||||||
|
'content',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function media()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Media::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
+28
-2
@@ -17,8 +17,12 @@ class Media extends Model
|
|||||||
'external_url',
|
'external_url',
|
||||||
'duration',
|
'duration',
|
||||||
'visibility',
|
'visibility',
|
||||||
|
'is_premium'
|
||||||
|
];
|
||||||
|
protected $appends = [
|
||||||
|
'average_rating',
|
||||||
|
'user_rating',
|
||||||
];
|
];
|
||||||
|
|
||||||
public function image()
|
public function image()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Image::class);
|
return $this->belongsTo(Image::class);
|
||||||
@@ -64,5 +68,27 @@ public function getIsSavedAttribute()
|
|||||||
->where('user_id', auth()->id())
|
->where('user_id', auth()->id())
|
||||||
->exists();
|
->exists();
|
||||||
}
|
}
|
||||||
|
public function ratings()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Rating::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAverageRatingAttribute()
|
||||||
|
{
|
||||||
|
return round($this->ratings()->avg('stars'), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUserRatingAttribute()
|
||||||
|
{
|
||||||
|
if (!auth()->check()) return null;
|
||||||
|
|
||||||
|
return $this->ratings()
|
||||||
|
->where('user_id', auth()->id())
|
||||||
|
->value('stars');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function comments()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Comment::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Rating extends Model
|
||||||
|
{
|
||||||
|
protected $fillable = [
|
||||||
|
'user_id',
|
||||||
|
'media_id',
|
||||||
|
'stars',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function media()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Media::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
Regular → Executable
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('media', function (Blueprint $table) {
|
||||||
|
$table->boolean('is_premium')->default(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('media', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('is_premium');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('ratings', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->foreignId('media_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->tinyInteger('stars'); // 1-5
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['user_id', 'media_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('ratings');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('comments', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->foreignId('media_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->text('content');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('comments');
|
||||||
|
}
|
||||||
|
};
|
||||||
+8
-2
@@ -135,13 +135,19 @@
|
|||||||
|
|
||||||
|
|
||||||
///media
|
///media
|
||||||
|
Route::get('/media/filters', [MediaController::class, 'filters']);
|
||||||
Route::post('/media', [MediaController::class, 'store']);
|
Route::post('/media', [MediaController::class, 'store']);
|
||||||
Route::get('/media', [MediaController::class, 'index']);
|
Route::get('/media', [MediaController::class, 'index']);
|
||||||
Route::post('/media/{id}', [MediaController::class, 'update']);
|
Route::post('/media/{id}', [MediaController::class, 'update']);
|
||||||
|
Route::get('/media/saved', [MediaController::class, 'saved']);
|
||||||
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
Route::delete('/media/{id}', [MediaController::class, 'destroy']);
|
||||||
Route::get('/media/{id}', [MediaController::class, 'show']);
|
Route::get('/media/{id}', [MediaController::class, 'show']);
|
||||||
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
Route::post('/media/{id}/save', [MediaController::class, 'toggleSaveMedia']);
|
||||||
Route::get('/media/saved', [MediaController::class, 'saved']);
|
Route::post('/media/{id}/rate', [MediaController::class, 'rate']);
|
||||||
|
Route::post('/media/{id}/comment', [MediaController::class, 'storeComment']);
|
||||||
|
Route::get('/media/{id}/comments', [MediaController::class, 'getComments']);
|
||||||
|
Route::post('/media/{id}/feedback', [MediaController::class, 'submitFeedback']);
|
||||||
|
|
||||||
|
|
||||||
Route::post('/media/{id}/note', [MediaController::class, 'storeNote']);
|
Route::post('/media/{id}/note', [MediaController::class, 'storeNote']);
|
||||||
});
|
});
|
||||||
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Regular → Executable
Reference in New Issue
Block a user