D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
forge
/
storage-online.ghanempharmacy.com
/
app
/
Http
/
Controllers
/
Filename :
ClientController.php
back
Copy
<?php namespace App\Http\Controllers; use App\Models\Client; use App\Models\Medicine; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; class ClientController extends Controller { public function index() { $clients = Client::all(); return view('clients.index', compact('clients')); } public function store(Request $request) { $request->validate([ 'name' => 'required|string', 'api_url' => 'required|url', ]); Client::create($request->only('name', 'api_url')); return back()->with('success', 'Client added successfully.'); } public function sync() { $clients = Client::all(); $totalSynced = 0; $errors = []; foreach ($clients as $client) { try { // Assuming client API is /api/medicines/sync which returns { "data": [ ... ] } // We append /api/medicines/sync if not present, OR use the full URL provided // Let's assume the user enters the BASE URL or the full API URL. // To be safe, let's enforce entering the full "Sync" URL in the UI. $response = Http::timeout(10)->get($client->api_url); if ($response->successful()) { $movies = $response->json('data'); // Assuming structure matches our own export if (is_array($movies)) { foreach ($movies as $item) { $name = $item['n'] ?? $item['name'] ?? null; $image = $item['i'] ?? $item['image'] ?? null; if ($name && $image) { Medicine::firstOrCreate( ['name' => $name], ['thumbnail' => $image] ); $totalSynced++; } } } } else { $errors[] = "Failed to sync with {$client->name}: " . $response->status(); } } catch (\Exception $e) { $errors[] = "Error connecting to {$client->name}: " . $e->getMessage(); } } if (count($errors) > 0) { return back()->with('error', 'Sync completed with errors: ' . implode(', ', $errors))->with('success', "Synced $totalSynced medicines."); } return back()->with('success', "Successfully synced $totalSynced medicines from " . $clients->count() . " clients."); } }