feature: form edit
This commit is contained in:
@@ -64,7 +64,7 @@ class AdminController extends Controller
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('admin.users.create');
|
||||
return view('admin.users.form', ['mode' => 'create']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,21 +85,29 @@ class AdminController extends Controller
|
||||
$user = $this->adminService->getUserByMsnv($msnv);
|
||||
$selectedMonth = (string) $request->input('month', Carbon::now()->format('Y-m'));
|
||||
$administrations = $this->adminService->getUserTransactions($msnv, $selectedMonth);
|
||||
$addCards = $this->adminService->getAddCardsHistory($user->msnv);
|
||||
$addCards = $this->adminService->getAddCardsHistory($user->msnv, $selectedMonth);
|
||||
$admins = $this->adminService->getActiveAdmins();
|
||||
|
||||
return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins'));
|
||||
return view('admin.users.form', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins'))->with('mode', 'edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified user in storage.
|
||||
*/
|
||||
public function update(UpdateUserRequest $request, string $msnv): RedirectResponse
|
||||
public function update(UpdateUserRequest $request, string $msnv)
|
||||
{
|
||||
$user = $this->adminService->getUserByMsnv($msnv);
|
||||
|
||||
$this->adminService->updateUser($user, $request->validated());
|
||||
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => __('messages.user_update_success'),
|
||||
'card_balance' => $user->fresh()->card,
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', __('messages.user_update_success'));
|
||||
}
|
||||
|
||||
@@ -116,12 +124,24 @@ class AdminController extends Controller
|
||||
/**
|
||||
* Update card allocation history record.
|
||||
*/
|
||||
public function updateAddCard(UpdateAddCardRequest $request, int $id): RedirectResponse
|
||||
public function updateAddCard(UpdateAddCardRequest $request, int $id)
|
||||
{
|
||||
try {
|
||||
$this->adminService->updateAddCard($id, $request->validated());
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Cập nhật lịch sử cấp phát thẻ thành công.',
|
||||
]);
|
||||
}
|
||||
return redirect()->back()->with('success', 'Cập nhật lịch sử cấp phát thẻ thành công.');
|
||||
} catch (\Exception $e) {
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage(),
|
||||
], 422);
|
||||
}
|
||||
return redirect()->back()->withErrors(['error' => $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,11 +219,18 @@ class AdminService implements AdminServiceInterface
|
||||
return User::where('msnv', $msnv)->firstOrFail();
|
||||
}
|
||||
|
||||
public function getAddCardsHistory(string $buyerMsnv): \Illuminate\Database\Eloquent\Collection
|
||||
public function getAddCardsHistory(string $buyerMsnv, ?string $selectedMonth = null): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return AddCard::with('sellerUser')
|
||||
->where('buyer', $buyerMsnv)
|
||||
->orderBy('date', 'desc')
|
||||
$query = AddCard::with('sellerUser')
|
||||
->where('buyer', $buyerMsnv);
|
||||
|
||||
if ($selectedMonth) {
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth()->format('Y-m-d');
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth()->format('Y-m-d');
|
||||
$query->whereBetween('date', [$startOfMonth, $endOfMonth]);
|
||||
}
|
||||
|
||||
return $query->orderBy('date', 'desc')
|
||||
->orderBy('id', 'desc')
|
||||
->get();
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ interface AdminServiceInterface
|
||||
|
||||
public function getUserByMsnv(string $msnv): \App\Models\User;
|
||||
|
||||
public function getAddCardsHistory(string $buyerMsnv): \Illuminate\Database\Eloquent\Collection;
|
||||
public function getAddCardsHistory(string $buyerMsnv, ?string $selectedMonth = null): \Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
public function getActiveAdmins(): \Illuminate\Database\Eloquent\Collection;
|
||||
}
|
||||
Reference in New Issue
Block a user