Skip to content

Commit

Permalink
Merge pull request #7 from KAKAO-TOUR-API-CONTEST/develop
Browse files Browse the repository at this point in the history
FEAT : users/mypage
  • Loading branch information
femmefatalehaein authored Aug 15, 2024
2 parents 3764745 + b94d03c commit c3518ea
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 124 deletions.
21 changes: 14 additions & 7 deletions src/main/java/com/example/ai_jeju/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.ai_jeju.controller;

import com.example.ai_jeju.domain.User;
import com.example.ai_jeju.dto.MyPageResponse;
import com.example.ai_jeju.dto.SignUpRequest;
import com.example.ai_jeju.dto.WithdrawRequest;
import com.example.ai_jeju.handler.SignUpHandler;
Expand All @@ -23,19 +25,24 @@ public class UserController {
//회원가입
/*여기 확인해보자*/
@PostMapping("/register")
public void signUp(@RequestBody SignUpRequest signUpRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
userService.signUp(signUpRequest,request,response);
public Long registerUser(@RequestBody SignUpRequest signUpRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
return userService.registerUser(signUpRequest,request,response);
}
@GetMapping("/checks")
public Object CheckIfUser(@RequestParam String email){
return userService.checkIfUser(email);
public Long CheckIfUser(@RequestParam String email, HttpServletRequest request, HttpServletResponse response){
return userService.checkIfUser(email,request,response);
}

@GetMapping("/RandomList")
public Object GetRandomList(@RequestParam String email){
return userService.checkIfUser(email);
@GetMapping("/mypage")
public MyPageResponse myPage(@RequestParam Long userId){
return userService.getMyPage(userId);
}

// @GetMapping("/RandomList")
// public Object GetRandomList(){
// return userService.getRandomList();
// }

//탈퇴하기
@GetMapping("/withdraw")
public ResponseEntity<String> signOut(@RequestBody WithdrawRequest withdrawRequest) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/example/ai_jeju/domain/Child.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public class Child {
@Column
private String realtion;

@Column
private String phoneNum;

@Column
private long childProfile;
}
17 changes: 10 additions & 7 deletions src/main/java/com/example/ai_jeju/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ public class User {
@Column(name="email", nullable = false, unique = true)
private String email;
//실명
@Column(name = "name")
@Column(name = "name",nullable = false)
private String name;

@Column(name = "nickname")
@Column(name = "nickname",nullable = false)
private String nickname;

@Column(name = "provider")
@Column(name = "provider", nullable = false)
private String provider;

@Column(name="snsprofile")
private String snsprofile;
@Column(name="profileImg")
private String profileImg;

@Column(name="profileimg")
private String profileimg;
@Column(name="rgtDate", nullable = false)
private String rgtDate;

@Column(name="phoneNum", nullable = false)
private String phoneNum;


/*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/example/ai_jeju/dto/ChildRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public class ChildRequest {
private String childName;
private Boolean gender;
private long childProfile;
private String relation;

}
26 changes: 26 additions & 0 deletions src/main/java/com/example/ai_jeju/dto/MyPageResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.ai_jeju.dto;

import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class MyPageResponse {



private String email;
private String name;
private String nickname;
private String profileImg;
private String rgtDate;
private String phoneNum;

private int numOfChilds;
}
3 changes: 2 additions & 1 deletion src/main/java/com/example/ai_jeju/dto/SignUpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public class SignUpRequest {
private String name;
private String nickname;
private String email;
private String profile;
private String profileImg;
private String provider;
private String phoneNum;
/**
* 함께 보낼 동반 아동에 대한 정보 -> 여러명일 수도 있으므로 배열 형식으로 받는다.
* private List<Child> child;
Expand Down
116 changes: 58 additions & 58 deletions src/main/java/com/example/ai_jeju/handler/SignUpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,70 +29,68 @@
@NoArgsConstructor
@AllArgsConstructor
public class SignUpHandler{
public static final String REFRESH_TOKEN_COOKIE_NAME = "refresh_token";
public static final String ACCESS_TOKEN_COOKIE_NAME = "access_token";
public static final Duration REFRESH_TOKEN_DURATION = Duration.ofDays(14);
public static final Duration ACCESS_TOKEN_DURATION = Duration.ofDays(1);

HttpServletResponse response;

RefreshTokenRepository refreshTokenRepository;

CookieUtil cookieUtil;

@Autowired
private ChildRepository childRepository;
private UserRepository userRepository;

public String successHadler(HttpServletRequest request,
HttpServletResponse response, SignUpRequest signUpRequest, TokenProvider tokenProvider) throws IOException {

String nick = signUpRequest.getNickname();
if(nick==null){
nick = new NickNameGenerator().getNickname();
}
// Save new user using builder pattern
User newUser = User.builder()
.name(signUpRequest.getName())
.nickname(nick)
.provider(signUpRequest.getProvider())
.email(signUpRequest.getEmail())
.snsprofile(signUpRequest.getProfile())
.provider(signUpRequest.getProvider())
.build();


String accessToken = tokenProvider.generateToken(newUser, REFRESH_TOKEN_DURATION);
/*-------------------------------------------*/
//동반아동
List<ChildRequest> childList = signUpRequest.getChild();
for(int i=0; i<childList.size(); i++){
Child child = Child.builder()
.userId(newUser.getId())
.childName(childList.get(i).getChildName())
.birthDate(childList.get(i).getBirthDate())
.gender(childList.get(i).getGender())
.build();
childRepository.save(child);
}

userRepository.save(newUser);
String refresh_token = tokenProvider.generateToken(newUser, REFRESH_TOKEN_DURATION);
/*
RefreshToken refreshToken = RefreshToken.builder()
.refresh_token(refresh_token)
.userId(newUser.getId()).build();*/

/*
refreshTokenRepository.save(refreshToken);
userRepository.save(newUser);
int cookieMaxAge = (int) REFRESH_TOKEN_DURATION.toSeconds();
CookieUtil.addCookie(response, REFRESH_TOKEN_COOKIE_NAME, refresh_token, cookieMaxAge);*/

saveRefreshToken(newUser.getId(), refresh_token);
addRefreshTokenToCookie(request,response,refresh_token);
return accessToken;
}

// public String successHadler(HttpServletRequest request,
// HttpServletResponse response, SignUpRequest signUpRequest, TokenProvider tokenProvider) throws IOException {
//
// String nick = signUpRequest.getNickname();
// if(nick==null){
// nick = new NickNameGenerator().getNickname();
// }
// // Save new user using builder pattern
// User newUser = User.builder()
// .name(signUpRequest.getName())
// .nickname(nick)
// .provider(signUpRequest.getProvider())
// .email(signUpRequest.getEmail())
// .snsprofile(signUpRequest.getProfile())
// .provider(signUpRequest.getProvider())
// .phoneNum(signUpRequest.getPhoneNum())
// .build();
//
//
// String accessToken = tokenProvider.generateToken(newUser, REFRESH_TOKEN_DURATION);
// /*-------------------------------------------*/
// //동반아동
// List<ChildRequest> childList = signUpRequest.getChild();
// for(int i=0; i<childList.size(); i++){
// Child child = Child.builder()
// .userId(newUser.getId())
// .childName(childList.get(i).getChildName())
// .birthDate(childList.get(i).getBirthDate())
// .gender(childList.get(i).getGender())
// .build();
// childRepository.save(child);
// }
//
// userRepository.save(newUser);
// String refresh_token = tokenProvider.generateToken(newUser, REFRESH_TOKEN_DURATION);
// /*
// RefreshToken refreshToken = RefreshToken.builder()
// .refresh_token(refresh_token)
// .userId(newUser.getId()).build();*/
//
// /*
// refreshTokenRepository.save(refreshToken);
// userRepository.save(newUser);
// int cookieMaxAge = (int) REFRESH_TOKEN_DURATION.toSeconds();
// CookieUtil.addCookie(response, REFRESH_TOKEN_COOKIE_NAME, refresh_token, cookieMaxAge);*/
//
// saveRefreshToken(newUser.getId(), refresh_token);
// addRefreshTokenToCookie(request,response,refresh_token);
// return accessToken;
// }

//생성된 리프레시 토큰을 전달받아 데이터베이스 저장
private void saveRefreshToken(Long userId, String newRefreshToken) {
Expand All @@ -105,15 +103,17 @@ private void saveRefreshToken(Long userId, String newRefreshToken) {

//생성된 리프레시 토큰을 쿠키에 저장
private void addRefreshTokenToCookie(HttpServletRequest request,
HttpServletResponse response, String refreshToken) {
int cookieMaxAge = (int) REFRESH_TOKEN_DURATION.toSeconds();
CookieUtil.deleteCookie(request, response, REFRESH_TOKEN_COOKIE_NAME);
CookieUtil.addCookie(response, REFRESH_TOKEN_COOKIE_NAME, refreshToken, cookieMaxAge);
System.out.println("addRefreshToken 동작");
HttpServletResponse response, String accessToken) {
int cookieMaxAge = (int) ACCESS_TOKEN_DURATION.toSeconds();
CookieUtil.deleteCookie(request, response, ACCESS_TOKEN_COOKIE_NAME);
CookieUtil.addCookie(response, ACCESS_TOKEN_COOKIE_NAME, accessToken, cookieMaxAge);

}







}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@


import com.example.ai_jeju.domain.Child;
import com.example.ai_jeju.domain.Store;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface ChildRepository extends JpaRepository<Child, Long> {

@Query("SELECT c FROM Child c WHERE c.userId = :userId")
List<Child> findAllById(@Param("userId") Long userId);


}
13 changes: 13 additions & 0 deletions src/main/java/com/example/ai_jeju/repository/StoreRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.ai_jeju.repository;

import com.example.ai_jeju.domain.Store;
import com.example.ai_jeju.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface StoreRepository extends JpaRepository<Store, Long> {
Optional<Store> findById(Long id);
}
33 changes: 14 additions & 19 deletions src/main/java/com/example/ai_jeju/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,39 @@ public void updateNickname(Long id, String nickname) {
userRepository.save(user);
}

public void updateSnsProfile(Long id, String snsprofile) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setSnsprofile(snsprofile);
userRepository.save(user);
}


@Transactional
public void updateProfile(Long id, String profileimg) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setProfileimg(profileimg); // S3에 업로드된 이미지의 URL을 프로필 이미지로 설정
user.setProfileImg(profileimg); // S3에 업로드된 이미지의 URL을 프로필 이미지로 설정
userRepository.save(user);
}

//프로필 이미지 반환
public String getProfileUrl(Long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Profile not found"));
return user.getProfileimg(); // 프로필 이미지 URL 반환
return user.getProfileImg(); // 프로필 이미지 URL 반환
}

public void deleteSnsProfile(Long id) {
Optional<User> userOptional = userRepository.findById(id);
if (userOptional.isPresent()) {
User user = userOptional.get();
user.setSnsprofile(null);
userRepository.save(user);
} else {
throw new NoSuchElementException("not found");
}
}
// public void deleteSnsProfile(Long id) {
// Optional<User> userOptional = userRepository.findById(id);
// if (userOptional.isPresent()) {
// User user = userOptional.get();
// user.setSnsprofile(null);
// userRepository.save(user);
// } else {
// throw new NoSuchElementException("not found");
// }
// }

public void deleteProfileImage(Long id) {
Optional<User> userOptional = userRepository.findById(id);
if (userOptional.isPresent()) {
User user = userOptional.get();
user.setProfileimg(null);
user.setProfileImg(null);
userRepository.save(user);
} else {
throw new NoSuchElementException("not found");
Expand Down
Loading

0 comments on commit c3518ea

Please sign in to comment.