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

[Backend] hotfix : DB 와 모듈화 수정 #43

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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
Loading