Skip to content

Commit

Permalink
Merge pull request #43 from tukcomCD2024/backend/feature/25-amadeus-api
Browse files Browse the repository at this point in the history
[Backend] hotfix : DB 와 모듈화 수정
  • Loading branch information
Dayon-Hong authored Mar 8, 2024
2 parents 34f1d84 + 6207311 commit c5b15ec
Show file tree
Hide file tree
Showing 17 changed files with 385 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RequestMapping("api/chat-gpt")
@RequestMapping("/chat-gpt")
@RestController
public class ChatGptController {
private final ChatGptService chatGptService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/images")
@RequestMapping("/images")
@RequiredArgsConstructor
public class ImageController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/members")
@RequestMapping("/members")
@RequiredArgsConstructor
public class MemberController {

Expand All @@ -29,6 +29,7 @@ public ResponseEntity<String> memberLogin(@RequestBody GoogleLoginRequest reques
}



/**
* 회원가입 - 추가 정보 API
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import com.isp.backend.domain.member.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByUidAndActivatedIsTrue(String Uid);
Member findByUidAndActivatedIsTrue(String Uid);

Optional<Member> findByUid(String Uid);
Member findByUid(String Uid);

boolean existsByUid(String Uid);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,18 @@
import com.isp.backend.domain.member.dto.MemberDetailResponse;
import com.isp.backend.domain.member.dto.SignUpRequest;
import com.isp.backend.domain.member.entity.Member;
import com.isp.backend.domain.member.repository.MemberRepository;
import com.isp.backend.global.exception.member.AuthenticationFailedException;
import com.isp.backend.global.exception.member.MemberNotActivatedException;
import com.isp.backend.global.exception.member.MemberNotFoundException;
import com.isp.backend.global.jwt.TokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
public interface MemberService {
ResponseEntity<String> memberLogin(GoogleLoginRequest request);

//import java.util.Optional;
void signUp(SignUpRequest signUpRequest, String memberUid);

@RequiredArgsConstructor
@Service
ResponseEntity<String> handleExistingMemberLogin(Member existingMember);

public class MemberService {

private final MemberRepository memberRepository;
private final TokenProvider tokenProvider;

/**
* 로그인 메서드 - jwt 토큰 생성후 응답
*/
@Transactional
public ResponseEntity<String> memberLogin(GoogleLoginRequest request) {
Optional<Member> existingMember = memberRepository.findByUid(request.getUid());

if (existingMember.isPresent()) {

Member activatedMember = existingMember
.filter(Member::isActivated)
.orElseThrow(MemberNotActivatedException::new);

// 기존 회원의 로그인
String accessToken = tokenProvider.createAccessToken(activatedMember.getUid());
String refreshToken = tokenProvider.createRefreshToken(activatedMember.getUid());

return ResponseEntity.ok()
.header("Access-Token", accessToken)
.header("Refresh-Token", refreshToken)
.body("기존 회원 로그인");
} else {
// 신규 회원의 로그인 - db 저장
Member newMember = Member.builder()
.uid(request.getUid())
.loginType("google")
.build();
memberRepository.save(newMember);

String accessToken = tokenProvider.createAccessToken(newMember.getUid());
String refreshToken = tokenProvider.createRefreshToken(newMember.getUid());

return ResponseEntity.status(HttpStatus.CREATED)
.header("Access-Token", accessToken)
.header("Refresh-Token", refreshToken)
.body("신규 회원 로그인");
}

}


/**
* 회원가입 - 신규 유저의 경우 추가 정보 저장
*/
@Transactional
public void signUp(SignUpRequest signUpRequest, String memberUid) {
Member member = memberRepository.findByUid(memberUid)
.orElseThrow(() -> new MemberNotFoundException());

signUpRequest.toEntity(member);
memberRepository.save(member);
}


/**
* 토큰 재발행
*/
@Transactional
public ResponseEntity<String> authRecreate(AuthRecreateRequest authRecreateRequest) {

if (!tokenProvider.validateRefreshToken(authRecreateRequest.getRefreshToken())) {
throw new AuthenticationFailedException();
}
String uid = tokenProvider.getUid(authRecreateRequest.getRefreshToken());
Member member = memberRepository.findByUidAndActivatedIsTrue(uid)
.orElseThrow(MemberNotFoundException::new);

String accessToken = tokenProvider.createAccessToken(member.getUid());
String refreshToken = tokenProvider.createRefreshToken(member.getUid());

return ResponseEntity.ok()
.header("Access-Token", accessToken)
.header("Refresh-Token", refreshToken)
.body(null);
}


/**
* 멤버 정보 조회
*/
@Transactional(readOnly = true)
public MemberDetailResponse getMemberInfo(String uid) {

Member member = memberRepository.findByUid(uid)
.orElseThrow(MemberNotFoundException::new);

return (MemberDetailResponse.fromEntity(member));
}
ResponseEntity<String> handleNewMemberLogin(GoogleLoginRequest request);

ResponseEntity<String> authRecreate(AuthRecreateRequest authRecreateRequest);

MemberDetailResponse getMemberInfo(String uid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.isp.backend.domain.member.service;

import com.isp.backend.domain.member.dto.AuthRecreateRequest;
import com.isp.backend.domain.member.dto.GoogleLoginRequest;
import com.isp.backend.domain.member.dto.MemberDetailResponse;
import com.isp.backend.domain.member.dto.SignUpRequest;
import com.isp.backend.domain.member.entity.Member;
import com.isp.backend.domain.member.repository.MemberRepository;
import com.isp.backend.global.exception.member.AuthenticationFailedException;
import com.isp.backend.global.exception.member.MemberNotActivatedException;
import com.isp.backend.global.exception.member.MemberNotFoundException;
import com.isp.backend.global.jwt.TokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service

public class MemberServiceImpl implements MemberService {

private final MemberRepository memberRepository;
private final TokenProvider tokenProvider;

/**
* 로그인 메서드 - jwt 토큰 생성후 응답
*/
@Transactional
@Override
public ResponseEntity<String> memberLogin(GoogleLoginRequest request) {
Member existingMember = memberRepository.findByUid(request.getUid());

if (existingMember != null) {
if (existingMember.isActivated()) {
return handleExistingMemberLogin(existingMember);
} else {
throw new MemberNotActivatedException();
}
} else {
return handleNewMemberLogin(request);
}
}



/** 기존 회원의 로그인 **/
@Override
public ResponseEntity<String> handleExistingMemberLogin(Member existingMember) {
String accessToken = tokenProvider.createAccessToken(existingMember.getUid());
String refreshToken = tokenProvider.createRefreshToken(existingMember.getUid());
return ResponseEntity.ok()
.header("Access-Token", accessToken)
.header("Refresh-Token", refreshToken)
.body("기존 회원 로그인");
}




/** 신규 회원의 로그인 -> DB 저장 **/
@Override
public ResponseEntity<String> handleNewMemberLogin(GoogleLoginRequest request) {
Member newMember = Member.builder()
.uid(request.getUid())
.loginType("google")
.build();
memberRepository.save(newMember);

String accessToken = tokenProvider.createAccessToken(newMember.getUid());
String refreshToken = tokenProvider.createRefreshToken(newMember.getUid());

return ResponseEntity.status(HttpStatus.CREATED)
.header("Access-Token", accessToken)
.header("Refresh-Token", refreshToken)
.body("신규 회원 로그인");
}


/**
* 회원가입 - 신규 유저의 경우 추가 정보 저장
*/
@Transactional
@Override
public void signUp(SignUpRequest signUpRequest, String memberUid) {
Member findMember = memberRepository.findByUid(memberUid);
if (findMember == null) {
throw new MemberNotFoundException();
}

signUpRequest.toEntity(findMember);
memberRepository.save(findMember);
}


/**
* 토큰 재발행
*/
@Transactional
@Override
public ResponseEntity<String> authRecreate(AuthRecreateRequest authRecreateRequest) {

if (!tokenProvider.validateRefreshToken(authRecreateRequest.getRefreshToken())) {
throw new AuthenticationFailedException();
}
String uid = tokenProvider.getUid(authRecreateRequest.getRefreshToken());
Member member = memberRepository.findByUidAndActivatedIsTrue(uid);

if (member == null) {
throw new MemberNotFoundException();
}
String accessToken = tokenProvider.createAccessToken(member.getUid());
String refreshToken = tokenProvider.createRefreshToken(member.getUid());

return ResponseEntity.ok()
.header("Access-Token", accessToken)
.header("Refresh-Token", refreshToken)
.body(null);
}


/**
* 멤버 정보 조회
*/
@Override
public MemberDetailResponse getMemberInfo(String uid) {

Member findMember = memberRepository.findByUid(uid);
if (findMember == null) {
throw new MemberNotFoundException();
}
return (MemberDetailResponse.fromEntity(findMember));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/receipts")
@RequestMapping("/receipts")
@RequiredArgsConstructor
public class ReceiptController {

Expand Down
Loading

0 comments on commit c5b15ec

Please sign in to comment.