-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: 유저 정보 관련 API #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eunseobb
wants to merge
4
commits into
dev
Choose a base branch
from
feat/#18
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/sequence/anonymous/user/application/UserService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,40 @@ | ||
| package com.sequence.anonymous.user.application; | ||
|
|
||
| import com.sequence.anonymous.security.CustomOAuth2User; | ||
| import com.sequence.anonymous.user.domain.repository.UserRepository; | ||
| import com.sequence.anonymous.user.domain.user.Gender; | ||
| import com.sequence.anonymous.user.domain.user.User; | ||
| import com.sequence.anonymous.user.dto.InitializeRequest; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class UserService { | ||
|
|
||
| private final UserRepository userRepository; | ||
|
|
||
|
|
||
| public User findById(Long userId) { | ||
| return userRepository.findById(userId) | ||
| .orElseThrow(() -> new IllegalArgumentException("not found : " + userId)); | ||
| } | ||
|
|
||
| public User findByName(String userName) { | ||
| return userRepository.findByName(userName) | ||
| .orElseThrow(() -> new IllegalArgumentException("not found : " + userName)); | ||
| } | ||
|
|
||
| public void initializeProfile(Long userId, InitializeRequest initializeRequest){ | ||
|
|
||
|
|
||
| User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("not found : " + userId)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. findById().orElseThrow() 사이에 줄바꿈을 넣어주시면 더욱 가독성 좋은 코드가 될 것 같습니다! User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("not found : " + userId)); |
||
| user.initializeProfile(initializeRequest.getName(), initializeRequest.getAge(), initializeRequest.getGender()); | ||
| } | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/sequence/anonymous/user/dto/InitializeRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.sequence.anonymous.user.dto; | ||
|
|
||
| import com.sequence.anonymous.user.domain.user.Gender; | ||
| import com.sequence.anonymous.user.domain.user.User; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| public class InitializeRequest { | ||
| private String name; | ||
| private Integer age; | ||
| private Gender gender; | ||
|
|
||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/sequence/anonymous/user/dto/UserResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.sequence.anonymous.user.dto; | ||
|
|
||
| import com.sequence.anonymous.user.domain.user.Gender; | ||
| import com.sequence.anonymous.user.domain.user.OAuth2Provider; | ||
| import com.sequence.anonymous.user.domain.user.Role; | ||
| import com.sequence.anonymous.user.domain.user.User; | ||
| import lombok.Getter; | ||
|
|
||
|
|
||
| @Getter | ||
|
|
||
| public class UserResponse { | ||
|
|
||
| private final Long id; | ||
| private final String providerId; | ||
| private final OAuth2Provider provider; | ||
| private final String name; | ||
| private final Integer age; | ||
| private final Gender gender; | ||
| private final String email; | ||
| private final Role role; | ||
| private final Boolean withdrawal; | ||
|
|
||
| public UserResponse(User user){ | ||
| this.id = user.getId(); | ||
| this.providerId = user.getProviderId(); | ||
| this.provider = user.getProvider(); | ||
| this.name = user.getName(); | ||
| this.age = user.getAge(); | ||
| this.gender = user.getGender(); | ||
| this.email = user.getEmail(); | ||
| this.role = user.getRole(); | ||
| this.withdrawal = user.getWithdrawal(); | ||
| } | ||
| } |
48 changes: 46 additions & 2 deletions
48
src/main/java/com/sequence/anonymous/user/presentation/UserController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,58 @@ | ||
| package com.sequence.anonymous.user.presentation; | ||
|
|
||
| import com.sequence.anonymous.security.CustomOAuth2User; | ||
| import com.sequence.anonymous.user.application.UserService; | ||
| import com.sequence.anonymous.user.domain.user.Gender; | ||
| import com.sequence.anonymous.user.domain.user.User; | ||
| import com.sequence.anonymous.user.dto.InitializeRequest; | ||
| import com.sequence.anonymous.user.dto.UserResponse; | ||
| import jakarta.transaction.Transactional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
|
|
||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @Transactional | ||
| @RequiredArgsConstructor | ||
| @RestController | ||
| @RequestMapping("/users") | ||
| public class UserController { | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| @GetMapping("/me") | ||
| public ResponseEntity<UserResponse> findMyInfo(@AuthenticationPrincipal CustomOAuth2User user){ | ||
| Long currentUserId = user.getId(); | ||
| User currentUser = userService.findById(currentUserId); | ||
|
|
||
| return ResponseEntity.ok(new UserResponse((User) currentUser)); | ||
|
|
||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<UserResponse> findUser(@RequestParam(name = "id", required = false) Long id, | ||
| @RequestParam(name = "name", required = false) String name) { | ||
| if (id != null) { | ||
| User user = userService.findById(id); | ||
| return ResponseEntity.ok(new UserResponse(user)); | ||
| } | ||
|
|
||
| else if (name != null) { | ||
| User user = userService.findByName(name); | ||
| return ResponseEntity.ok(new UserResponse(user)); | ||
| } | ||
|
|
||
| else { | ||
| throw new IllegalArgumentException("Unexpected user"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @PostMapping("/profiles") | ||
| public void initializeProfile(@RequestBody InitializeRequest initializeRequest, @AuthenticationPrincipal CustomOAuth2User user) { | ||
| userService.initializeProfile(user.getId(), initializeRequest); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.