-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUserController.java
More file actions
52 lines (43 loc) · 2.26 KB
/
UserController.java
File metadata and controls
52 lines (43 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.cleanengine.coin.user.info.presentation;
import com.cleanengine.coin.common.response.ApiResponse;
import com.cleanengine.coin.common.response.ErrorResponse;
import com.cleanengine.coin.common.response.ErrorStatus;
import com.cleanengine.coin.user.domain.Account;
import com.cleanengine.coin.user.domain.Wallet;
import com.cleanengine.coin.user.info.application.AccountService;
import com.cleanengine.coin.user.info.application.WalletService;
import com.cleanengine.coin.user.info.application.UserService;
import com.cleanengine.coin.user.login.infra.CustomOAuth2User;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
private final UserService userService;
private final AccountService accountService;
private final WalletService walletService;
public UserController(UserService userService, AccountService accountService, WalletService walletService) {
this.userService = userService;
this.accountService = accountService;
this.walletService = walletService;
}
@GetMapping("/api/userinfo")
public ApiResponse<UserInfoDTO> retrieveUserInfo() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getPrincipal() instanceof CustomOAuth2User oAuth2User) {
Integer userId = oAuth2User.getUserId();
UserInfoDTO userInfoDTO = userService.retrieveUserInfoByUserId(userId);
if (userInfoDTO == null) {
return ApiResponse.fail(ErrorResponse.of(ErrorStatus.UNAUTHORIZED_RESOURCE));
}
Account account = accountService.retrieveAccountByUserId(userId);
List<Wallet> wallets = walletService.retrieveWalletsByAccountId(account.getId());
userInfoDTO.setWallets(wallets);
return ApiResponse.success(userInfoDTO, HttpStatus.OK);
}
return ApiResponse.fail(ErrorResponse.of(ErrorStatus.UNAUTHORIZED_RESOURCE));
}
}