Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 authored Nov 19, 2024
2 parents 25cb8d0 + 54fd78b commit 4b89797
Show file tree
Hide file tree
Showing 26 changed files with 591 additions and 555 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -16,7 +16,7 @@
import com.jangburich.domain.menu.domain.MenuGetResponseDTO;
import com.jangburich.domain.menu.domain.MenuUpdateRequestDTO;
import com.jangburich.domain.menu.domain.service.MenuService;
import com.jangburich.domain.oauth.domain.CustomOAuthUser;
import com.jangburich.global.GetAuthorization;
import com.jangburich.global.payload.Message;
import com.jangburich.global.payload.ResponseCustom;

Expand All @@ -32,32 +32,32 @@ public class MenuController {
private final MenuService menuService;

@PostMapping("/register")
public ResponseCustom<Message> registerMenu(Authentication authentication,
public ResponseCustom<Message> registerMenu(
@RequestAttribute(value = "authorizationHeader") String authorizationHeader,
@RequestBody MenuCreateRequestDTO menuCreateRequestDTO) {
CustomOAuthUser customOAuthUser = (CustomOAuthUser)authentication.getPrincipal();
menuService.registerMenu(customOAuthUser, menuCreateRequestDTO);
menuService.registerMenu(GetAuthorization.getUserId(authorizationHeader), menuCreateRequestDTO);
return ResponseCustom.OK(Message.builder().message("success").build());
}

@PatchMapping("/update/{id}")
public ResponseCustom<Message> updateMenu(Authentication authentication, @PathVariable Long id,
public ResponseCustom<Message> updateMenu(
@RequestAttribute(value = "authorizationHeader") String authorizationHeader, @PathVariable Long id,
@RequestBody MenuUpdateRequestDTO menuUpdateRequestDTO) {
CustomOAuthUser customOAuthUser = (CustomOAuthUser)authentication.getPrincipal();
menuService.updateMenu(customOAuthUser, id, menuUpdateRequestDTO);
menuService.updateMenu(GetAuthorization.getUserId(authorizationHeader), id, menuUpdateRequestDTO);
return ResponseCustom.OK(Message.builder().message("success").build());
}

@DeleteMapping("/{id}")
public ResponseCustom<Message> deleteMenu(Authentication authentication, @PathVariable Long id) {
CustomOAuthUser customOAuthUser = (CustomOAuthUser)authentication.getPrincipal();
menuService.deleteMenu(customOAuthUser, id);
public ResponseCustom<Message> deleteMenu(
@RequestAttribute(value = "authorizationHeader") String authorizationHeader, @PathVariable Long id) {
menuService.deleteMenu(GetAuthorization.getUserId(authorizationHeader), id);
return ResponseCustom.OK(Message.builder().message("success").build());
}

@GetMapping("")
public ResponseCustom<List<MenuGetResponseDTO>> getMenu(Authentication authentication) {
CustomOAuthUser customOAuthUser = (CustomOAuthUser)authentication.getPrincipal();
List<MenuGetResponseDTO> menu = menuService.getMenu(customOAuthUser);
public ResponseCustom<List<MenuGetResponseDTO>> getMenu(
@RequestAttribute(value = "authorizationHeader") String authorizationHeader) {
List<MenuGetResponseDTO> menu = menuService.getMenu(GetAuthorization.getUserId(authorizationHeader));
return ResponseCustom.OK(menu);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
import com.jangburich.domain.menu.domain.MenuGetResponseDTO;
import com.jangburich.domain.menu.domain.MenuUpdateRequestDTO;
import com.jangburich.domain.menu.domain.repository.MenuRepository;
import com.jangburich.domain.oauth.domain.CustomOAuthUser;
import com.jangburich.domain.owner.domain.Owner;
import com.jangburich.domain.owner.domain.repository.OwnerRepository;
import com.jangburich.domain.store.domain.Store;
import com.jangburich.domain.store.domain.repository.StoreRepository;
import com.jangburich.domain.user.domain.User;
import com.jangburich.domain.user.domain.repository.UserRepository;
import com.jangburich.domain.user.repository.UserRepository;
import com.jangburich.global.error.DefaultNullPointerException;
import com.jangburich.global.payload.ErrorCode;

Expand All @@ -31,8 +30,8 @@ public class MenuService {
private final StoreRepository storeRepository;
private final UserRepository userRepository;

public void registerMenu(CustomOAuthUser customOAuthUser, MenuCreateRequestDTO menuCreateRequestDTO) {
User user = userRepository.findByProviderId(customOAuthUser.getUserId())
public void registerMenu(String customOAuthUser, MenuCreateRequestDTO menuCreateRequestDTO) {
User user = userRepository.findByProviderId(customOAuthUser)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

Owner owner = ownerRepository.findByUser(user)
Expand All @@ -45,10 +44,10 @@ public void registerMenu(CustomOAuthUser customOAuthUser, MenuCreateRequestDTO m
menuCreateRequestDTO.getImage_url(), menuCreateRequestDTO.getPrice(), store));
}

public void updateMenu(CustomOAuthUser customOAuthUser, Long menuId, MenuUpdateRequestDTO menuUpdateRequestDTO) {
public void updateMenu(String customOAuthUser, Long menuId, MenuUpdateRequestDTO menuUpdateRequestDTO) {
Menu menu = menuRepository.findById(menuId)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_PARAMETER));
if (!menu.getStore().getOwner().getUser().getProviderId().equals(customOAuthUser.getUserId())) {
if (!menu.getStore().getOwner().getUser().getProviderId().equals(customOAuthUser)) {
throw new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION);
}
if (menuUpdateRequestDTO.getName() != null)
Expand All @@ -64,17 +63,17 @@ public void updateMenu(CustomOAuthUser customOAuthUser, Long menuId, MenuUpdateR
}

@Transactional
public void deleteMenu(CustomOAuthUser customOAuthUser, Long id) {
public void deleteMenu(String customOAuthUser, Long id) {
Menu menu = menuRepository.findById(id)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_PARAMETER));
if (!menu.getStore().getOwner().getUser().getProviderId().equals(customOAuthUser.getUserId())) {
if (!menu.getStore().getOwner().getUser().getProviderId().equals(customOAuthUser)) {
throw new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION);
}
menuRepository.delete(menu);
}

public List<MenuGetResponseDTO> getMenu(CustomOAuthUser customOAuthUser) {
User user = userRepository.findByProviderId(customOAuthUser.getUserId())
public List<MenuGetResponseDTO> getMenu(String customOAuthUser) {
User user = userRepository.findByProviderId(customOAuthUser)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

Owner owner = ownerRepository.findByUser(user)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,113 +1,113 @@
package com.jangburich.domain.oauth.domain.service;

import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.jangburich.domain.oauth.domain.CustomOAuthUser;
import com.jangburich.domain.oauth.domain.KakaoResponse;
import com.jangburich.domain.oauth.domain.OAuth2Response;
import com.jangburich.domain.oauth.domain.OAuthUserDTO;
import com.jangburich.domain.owner.domain.Owner;
import com.jangburich.domain.owner.domain.repository.OwnerRepository;
import com.jangburich.domain.user.domain.User;
import com.jangburich.domain.user.domain.repository.UserRepository;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class OAuthUserService extends DefaultOAuth2UserService {
private final UserRepository userRepository;
private final OwnerRepository ownerRepository;

public OAuthUserService(UserRepository userRepository, OwnerRepository ownerRepository) {
this.userRepository = userRepository;
this.ownerRepository = ownerRepository;
}

@Override
public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(oAuth2UserRequest);
log.info("OAuth2User loaded: {}", oAuth2User.getName());

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String state = (String)request.getSession().getAttribute("oauth2_state");
request.getSession().removeAttribute("oauth2_state");

OAuth2Response oAuth2Response = new KakaoResponse(oAuth2User.getAttributes());

// String userId = oAuth2Response.getProvider() + "_" + oAuth2Response.getProviderId();
String userId = oAuth2Response.getProviderId();

if ("user".equals(state)) {
User existUser = userRepository.findByProviderId(userId).orElse(null);
if (existUser == null) {
User newUser = User.create(userId, oAuth2Response.getNickname(), oAuth2Response.getImage(),
"ROLE_USER");
userRepository.save(newUser);

OAuthUserDTO userDTO = new OAuthUserDTO();
userDTO.setUserId(userId);
userDTO.setNickname(oAuth2Response.getNickname());
userDTO.setImage(oAuth2Response.getImage());
userDTO.setRole("ROLE_USER");

return new CustomOAuthUser(userDTO);
} else {
existUser.setNickname(oAuth2Response.getNickname());
existUser.setProfileImageUrl(oAuth2Response.getImage());

userRepository.save(existUser);

OAuthUserDTO userDTO = new OAuthUserDTO();
userDTO.setUserId(existUser.getProviderId());
userDTO.setNickname(existUser.getNickname());
userDTO.setImage(existUser.getProfileImageUrl());
userDTO.setCreatedAt(existUser.getCreatedAt());
userDTO.setRole("ROLE_USER");

return new CustomOAuthUser(userDTO);
}
} else if ("owner".equals(state)) {
User existUser = userRepository.findByProviderId(userId).orElse(null);
if (existUser == null || !existUser.getRole().equals("ROLE_OWNER")) {
User newUser = User.create(userId, oAuth2Response.getNickname(), oAuth2Response.getImage(),
"ROLE_OWNER");
userRepository.save(newUser);

OAuthUserDTO userDTO = new OAuthUserDTO();
userDTO.setUserId(userId);
userDTO.setNickname(oAuth2Response.getNickname());
userDTO.setImage(oAuth2Response.getImage());
userDTO.setRole("ROLE_OWNER");

Owner newOwner = Owner.create(newUser);
newOwner.setUser(newUser);
ownerRepository.save(newOwner);

return new CustomOAuthUser(userDTO);
} else {
existUser.setNickname(oAuth2Response.getNickname());
existUser.setProfileImageUrl(oAuth2Response.getImage());

userRepository.save(existUser);

OAuthUserDTO userDTO = new OAuthUserDTO();
userDTO.setUserId(existUser.getProviderId());
userDTO.setNickname(existUser.getNickname());
userDTO.setImage(existUser.getProfileImageUrl());
userDTO.setRole("ROLE_OWNER");

return new CustomOAuthUser(userDTO);
}
} else {
return null;
}
}
}
// package com.jangburich.domain.oauth.domain.service;
//
// import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
// import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
// import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
// import org.springframework.security.oauth2.core.user.OAuth2User;
// import org.springframework.stereotype.Service;
// import org.springframework.web.context.request.RequestContextHolder;
// import org.springframework.web.context.request.ServletRequestAttributes;
//
// import com.jangburich.domain.oauth.domain.CustomOAuthUser;
// import com.jangburich.domain.oauth.domain.KakaoResponse;
// import com.jangburich.domain.oauth.domain.OAuth2Response;
// import com.jangburich.domain.oauth.domain.OAuthUserDTO;
// import com.jangburich.domain.owner.domain.Owner;
// import com.jangburich.domain.owner.domain.repository.OwnerRepository;
// import com.jangburich.domain.user.domain.User;
// import com.jangburich.domain.user.repository.UserRepository;
//
// import jakarta.servlet.http.HttpServletRequest;
// import lombok.extern.slf4j.Slf4j;
//
// @Service
// @Slf4j
// public class OAuthUserService extends DefaultOAuth2UserService {
// private final UserRepository userRepository;
// private final OwnerRepository ownerRepository;
//
// public OAuthUserService(UserRepository userRepository, OwnerRepository ownerRepository) {
// this.userRepository = userRepository;
// this.ownerRepository = ownerRepository;
// }
//
// @Override
// public OAuth2User loadUser(OAuth2UserRequest oAuth2UserRequest) throws OAuth2AuthenticationException {
// OAuth2User oAuth2User = super.loadUser(oAuth2UserRequest);
// log.info("OAuth2User loaded: {}", oAuth2User.getName());
//
// HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
// String state = (String)request.getSession().getAttribute("oauth2_state");
// request.getSession().removeAttribute("oauth2_state");
//
// OAuth2Response oAuth2Response = new KakaoResponse(oAuth2User.getAttributes());
//
// // String userId = oAuth2Response.getProvider() + "_" + oAuth2Response.getProviderId();
// String userId = oAuth2Response.getProviderId();
//
// if ("user".equals(state)) {
// User existUser = userRepository.findByProviderId(userId).orElse(null);
// if (existUser == null) {
// User newUser = User.create(userId, oAuth2Response.getNickname(), oAuth2Response.getImage(),
// "ROLE_USER");
// userRepository.save(newUser);
//
// OAuthUserDTO userDTO = new OAuthUserDTO();
// userDTO.setUserId(userId);
// userDTO.setNickname(oAuth2Response.getNickname());
// userDTO.setImage(oAuth2Response.getImage());
// userDTO.setRole("ROLE_USER");
//
// return new CustomOAuthUser(userDTO);
// } else {
// existUser.setNickname(oAuth2Response.getNickname());
// existUser.setProfileImageUrl(oAuth2Response.getImage());
//
// userRepository.save(existUser);
//
// OAuthUserDTO userDTO = new OAuthUserDTO();
// userDTO.setUserId(existUser.getProviderId());
// userDTO.setNickname(existUser.getNickname());
// userDTO.setImage(existUser.getProfileImageUrl());
// userDTO.setCreatedAt(existUser.getCreatedAt());
// userDTO.setRole("ROLE_USER");
//
// return new CustomOAuthUser(userDTO);
// }
// } else if ("owner".equals(state)) {
// User existUser = userRepository.findByProviderId(userId).orElse(null);
// if (existUser == null || !existUser.getRole().equals("ROLE_OWNER")) {
// User newUser = User.create(userId, oAuth2Response.getNickname(), oAuth2Response.getImage(),
// "ROLE_OWNER");
// userRepository.save(newUser);
//
// OAuthUserDTO userDTO = new OAuthUserDTO();
// userDTO.setUserId(userId);
// userDTO.setNickname(oAuth2Response.getNickname());
// userDTO.setImage(oAuth2Response.getImage());
// userDTO.setRole("ROLE_OWNER");
//
// Owner newOwner = Owner.create(newUser);
// newOwner.setUser(newUser);
// ownerRepository.save(newOwner);
//
// return new CustomOAuthUser(userDTO);
// } else {
// existUser.setNickname(oAuth2Response.getNickname());
// existUser.setProfileImageUrl(oAuth2Response.getImage());
//
// userRepository.save(existUser);
//
// OAuthUserDTO userDTO = new OAuthUserDTO();
// userDTO.setUserId(existUser.getProviderId());
// userDTO.setNickname(existUser.getNickname());
// userDTO.setImage(existUser.getProfileImageUrl());
// userDTO.setRole("ROLE_OWNER");
//
// return new CustomOAuthUser(userDTO);
// }
// } else {
// return null;
// }
// }
// }
Loading

0 comments on commit 4b89797

Please sign in to comment.