Skip to content

Commit

Permalink
SCRUM-84 refact: seller객체와 role db에서 null 가능하게 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareyong committed Sep 8, 2024
1 parent 66fa325 commit aab92e8
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,10 @@ public class AuthController {

private final AuthService1 authService;

// 해당하는 유저에다가 추가적인 정보 저장하기

// @Operation(summary = "회원가입 추가정보 진행", description = "(nickname, gender) 저장")
// @PostMapping("/join")
// public ApiResponse<String> join(@RequestBody AuthRequest authRequest, @AuthenticationPrincipal CustomOAuth2User customUser) {
// String username = customUser.getUsername();
//
// // 1. UserService를 통해 (nickname, gender) 저장
// authService.saveUserDetails(username, authRequest);
//
// // 2. 기존 UserDTO를 업데이트
// UserDTO updatedUserDTO = customUser.getUserDTO();
// updatedUserDTO.setNickname(authRequest.nickName());
// updatedUserDTO.setGender(authRequest.gender());
//
//
// // 3. CustomOAuth2User 객체 업데이트 (UserEntity 유지)
// UserEntity userEntity = customUser.getUserEntity(); // 기존 UserEntity 유지
// CustomOAuth2User updatedCustomOAuth2User = new CustomOAuth2User(updatedUserDTO, userEntity);
//
// Authentication newAuth = new UsernamePasswordAuthenticationToken(updatedCustomOAuth2User, null, updatedCustomOAuth2User.getAuthorities());
//
// // 5. SecurityContextHolder에 새로운 Authentication 객체로 업데이트
// SecurityContextHolder.getContext().setAuthentication(newAuth);
//
// return ApiResponse.onSuccess("회원가입 성공");
// }

@Operation(summary = "회원가입 추가정보 진행", description = "(nickname, gender, role) 저장")
@PostMapping("/join")
public ApiResponse<String> join(@RequestBody AuthRequest authRequest, @AuthenticationPrincipal CustomOAuth2User customUser) {
String username = customUser.getUsername();
// String username = customUser.getUsername();

// 1. UserEntity 가져오기
UserEntity userEntity = customUser.getUserEntity();
Expand All @@ -83,15 +55,12 @@ public ApiResponse<String> join(@RequestBody AuthRequest authRequest, @Authentic
.address(authRequest.address())
.build();
userEntity.setBuyer(buyer); // UserEntity에 Buyer 설정
// } else if ("SELLER".equalsIgnoreCase(authRequest.role())) {
// Seller seller = Seller.builder()
// .user(userEntity)
// .businessName(authRequest.address()) // Seller의 추가 정보
// .role(Role.SELLER)
// .build();
// userEntity.setSeller(seller); // UserEntity에 Seller 설정
// Authentication newAuth = new UsernamePasswordAuthenticationToken(seller, null, customUser.getAuthorities());
// SecurityContextHolder.getContext().setAuthentication(newAuth);
} else if (Role.SELLER == authRequest.role()) {
Seller seller = Seller.builder()
.user(userEntity)
.address(authRequest.address())
.build();
userEntity.setSeller(seller); // UserEntity에 Seller 설정
} else {
return ApiResponse.onFailure("Invalid role");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public class UserEntity implements UserDetails {
private Gender gender = Gender.valueOf("MALE");

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Role role = Role.valueOf("BUYER");
// @Column(nullable = false)
// private Role role = Role.valueOf("BUYER");
private Role role;

// 추가
private String refreshToken; // JWT 리프레시 토큰 발급
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,9 @@ public class BuyerController {

private final BuyerService buyerService;

// @Operation(summary = "구매자 마이페이지 조회", description = "구매자의 정보를 조회합니다.")
// @GetMapping("/mypage")
// public ApiResponse<BuyerResponse> getMyPage(@AuthenticationPrincipal CustomOAuth2User customUser) {
//
// UserEntity userEntity = customUser.getUserEntity();
// BuyerResponse buyerResponse = BuyerService.getMyPage(userEntity); // Buyer 객체를 BuyerService로 넘겨서 BuyerResponse 생성
// return ApiResponse.onSuccess(buyerResponse);
// }

@Operation(summary = "구매자 마이페이지 조회", description = "구매자의 정보를 조회합니다.")
@GetMapping("/mypage")
public ApiResponse<BuyerResponse> getMyPage(@AuthenticationPrincipal Buyer buyer) {

// UserEntity userEntity = customUser.getUserEntity();
// BuyerResponse buyerResponse = BuyerService.getMyPage(userEntity); // Buyer 객체를 BuyerService로 넘겨서 BuyerResponse 생성
BuyerResponse buyerResponse = BuyerService.getMyPage(buyer);
return ApiResponse.onSuccess(buyerResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ public class Buyer extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long buyerId;
private String address; // @Column(nullable = false)

@OneToOne // Buyer는 하나의 UserEntity와만 연결됩니다.
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;

private String address; // @Column(nullable = false)

// @Enumerated(EnumType.STRING)
// @Column(nullable = false)
// private Role role = Role.valueOf("BUYER");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.kakaoteck.golagola.domain.buyer.dto.BuyerResponse;
import com.kakaoteck.golagola.domain.buyer.entity.Buyer;
import com.kakaoteck.golagola.domain.buyer.repository.BuyerRepository;
import com.kakaoteck.golagola.domain.seller.entity.Seller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -21,15 +22,6 @@ public class BuyerService {
private final UserRepository userRepository;

public static BuyerResponse getMyPage(Buyer buyer) {
// return BuyerResponse.builder()
// .email(userEntity.getEmail())
// .role(userEntity.getRole())
// .address(userEntity.getBuyer().getAddress())
// .realName(userEntity.getName())
// .gender(userEntity.getGender())
// .phoneNum(userEntity.getPhoneNum())
// .nickname(userEntity.getNickname())
// .build();
return BuyerResponse.builder()
.email(buyer.getUser().getEmail())
.role(buyer.getUser().getRole())
Expand All @@ -41,41 +33,21 @@ public static BuyerResponse getMyPage(Buyer buyer) {
.build();
}

// public BuyerResponse updateMyPage(@AuthenticationPrincipal CustomOAuth2User customUser, BuyerRequest.MyPagePutDto request) {
// Buyer buyer = buyerRepository.findByUser(userEntity)
// .orElseThrow(() -> new IllegalArgumentException("Buyer not found"));
//
// buyer.updateProfile(request);
//
// // 3. Buyer 및 UserEntity 정보 저장
// buyerRepository.save(buyer);
// userRepository.save(userEntity);
//
// return BuyerResponse.builder()
// .email(userEntity.getEmail())
// .role(userEntity.getRole())
// .address(userEntity.getBuyer().getAddress())
// .realName(userEntity.getName())
// .gender(userEntity.getGender())
// .phoneNum(userEntity.getPhoneNum())
// .nickname(userEntity.getNickname())
// .build();
// }
public BuyerResponse updateMyPage(Buyer buyer, BuyerRequest.MyPagePutDto request) {
buyer.updateProfile(request);

// 3. Buyer 및 UserEntity 정보 저장
buyerRepository.save(buyer);
// Buyer 및 UserEntity 정보 저장
Buyer savedBuyer = buyerRepository.save(buyer);
userRepository.save(buyer.getUser());

return BuyerResponse.builder()
.email(buyer.getUser().getEmail())
.role(buyer.getUser().getRole())
.address(buyer.getAddress())
.realName(buyer.getUser().getName())
.gender(buyer.getUser().getGender())
.phoneNum(buyer.getUser().getPhoneNum())
.nickname(buyer.getUser().getNickname())
.email(savedBuyer.getUser().getEmail())
.role(savedBuyer.getUser().getRole())
.address(savedBuyer.getAddress())
.realName(savedBuyer.getUser().getName())
.gender(savedBuyer.getUser().getGender())
.phoneNum(savedBuyer.getUser().getPhoneNum())
.nickname(savedBuyer.getUser().getNickname())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,15 @@
@NoArgsConstructor
@AllArgsConstructor
@Getter
//@SuperBuilder
@Builder
@Table(name = "seller_table")
//@DiscriminatorValue("SELLER")
public class Seller extends BaseEntity{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long sellerId;

private String address; // @Column(nullable = false)

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Role role = Role.valueOf("SELLER");

@OneToOne // Seller는 하나의 UserEntity와만 연결됩니다.
@JoinColumn(name = "user_id", nullable = false)
private UserEntity user;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kakaoteck.golagola.domain.seller.service;

import com.kakaoteck.golagola.domain.auth.Repository.UserRepository;
import com.kakaoteck.golagola.domain.seller.dto.SellerRequest;
import com.kakaoteck.golagola.domain.seller.dto.SellerResponse;
import com.kakaoteck.golagola.domain.seller.entity.Seller;
Expand All @@ -16,13 +17,13 @@
public class SellerService {

private final SellerRepository sellerRepository;
private final UserRepository userRepository;

public SellerResponse getMyPage(Seller seller) {
return SellerResponse.builder()
.email(seller.getUser().getEmail())
.role(seller.getRole())
.role(seller.getUser().getRole())
.address(seller.getAddress())
// .registerDate(seller.getRegisterDate())
.realName(seller.getUser().getName())
.gender(seller.getUser().getGender())
.phoneNum(seller.getUser().getPhoneNum())
Expand All @@ -33,12 +34,15 @@ public SellerResponse getMyPage(Seller seller) {

public SellerResponse updateMyPage(Seller seller, SellerRequest.MyPagePutDto request) {
seller.updateProfile(request);

// Seller 및 UserEntity 정보 저장
Seller savedSeller = sellerRepository.save(seller);
userRepository.save(seller.getUser());

return SellerResponse.builder()
.email(savedSeller.getUser().getEmail())
.role(savedSeller.getRole())
.role(savedSeller.getUser().getRole())
.address(savedSeller.getAddress())
// .registerDate(savedSeller.getRegisterDate())
.realName(savedSeller.getUser().getName())
.gender(savedSeller.getUser().getGender())
.phoneNum(savedSeller.getUser().getPhoneNum())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,18 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
// userDTO.setRole(role); // buyer, seller받아오는걸로 바꾸기

Authentication authToken = null;
CustomOAuth2User customOAuth2User = new CustomOAuth2User(userDTO, userEntity); // UserDetails에 회원 정보 객체 담기
// 연관된 1대1 매핑에 값이 존재할 때(CustomOAuth2User, buyer, seller)로 받기
if (userEntity.getRole() == Role.BUYER) {
Buyer buyer = userEntity.getBuyer(); // 1:1 매핑된 Buyer 가져오기

CustomOAuth2User customOAuth2User = new CustomOAuth2User(userDTO, userEntity);
authToken = new UsernamePasswordAuthenticationToken(buyer, null, customOAuth2User.getAuthoritiesForRole(Role.BUYER)); // Buyer에 맞는 권한 설정
}
else if (userEntity.getRole() == Role.SELLER){
Seller seller = userEntity.getSeller(); // 1:1 매핑된 Seller 가져오기

CustomOAuth2User customOAuth2User = new CustomOAuth2User(userDTO, userEntity);
authToken = new UsernamePasswordAuthenticationToken(seller, null, customOAuth2User.getAuthoritiesForRole(Role.SELLER)); // Seller에 맞는 권한 설정
}
else{
CustomOAuth2User customOAuth2User = new CustomOAuth2User(userDTO, userEntity); // UserDetails에 회원 정보 객체 담기
System.out.println("여기 걸리냐/?!!!!!!");
authToken = new UsernamePasswordAuthenticationToken(customOAuth2User, null, customOAuth2User.getAuthorities()); // 스프링 시큐리티 인증 토큰 생성, 스프링 시큐리티에서 세션을 생성해가지고 토큰을 등록하고 있음.
}
SecurityContextHolder.getContext().setAuthentication(authToken); // 세션에 사용자 등록
Expand Down

0 comments on commit aab92e8

Please sign in to comment.