Files
approagency/app/Http/Controllers/PromotionController.php
T
2025-11-25 15:49:24 +03:30

104 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Promotion;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class PromotionController extends Controller
{
public function index()
{
$now = now();
$promotions = Promotion::where('is_active', true)
->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']);
}
}