update
This commit is contained in:
+138
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\DomainFluctuation\App\Http\Controllers\Api\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\DomainFluctuation\App\Http\Requests\IndexDomainFluctuationRequest;
|
||||
use Modules\DomainFluctuation\App\Http\Requests\StoreDomainFluctuationRequest;
|
||||
use Modules\DomainFluctuation\App\Http\Requests\UpdateDomainFluctuationRequest;
|
||||
use Modules\DomainFluctuation\App\Http\Resources\DomainFluctuationResource;
|
||||
use Modules\DomainFluctuation\App\Models\DomainFluctuation;
|
||||
|
||||
class DomainFluctuationController extends Controller
|
||||
{
|
||||
public function index(IndexDomainFluctuationRequest $request): JsonResponse
|
||||
{
|
||||
$filter = $request->toDTO();
|
||||
|
||||
$query = DomainFluctuation::query()->with('term');
|
||||
|
||||
if ($filter->termId) {
|
||||
$query->where('term_id', $filter->termId);
|
||||
}
|
||||
|
||||
if ($filter->keyword) {
|
||||
$kw = '%' . $filter->keyword . '%';
|
||||
$query->where(function ($q) use ($kw) {
|
||||
$q->where('domainName', 'like', $kw)
|
||||
->orWhere('ownerName', 'like', $kw)
|
||||
->orWhere('email', 'like', $kw)
|
||||
->orWhere('phone', 'like', $kw);
|
||||
});
|
||||
}
|
||||
|
||||
if ($filter->ownerType) {
|
||||
$query->where('ownerType', $filter->ownerType);
|
||||
}
|
||||
|
||||
if ($filter->status !== null) {
|
||||
$query->where('status', $filter->status);
|
||||
}
|
||||
|
||||
if ($filter->action) {
|
||||
$query->where('action', $filter->action);
|
||||
}
|
||||
|
||||
if ($filter->from) {
|
||||
$query->where('registeredDate', '>=', $filter->from);
|
||||
}
|
||||
|
||||
if ($filter->to) {
|
||||
$query->where('registeredDate', '<=', $filter->to);
|
||||
}
|
||||
|
||||
$query->orderBy($filter->sortBy, $filter->sortDir);
|
||||
|
||||
$paginator = $query->paginate($filter->perPage, ['*'], 'page', $filter->page);
|
||||
|
||||
return DomainFluctuationResource::collection($paginator)->response();
|
||||
}
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$domain = DomainFluctuation::with('term')->find($id);
|
||||
|
||||
if (! $domain) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Không tìm thấy tên miền',
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => DomainFluctuationResource::make($domain),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(StoreDomainFluctuationRequest $request): JsonResponse
|
||||
{
|
||||
$dto = $request->toDTO();
|
||||
|
||||
$domain = DB::transaction(fn () => DomainFluctuation::create($dto->toArray()));
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Tạo tên miền thành công',
|
||||
'data' => DomainFluctuationResource::make($domain),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(int $id, UpdateDomainFluctuationRequest $request): JsonResponse
|
||||
{
|
||||
$domain = DomainFluctuation::find($id);
|
||||
|
||||
if (! $domain) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Không tìm thấy tên miền',
|
||||
], 404);
|
||||
}
|
||||
|
||||
try {
|
||||
DB::transaction(fn () => $domain->update($request->toDTO()->toArray()));
|
||||
} catch (\Throwable $e) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Cập nhật tên miền thành công',
|
||||
'data' => DomainFluctuationResource::make($domain->fresh('term')),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
$domain = DomainFluctuation::find($id);
|
||||
|
||||
if (! $domain) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Không tìm thấy tên miền',
|
||||
], 404);
|
||||
}
|
||||
|
||||
$domain->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Xoá tên miền thành công',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user