|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Models\MediaAsset; |
| 7 | +use App\Services\AuthorizationService; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Support\Facades\DB; |
| 11 | +use Illuminate\Support\Str; |
| 12 | + |
| 13 | +class MediaCollectionController extends Controller |
| 14 | +{ |
| 15 | + public function __construct(private readonly AuthorizationService $authz) {} |
| 16 | + |
| 17 | + /** |
| 18 | + * List all collections for a space. |
| 19 | + * GET /v1/media/collections?space_id=... |
| 20 | + */ |
| 21 | + public function index(Request $request): JsonResponse |
| 22 | + { |
| 23 | + $request->validate([ |
| 24 | + 'space_id' => ['required', 'ulid', 'exists:spaces,id'], |
| 25 | + ]); |
| 26 | + |
| 27 | + $spaceId = $request->input('space_id'); |
| 28 | + $this->authz->authorize($request->user(), 'media.read', $spaceId); |
| 29 | + |
| 30 | + $collections = DB::table('media_collections') |
| 31 | + ->where('space_id', $spaceId) |
| 32 | + ->orderBy('name') |
| 33 | + ->get() |
| 34 | + ->map(function ($col) { |
| 35 | + $col->items_count = DB::table('media_collection_items') |
| 36 | + ->where('collection_id', $col->id) |
| 37 | + ->count(); |
| 38 | + |
| 39 | + return $col; |
| 40 | + }); |
| 41 | + |
| 42 | + return response()->json(['data' => $collections]); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a new collection. |
| 47 | + * POST /v1/media/collections |
| 48 | + */ |
| 49 | + public function store(Request $request): JsonResponse |
| 50 | + { |
| 51 | + $data = $request->validate([ |
| 52 | + 'space_id' => ['required', 'ulid', 'exists:spaces,id'], |
| 53 | + 'name' => ['required', 'string', 'max:255'], |
| 54 | + 'description' => ['nullable', 'string', 'max:1000'], |
| 55 | + 'is_smart' => ['boolean'], |
| 56 | + 'criteria' => ['nullable', 'array'], |
| 57 | + ]); |
| 58 | + |
| 59 | + $this->authz->authorize($request->user(), 'media.update', $data['space_id']); |
| 60 | + |
| 61 | + $id = DB::table('media_collections')->insertGetId([ |
| 62 | + 'space_id' => $data['space_id'], |
| 63 | + 'name' => $data['name'], |
| 64 | + 'slug' => Str::slug($data['name']), |
| 65 | + 'description' => $data['description'] ?? null, |
| 66 | + 'is_smart' => $data['is_smart'] ?? false, |
| 67 | + 'criteria' => isset($data['criteria']) ? json_encode($data['criteria']) : null, |
| 68 | + 'created_at' => now(), |
| 69 | + 'updated_at' => now(), |
| 70 | + ]); |
| 71 | + |
| 72 | + $collection = DB::table('media_collections')->find($id); |
| 73 | + |
| 74 | + return response()->json(['data' => $collection], 201); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get a single collection with its items. |
| 79 | + * GET /v1/media/collections/{collection} |
| 80 | + */ |
| 81 | + public function show(Request $request, int $collection): JsonResponse |
| 82 | + { |
| 83 | + $col = DB::table('media_collections')->where('id', $collection)->first(); |
| 84 | + abort_unless($col !== null, 404); |
| 85 | + |
| 86 | + $this->authz->authorize($request->user(), 'media.read', $col->space_id); |
| 87 | + |
| 88 | + $items = MediaAsset::join('media_collection_items', 'media_assets.id', '=', 'media_collection_items.media_asset_id') |
| 89 | + ->where('media_collection_items.collection_id', $collection) |
| 90 | + ->orderBy('media_collection_items.sort_order') |
| 91 | + ->select('media_assets.*', 'media_collection_items.sort_order', 'media_collection_items.added_at') |
| 92 | + ->get() |
| 93 | + ->map(fn ($a) => array_merge($a->toArray(), ['url' => $a->url])); |
| 94 | + |
| 95 | + return response()->json([ |
| 96 | + 'data' => $col, |
| 97 | + 'items' => $items, |
| 98 | + ]); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Rename / update a collection. |
| 103 | + * PATCH /v1/media/collections/{collection} |
| 104 | + */ |
| 105 | + public function update(Request $request, int $collection): JsonResponse |
| 106 | + { |
| 107 | + $col = DB::table('media_collections')->where('id', $collection)->first(); |
| 108 | + abort_unless($col !== null, 404); |
| 109 | + |
| 110 | + $this->authz->authorize($request->user(), 'media.update', $col->space_id); |
| 111 | + |
| 112 | + $data = $request->validate([ |
| 113 | + 'name' => ['sometimes', 'string', 'max:255'], |
| 114 | + 'description' => ['nullable', 'string', 'max:1000'], |
| 115 | + 'criteria' => ['nullable', 'array'], |
| 116 | + ]); |
| 117 | + |
| 118 | + $patch = ['updated_at' => now()]; |
| 119 | + if (isset($data['name'])) { |
| 120 | + $patch['name'] = $data['name']; |
| 121 | + $patch['slug'] = Str::slug($data['name']); |
| 122 | + } |
| 123 | + if (array_key_exists('description', $data)) { |
| 124 | + $patch['description'] = $data['description']; |
| 125 | + } |
| 126 | + if (array_key_exists('criteria', $data)) { |
| 127 | + $patch['criteria'] = json_encode($data['criteria']); |
| 128 | + } |
| 129 | + |
| 130 | + DB::table('media_collections')->where('id', $collection)->update($patch); |
| 131 | + |
| 132 | + return response()->json(['data' => DB::table('media_collections')->find($collection)]); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Delete a collection. |
| 137 | + * DELETE /v1/media/collections/{collection} |
| 138 | + */ |
| 139 | + public function destroy(Request $request, int $collection): JsonResponse |
| 140 | + { |
| 141 | + $col = DB::table('media_collections')->where('id', $collection)->first(); |
| 142 | + abort_unless($col !== null, 404); |
| 143 | + |
| 144 | + $this->authz->authorize($request->user(), 'media.delete', $col->space_id); |
| 145 | + |
| 146 | + DB::table('media_collections')->where('id', $collection)->delete(); |
| 147 | + |
| 148 | + return response()->json(null, 204); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Add an asset to a collection. |
| 153 | + * POST /v1/media/collections/{collection}/items |
| 154 | + */ |
| 155 | + public function addItem(Request $request, int $collection): JsonResponse |
| 156 | + { |
| 157 | + $col = DB::table('media_collections')->where('id', $collection)->first(); |
| 158 | + abort_unless($col !== null, 404); |
| 159 | + |
| 160 | + $this->authz->authorize($request->user(), 'media.update', $col->space_id); |
| 161 | + |
| 162 | + $data = $request->validate([ |
| 163 | + 'media_asset_id' => ['required', 'string', 'exists:media_assets,id'], |
| 164 | + 'sort_order' => ['integer'], |
| 165 | + ]); |
| 166 | + |
| 167 | + // Verify asset belongs to the same space as the collection |
| 168 | + MediaAsset::where('id', $data['media_asset_id']) |
| 169 | + ->where('space_id', $col->space_id) |
| 170 | + ->firstOrFail(); |
| 171 | + |
| 172 | + DB::table('media_collection_items')->insertOrIgnore([ |
| 173 | + 'collection_id' => $collection, |
| 174 | + 'media_asset_id' => $data['media_asset_id'], |
| 175 | + 'sort_order' => $data['sort_order'] ?? 0, |
| 176 | + 'added_at' => now(), |
| 177 | + ]); |
| 178 | + |
| 179 | + return response()->json(['message' => 'Asset added to collection.'], 201); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Remove an asset from a collection. |
| 184 | + * DELETE /v1/media/collections/{collection}/items/{asset} |
| 185 | + */ |
| 186 | + public function removeItem(Request $request, int $collection, string $asset): JsonResponse |
| 187 | + { |
| 188 | + $col = DB::table('media_collections')->where('id', $collection)->first(); |
| 189 | + abort_unless($col !== null, 404); |
| 190 | + |
| 191 | + $this->authz->authorize($request->user(), 'media.update', $col->space_id); |
| 192 | + |
| 193 | + DB::table('media_collection_items') |
| 194 | + ->where('collection_id', $collection) |
| 195 | + ->where('media_asset_id', $asset) |
| 196 | + ->delete(); |
| 197 | + |
| 198 | + return response()->json(null, 204); |
| 199 | + } |
| 200 | +} |
0 commit comments