- Created ExampleTest and RoutesTest for basic application response and route testing. - Implemented IsolatedDatabaseTestCase for isolated database testing. - Added unit tests for various models and services including Contract, CPE, and LeadDetail. - Developed request validation tests for CreateClientRequest, UpdateClientRequest, and ContractRequest. - Introduced Vite configuration for asset management in the Laravel application.
141 lines
3.8 KiB
PHP
141 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\CreateClientRequest;
|
|
use App\Http\Requests\UpdateClientRequest;
|
|
use App\Services\ClientService;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ClientController extends Controller
|
|
{
|
|
public function __construct(private ClientService $clients)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* List all clients
|
|
*
|
|
* @author Luis Medon <luismedon@gmail.com>
|
|
* @since 2025/11
|
|
* @version 2025/11
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
if ($request->has('per_page')) {
|
|
return response()->json(['clients' => $this->clients->listing($request, true)]);
|
|
}
|
|
$clients = $this->clients->listing($request)->map(function ($client) {
|
|
return [
|
|
'id' => $client->id,
|
|
'type' => $client->type,
|
|
'lead_status_id' => $client->lead_status_id,
|
|
'client_type_id' => $client->client_type_id,
|
|
'name' => $client->name,
|
|
'tax_number' => $client->tax_number,
|
|
'address' => $client->address,
|
|
'phone' => $client->phone,
|
|
'email' => $client->email,
|
|
'seller_id' => $client->seller_id,
|
|
'status' => $client->status,
|
|
'notes' => $client->notes,
|
|
'contact_info' => $client->contact_info,
|
|
'created_at' => $client->created_at,
|
|
'updated_at' => $client->updated_at,
|
|
];
|
|
});
|
|
|
|
return response()->json(['clients' => $clients]);
|
|
}
|
|
|
|
/**
|
|
* Apply filters to the clients query
|
|
*
|
|
* @author Luis Medon <luismedon@gmail.com>
|
|
* @since 2025/11
|
|
* @version 2025/11
|
|
*/
|
|
/**
|
|
* Show a single client
|
|
*
|
|
* @author Luis Medon <luismedon@gmail.com>
|
|
* @since 2025/11
|
|
* @version 2025/11
|
|
*/
|
|
public function show($id)
|
|
{
|
|
return response()->json($this->clients->find($id));
|
|
}
|
|
|
|
/**
|
|
* Store a new client
|
|
*
|
|
* @author Luis Medon <luismedon@gmail.com>
|
|
* @since 2025/11
|
|
* @version 2025/11
|
|
*/
|
|
public function store(CreateClientRequest $request)
|
|
{
|
|
$client = $this->clients->create($request->validated());
|
|
|
|
return response()->json([
|
|
'message' => 'Client created successfully',
|
|
'client' => $client
|
|
], 201);
|
|
}
|
|
|
|
/**
|
|
* Update an existing client
|
|
*
|
|
* @author Luis Medon <luismedon@gmail.com>
|
|
* @since 2025/11
|
|
* @version 2025/11
|
|
*/
|
|
public function update(UpdateClientRequest $request, $id)
|
|
{
|
|
$client = $this->clients->update($id, $request->validated());
|
|
|
|
return response()->json([
|
|
'message' => 'Client updated successfully',
|
|
'client' => $client
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Delete a client (soft delete by changing status)
|
|
*
|
|
* @author Luis Medon <luismedon@gmail.com>
|
|
* @since 2025/11
|
|
* @version 2025/11
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$this->clients->delete($id);
|
|
|
|
return response()->json(['message' => 'Client deleted successfully']);
|
|
}
|
|
|
|
public function options(Request $request)
|
|
{
|
|
return response()->json($request->filled('type')
|
|
? $this->clients->form($request->input('type'), $request->user())
|
|
: ['clientTypes' => $this->clients->types()]);
|
|
}
|
|
|
|
public function detail($id)
|
|
{
|
|
return response()->json($this->clients->detail($id));
|
|
}
|
|
|
|
public function searchName(Request $request)
|
|
{
|
|
return response()->json($this->clients->searchName($request->input('term', '')));
|
|
}
|
|
|
|
public function searchID(Request $request)
|
|
{
|
|
return response()->json($this->clients->searchID($request->input('id')));
|
|
}
|
|
}
|