126 lines
4.0 KiB
PHP
126 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
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 readonly UserServiceInterface $userService
|
|
) {}
|
|
|
|
/**
|
|
* Display the user dashboard with transaction history, stats, and rankings.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
$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);
|
|
|
|
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
|
|
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
|
|
|
|
$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());
|
|
}
|
|
|
|
/**
|
|
* Show the form to send thank cards.
|
|
*/
|
|
public function sendThankcards(): View
|
|
{
|
|
$users = $this->userService->getOtherActiveUsers(Auth::user());
|
|
return view('user.send', compact('users'));
|
|
}
|
|
|
|
/**
|
|
* Store and execute sending thank cards to another user.
|
|
*/
|
|
public function storeThankcards(SendThankCardRequest $request): JsonResponse
|
|
{
|
|
try {
|
|
$this->userService->sendThankcards(
|
|
Auth::user(),
|
|
(string) $request->input('receiver'),
|
|
(int) $request->input('amount')
|
|
);
|
|
} catch (ThankCardException $e) {
|
|
return response()->json(['success' => false, 'message' => $e->getMessage()], 422);
|
|
}
|
|
|
|
return response()->json(['success' => true, 'message' => __('messages.thank_card_send_success')]);
|
|
}
|
|
|
|
/**
|
|
* Show the change password form.
|
|
*/
|
|
public function changePasswordForm(): View
|
|
{
|
|
return view('user.change_password');
|
|
}
|
|
|
|
/**
|
|
* 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(UpdateProfileRequest $request): RedirectResponse
|
|
{
|
|
$user = Auth::user();
|
|
|
|
$this->userService->updateProfile(
|
|
$user,
|
|
$request->validated(),
|
|
$request->file('avatar')
|
|
);
|
|
|
|
return redirect()->route('user.edit')->with('success', 'Cập nhật hồ sơ thành công');
|
|
}
|
|
}
|