diff --git a/app/Http/Controllers/PromotionController.php b/app/Http/Controllers/PromotionController.php new file mode 100644 index 0000000..a19800e --- /dev/null +++ b/app/Http/Controllers/PromotionController.php @@ -0,0 +1,103 @@ +where(function ($q) use ($now) { + $q->whereNull('start_at') + ->orWhere('start_at', '<=', $now); + }) + ->where(function ($q) use ($now) { + $q->whereNull('end_at') + ->orWhere('end_at', '>=', $now); + }) + ->orderBy('priority', 'desc') + ->get(); + + return response()->json($promotions); + } + + public function store(Request $request) + { + $data = $request->validate([ + 'type' => 'required|string|in:slider,action,paid_app,banner', + 'title' => 'nullable|string', + 'subtitle' => 'nullable|string', + 'image' => 'nullable|file|image|max:2048', + 'action_text' => 'nullable|string', + 'url_myket' => 'nullable|string', + 'url_bazzar' => 'nullable|string', + 'url_google_play' => 'nullable|string', + 'url_site' => 'nullable|string', + 'priority' => 'nullable|integer', + 'start_at' => 'nullable|date', + 'end_at' => 'nullable|date', +]); + + + if ($request->hasFile('image')) { + $data['image'] = $request->file('image')->store('promotions', 'public'); + } + + $promotion = Promotion::create($data); + + return response()->json($promotion, 201); + } + + // UPDATE + public function update(Request $request, Promotion $promotion) + { +$data = $request->validate([ + 'type' => 'nullable|string|in:slider,action,paid_app,banner', + 'title' => 'nullable|string', + 'subtitle' => 'nullable|string', + + 'image' => 'nullable|file|image|max:2048', + + 'action_text' => 'nullable|string', + + 'url_myket' => 'nullable|string', + 'url_bazzar' => 'nullable|string', + 'url_google_play' => 'nullable|string', + 'url_site' => 'nullable|string', + + 'is_active' => 'nullable|boolean', + 'priority' => 'nullable|integer', + 'start_at' => 'nullable|date', + 'end_at' => 'nullable|date', +]); + + if ($request->hasFile('image')) { + if ($promotion->image) { + Storage::disk('public')->delete($promotion->image); + } + $data['image'] = $request->file('image')->store('promotions', 'public'); + } + + $promotion->update($data); + + return response()->json($promotion); + } + + // DELETE + public function destroy(Promotion $promotion) + { + if ($promotion->image) { + Storage::disk('public')->delete($promotion->image); + } + + $promotion->delete(); + + return response()->json(['message' => 'Promotion deleted']); + } +} diff --git a/app/Models/Promotion.php b/app/Models/Promotion.php new file mode 100644 index 0000000..6a21a7b --- /dev/null +++ b/app/Models/Promotion.php @@ -0,0 +1,20 @@ + 'boolean', + 'start_at' => 'datetime', + 'end_at' => 'datetime' + ]; +} diff --git a/database/migrations/2025_11_25_144031_create_promotions_table.php b/database/migrations/2025_11_25_144031_create_promotions_table.php new file mode 100644 index 0000000..0839837 --- /dev/null +++ b/database/migrations/2025_11_25_144031_create_promotions_table.php @@ -0,0 +1,49 @@ +id(); + $table->string('type'); // slider, action, paid_app, banner + + $table->string('title')->nullable(); + $table->string('subtitle')->nullable(); + + $table->string('image')->nullable(); + + // action labels + $table->string('action_text')->nullable(); + + // platform-specific URLs + $table->string('url_myket')->nullable(); + $table->string('url_bazzar')->nullable(); + $table->string('url_google_play')->nullable(); + $table->string('url_site')->nullable(); + + $table->boolean('is_active')->default(true); + $table->integer('priority')->default(0); + + $table->timestamp('start_at')->nullable(); + $table->timestamp('end_at')->nullable(); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('promotions'); + } +}; diff --git a/routes/api.php b/routes/api.php index 5a4f0e7..3b6116b 100644 --- a/routes/api.php +++ b/routes/api.php @@ -12,6 +12,7 @@ use App\Http\Controllers\UserController; use App\Http\Controllers\WalletController; use App\Http\Controllers\ZarinpalController; +use App\Http\Controllers\PromotionController; use App\Models\Transaction; use Illuminate\Support\Facades\Route; use Laravel\Socialite\Facades\Socialite; @@ -94,6 +95,11 @@ Route::delete('/users/{mobile}/status', 'unsubscribeUser'); }); }); + ///promotions + Route::get('/promotions', [PromotionController::class, 'index']); + Route::post('/promotions', [PromotionController::class, 'store']); + Route::post('/promotions/{promotion}', [PromotionController::class, 'update']); + Route::delete('/promotions/{promotion}', [PromotionController::class, 'destroy']); Route::get('/asanpardakht/pay', [AsanPardakhtController::class, 'pay']); });