137 lines
4.5 KiB
PHP
137 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\Api\ApiService;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PhotoSliderController extends Controller
|
|
{
|
|
protected ApiService $apiService;
|
|
|
|
public function __construct(ApiService $apiService)
|
|
{
|
|
$this->apiService = $apiService;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
try {
|
|
$response = $this->apiService->get('/photo-sliders');
|
|
|
|
if ($response->successful()) {
|
|
return view('pages.home page.photo-slider', [
|
|
'sliders' => $response->json()['data']
|
|
]);
|
|
}
|
|
|
|
return back()->with('error', 'Unable to fetch photo sliders.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
|
|
'order' => 'required|integer|min:1',
|
|
'status' => 'required|string|in:active,inactive',
|
|
'link_url' => 'nullable|url',
|
|
'start_date' => 'required|date',
|
|
'end_date' => 'required|date|after:start_date'
|
|
]);
|
|
|
|
try {
|
|
// Handle file upload
|
|
if ($request->hasFile('image')) {
|
|
$path = $request->file('image')->store('photo-sliders', 'public');
|
|
$validated['image_path'] = $path;
|
|
}
|
|
|
|
$response = $this->apiService->post('/photo-sliders', $validated);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('photo-slider')
|
|
->with('success', 'Photo slider created successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to create photo slider.');
|
|
}
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$validated = $request->validate([
|
|
'title' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
|
|
'order' => 'required|integer|min:1',
|
|
'status' => 'required|string|in:active,inactive',
|
|
'link_url' => 'nullable|url',
|
|
'start_date' => 'required|date',
|
|
'end_date' => 'required|date|after:start_date'
|
|
]);
|
|
|
|
try {
|
|
// Handle file upload if new image is provided
|
|
if ($request->hasFile('image')) {
|
|
$path = $request->file('image')->store('photo-sliders', 'public');
|
|
$validated['image_path'] = $path;
|
|
}
|
|
|
|
$response = $this->apiService->put("/photo-sliders/{$id}", $validated);
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('photo-slider')
|
|
->with('success', 'Photo slider updated successfully.');
|
|
}
|
|
|
|
return back()->withErrors($response->json()['errors']);
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Unable to update photo slider.');
|
|
}
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$response = $this->apiService->delete("/photo-sliders/{$id}");
|
|
|
|
if ($response->successful()) {
|
|
return redirect()->route('photo-slider')
|
|
->with('success', 'Photo slider deleted successfully.');
|
|
}
|
|
|
|
return back()->with('error', 'Unable to delete photo slider.');
|
|
} catch (\Exception $e) {
|
|
return back()->with('error', 'Service unavailable.');
|
|
}
|
|
}
|
|
|
|
public function updateOrder(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'orders' => 'required|array',
|
|
'orders.*.id' => 'required|integer',
|
|
'orders.*.order' => 'required|integer|min:1'
|
|
]);
|
|
|
|
try {
|
|
$response = $this->apiService->post("/photo-sliders/reorder", $validated);
|
|
|
|
if ($response->successful()) {
|
|
return response()->json(['message' => 'Order updated successfully']);
|
|
}
|
|
|
|
return response()->json(['error' => 'Unable to update order'], 400);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => 'Service unavailable'], 503);
|
|
}
|
|
}
|
|
}
|