refactor: refactor controllers, services, validation and introduce DTOs, FormRequests and domain exceptions

This commit is contained in:
antv
2026-07-09 10:27:47 +07:00
parent 54048bb4ff
commit 326e1f1028
15 changed files with 501 additions and 150 deletions
+70 -50
View File
@@ -3,102 +3,122 @@
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Models\Administration;
use App\Http\Requests\User\SendThankCardRequest;
use App\Http\Requests\User\UpdateProfileRequest;
use App\Http\Requests\User\UpdatePasswordRequest;
use App\DTOs\DashboardDataDto;
use App\Exceptions\ThankCardException;
use App\Services\User\Contracts\UserServiceInterface;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
class UserController extends Controller
{
public function __construct(
private UserServiceInterface $userService
private readonly UserServiceInterface $userService
) {}
public function index(Request $request)
/**
* Display the user dashboard with transaction history, stats, and rankings.
*/
public function index(Request $request): View
{
$selectedMonth = $request->input('month', Carbon::now()->format('Y-m'));
$selectedMonth = (string) $request->input('month', Carbon::now()->format('Y-m'));
$user = Auth::user();
$administrations = $this->userService->getDashboard($user, $selectedMonth);
$stats = $this->userService->getPersonalStats($user, $selectedMonth);
$stats = $this->userService->getPersonalStats($user, $selectedMonth);
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
return view('user.dashboard', array_merge([
'administrations' => $administrations,
'selectedMonth' => $selectedMonth,
'user' => $user,
'receivedRankers' => $receivedRanking['rankers'],
'receivedCurrentUserRankItem' => $receivedRanking['currentUserRankItem'],
'sentRankers' => $sentRanking['rankers'],
'sentCurrentUserRankItem' => $sentRanking['currentUserRankItem']
], $stats));
$dashboardData = new DashboardDataDto(
administrations: $administrations,
selectedMonth: $selectedMonth,
user: $user,
receivedRankers: $receivedRanking['rankers'],
receivedCurrentUserRankItem: $receivedRanking['currentUserRankItem'],
sentRankers: $sentRanking['rankers'],
sentCurrentUserRankItem: $sentRanking['currentUserRankItem'],
receivedCount: (int) $stats['receivedCount'],
sentCount: (int) $stats['sentCount'],
currentRank: (int) $stats['currentRank']
);
return view('user.dashboard', $dashboardData->toArray());
}
public function sendThankcards()
/**
* Show the form to send thank cards.
*/
public function sendThankcards(): View
{
$users = $this->userService->getOtherActiveUsers(Auth::user());
return view('user.send', compact('users'));
}
public function storeThankcards(Request $request)
/**
* Store and execute sending thank cards to another user.
*/
public function storeThankcards(SendThankCardRequest $request): JsonResponse
{
$request->validate([
'receiver' => [
'required',
\Illuminate\Validation\Rule::exists('user', 'msnv')->where('status', config('constants.STATUS_ACTIVE'))
],
'amount' => 'required|integer|min:1|max:' . Administration::MAX_SEND_CARD_PER_MONTH,
]);
try {
$this->userService->sendThankcards(
Auth::user(),
$request->receiver,
(int) $request->amount
(string) $request->input('receiver'),
(int) $request->input('amount')
);
} catch (\RuntimeException $e) {
} catch (ThankCardException $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()], 422);
}
return response()->json(['success' => true, 'message' => __('messages.thank_card_send_success')]);
}
public function changePasswordForm()
/**
* Show the change password form.
*/
public function changePasswordForm(): View
{
return view('user.change_password');
}
// Show edit profile page (avatar & password)
public function editProfile()
/**
* Handle user password change.
*/
public function updatePassword(UpdatePasswordRequest $request): RedirectResponse
{
$user = Auth::user();
$this->userService->updatePassword($user, $request->input('password'));
$route = $user->role == config('constants.ROLE_ADMIN') ? 'admin.dashboard' : 'user.dashboard';
return redirect()->route($route)->with('success', __('messages.password_change_success'));
}
/**
* Show the edit profile page (avatar & password).
*/
public function editProfile(): View
{
return view('user.edit');
}
// Handle avatar upload and optional password change
public function updateProfile(Request $request)
/**
* Handle avatar upload and optional password change.
*/
public function updateProfile(UpdateProfileRequest $request): RedirectResponse
{
$request->validate([
'avatar' => 'nullable|image|max:2048', // 2MB max
'new_password' => 'nullable|min:6|confirmed',
]);
$user = Auth::user();
// Avatar handling
if ($request->hasFile('avatar')) {
$path = $request->file('avatar')->store('avatars', 'public');
$user->avatar = 'storage/' . $path;
}
// Password handling (optional)
if ($request->filled('new_password')) {
$this->userService->updatePassword($user, $request->new_password);
}
$user->save();
$this->userService->updateProfile(
$user,
$request->file('avatar'),
$request->filled('new_password') ? $request->input('new_password') : null
);
return redirect()->route('user.edit')->with('success', 'Cập nhật hồ sơ thành công');
}