76 lines
2.6 KiB
PHP
76 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Customer\Concerns;
|
|
|
|
use App\Models\DnsDomain;
|
|
use App\Models\DnsRecord;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
trait ManagesCustomerDomains
|
|
{
|
|
public function getDomains(string $uuid): JsonResponse
|
|
{
|
|
$customer = $this->resolveCustomer($uuid);
|
|
|
|
$domains = $this->moduleDomainRepository->filter(['customer_id' => $customer->id], ['customer']);
|
|
|
|
return response()->json(['error' => false, 'data' => $domains]);
|
|
}
|
|
|
|
public function getDnsDomains(string $uuid): JsonResponse
|
|
{
|
|
$customer = $this->resolveCustomer($uuid);
|
|
|
|
$domains = DnsDomain::query()
|
|
->with(['server:id,name,ip_address', 'registrar:id,name,short_name,website'])
|
|
->withCount(['ns', 'record'])
|
|
->whereHas('customer', fn($q) => $q->where('customers.id', $customer->id))
|
|
->orderByDesc('id')
|
|
->get()
|
|
->map(fn($d) => [
|
|
'id' => $d->id,
|
|
'domain_name' => $d->domain_name,
|
|
'is_created' => (int) $d->is_created,
|
|
'plesk_id' => $d->plesk_id,
|
|
'ns_count' => (int) ($d->ns_count ?? 0),
|
|
'record_count' => (int) ($d->record_count ?? 0),
|
|
'server' => $d->server ? [
|
|
'id' => $d->server->id,
|
|
'name' => $d->server->name,
|
|
'ip_address' => $d->server->ip_address,
|
|
] : null,
|
|
'registrar' => $d->registrar ? [
|
|
'id' => $d->registrar->id,
|
|
'name' => $d->registrar->name,
|
|
'short_name' => $d->registrar->short_name,
|
|
'website' => $d->registrar->website,
|
|
] : null,
|
|
'created_at' => $d->created_at,
|
|
'updated_at' => $d->updated_at,
|
|
]);
|
|
|
|
return response()->json(['error' => false, 'data' => $domains]);
|
|
}
|
|
|
|
public function getDnsDomainRecords(string $uuid, int $domainId): JsonResponse
|
|
{
|
|
$customer = $this->resolveCustomer($uuid);
|
|
|
|
$owns = DB::table('dns_domain_user')
|
|
->where('domain_id', $domainId)
|
|
->where('user_id', $customer->id)
|
|
->exists();
|
|
|
|
abort_if(!$owns, 403, 'Domain không thuộc về khách hàng này');
|
|
|
|
$records = DnsRecord::query()
|
|
->where('dns_manager_domain_id', $domainId)
|
|
->orderBy('type')
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
return response()->json(['error' => false, 'data' => $records]);
|
|
}
|
|
}
|