Skip to content
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

Feat: 유저 정보 관련 API #21

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

import lombok.Getter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.user.OAuth2User;

@Getter

public class CustomOAuth2User implements OAuth2User, Serializable {

private final Long id;
Expand Down Expand Up @@ -70,4 +74,6 @@ public String toString() {
.append("[OAuth2UserInfo]")
.toString();
}


}
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
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 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 List<User> findMyInfo(){
return userRepository.findAll();
}

public User findById(Long userId) {
return userRepository.findById(userId)
Expand All @@ -28,6 +27,5 @@ public User findByName(String userName) {
.orElseThrow(() -> new IllegalArgumentException("not found : " + userName));
}

public void initializeProfile(String name, Integer age, Gender gender){
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sequence.anonymous.user.domain.repository;

import com.sequence.anonymous.user.domain.user.User;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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.UserResponse;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.core.annotation.AuthenticationPrincipal;

import java.security.Principal;
import java.util.List;
import org.springframework.web.bind.annotation.*;

@Transactional
@RequiredArgsConstructor
@RestController
@RequestMapping("/users")
Expand All @@ -20,26 +21,25 @@ public class UserController {
private final UserService userService;

@GetMapping("/me")
public ResponseEntity<UserResponse> findMyInfo(Principal principal){
List<User> currentUser = userService.findMyInfo();
public ResponseEntity<UserResponse> findMyInfo(@AuthenticationPrincipal CustomOAuth2User user){
Long currentUserId = user.getId();
User currentUser = userService.findById(currentUserId);

return ResponseEntity.ok(new UserResponse((User) currentUser));

return ResponseEntity.ok()
.body(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()
.body(new UserResponse(user));
return ResponseEntity.ok(new UserResponse(user));
}

else if (name != null) {
User user = userService.findByName(name);
return ResponseEntity.ok()
.body(new UserResponse(user));
return ResponseEntity.ok(new UserResponse(user));
ckyeon marked this conversation as resolved.
Show resolved Hide resolved
}

else {
Expand All @@ -48,23 +48,10 @@ else if (name != null) {
}


/*@GetMapping("/{id}")
public ResponseEntity<UserResponse> findUserById(@PathVariable Long id){
User user = userService.findById(id);
return ResponseEntity.ok()
.body(new UserResponse(user));
}
@GetMapping("/{name}")
public ResponseEntity<UserResponse> findUserByName(@PathVariable String name){
User user = userService.findByName(name);
return ResponseEntity.ok()
.body(new UserResponse(user));
}*/

@PostMapping("/profiles")
public void initializeProfile(String name, Integer age, Gender gender) {
Long currentUserId = 1L;

public void initializeProfile(User user, String name, Integer age, Gender gender) {
user.initializeProfile(name, age, gender);
ckyeon marked this conversation as resolved.
Show resolved Hide resolved
}

}