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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\DomainFluctuation\App\Http\Controllers\Api\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\DomainFluctuation\App\Http\Requests\StoreTermRequest;
|
||||
use Modules\DomainFluctuation\App\Http\Resources\DomainFluctuationTermResource;
|
||||
use Modules\DomainFluctuation\App\Models\DomainFluctuationTerm;
|
||||
use Modules\DomainFluctuation\App\Models\TermFluctuationType;
|
||||
|
||||
class TermController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = (int) $request->input('per_page', 20);
|
||||
|
||||
$terms = DomainFluctuationTerm::query()
|
||||
->with('type')
|
||||
->withCount('fluctuations')
|
||||
->orderByDesc('id')
|
||||
->paginate($perPage);
|
||||
|
||||
return DomainFluctuationTermResource::collection($terms)->response();
|
||||
}
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$term = DomainFluctuationTerm::with('type')->withCount('fluctuations')->find($id);
|
||||
|
||||
if (! $term) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Không tìm thấy đợt khai báo',
|
||||
], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => DomainFluctuationTermResource::make($term),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(StoreTermRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) ($request->user()?->id ?? 0);
|
||||
$dto = $request->toDTO($userId);
|
||||
|
||||
$term = DB::transaction(function () use ($dto, $request) {
|
||||
$term = DomainFluctuationTerm::create($dto->toArray());
|
||||
|
||||
TermFluctuationType::create([
|
||||
'term_fluctuation_id' => $term->id,
|
||||
'type' => $request->input('type', 'vnnic_06'),
|
||||
]);
|
||||
|
||||
return $term->load('type');
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Tạo đợt khai báo thành công',
|
||||
'data' => DomainFluctuationTermResource::make($term),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function update(int $id, StoreTermRequest $request): JsonResponse
|
||||
{
|
||||
$term = DomainFluctuationTerm::find($id);
|
||||
|
||||
if (! $term) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Không tìm thấy đợt khai báo',
|
||||
], 404);
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($term, $request) {
|
||||
$term->update($request->only(['term_name', 'description', 'date_create', 'status']));
|
||||
|
||||
if ($request->filled('type')) {
|
||||
TermFluctuationType::updateOrCreate(
|
||||
['term_fluctuation_id' => $term->id],
|
||||
['type' => $request->input('type')],
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Cập nhật đợt khai báo thành công',
|
||||
'data' => DomainFluctuationTermResource::make($term->fresh('type')),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(int $id): JsonResponse
|
||||
{
|
||||
$term = DomainFluctuationTerm::find($id);
|
||||
|
||||
if (! $term) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Không tìm thấy đợt khai báo',
|
||||
], 404);
|
||||
}
|
||||
|
||||
$term->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Xoá đợt khai báo thành công',
|
||||
]);
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\DomainFluctuation\App\Http\Controllers\Api\Client;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Modules\DomainFluctuation\App\Http\Requests\IndexDomainFluctuationRequest;
|
||||
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')
|
||||
->where('status', 1);
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
$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')->where('status', 1)->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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user