Skip to content

Commit

Permalink
Fix: Member 회사 정보와 email 로 값 변환 하게 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
gytjd committed Nov 18, 2024
1 parent c387ae3 commit 17db2be
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package AIWA.MCPBackend_Member.Controller;

import AIWA.MCPBackend_Member.Dto.*;
import AIWA.MCPBackend_Member.Entity.AiwaKey;
import AIWA.MCPBackend_Member.Entity.Member;
import AIWA.MCPBackend_Member.Service.member.MemberService;
import AIWA.MCPBackend_Member.Service.response.ResponseService;
Expand All @@ -11,6 +12,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
Expand Down Expand Up @@ -49,26 +51,42 @@ public ListResult<MemberResponseDto> getAllMembers() {
return responseService.getListResult(memberResponseDtoList);
}

@GetMapping("/{email}/{companyName}")
public SingleResult<MemberCredentialDto> getMemberKey(
@PathVariable String email,
@PathVariable String companyName) {
Member findMember = memberService.getMemberByEmail(email);
if (findMember == null) {
return (SingleResult<MemberCredentialDto>) responseService.getFailResult("Member not found");
}

Optional<AiwaKey> matchingKey = findMember.getAiwaKeys().stream()
.filter(key -> companyName.equalsIgnoreCase(key.getCompanyName()))
.findFirst();

@GetMapping("/email")
public SingleResult<MemberResponseDto> getMemberByEmail(@RequestParam String email) {
// 특정 회원 조회
Member member = memberService.getMemberByEmail(email);
if (member == null) {
throw new RuntimeException("Member not found with Email: " + email); // 예외 처리
if (matchingKey.isPresent()) {
AiwaKey aiwaKey = matchingKey.get();
MemberCredentialDto memberCredentialDto = new MemberCredentialDto(
findMember.getEmail(),
aiwaKey.getAccessKey(),
aiwaKey.getSecretKey()
);
return responseService.getSingleResult(memberCredentialDto);
} else {
return (SingleResult<MemberCredentialDto>) responseService.getFailResult(
"No key found for company: " + companyName
);
}
MemberResponseDto memberResponseDto = MemberResponseDto.toDto(member);
return responseService.getSingleResult(memberResponseDto);
}




// AWS 키 추가/업데이트
@PostMapping("/add-aws-gcp-key")
public SingleResult<String> addAwsAndGcpKey(@RequestBody AddAwsAndGcpKeyRequestDto requestDto) {
String result = memberService.addOrUpdateAwsAndGcpKey(
requestDto.getEmail(),
requestDto.getCompanyName(),
requestDto.getAccessKey(),
requestDto.getSecretKey(),
requestDto.getGcpKeyContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@Setter
public class AddAwsAndGcpKeyRequestDto {
private String email;
private String companyName;
private String accessKey;
private String secretKey;
private String gcpKeyContent;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/AIWA/MCPBackend_Member/Dto/MemberCredentialDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package AIWA.MCPBackend_Member.Dto;

public class MemberCredentialDto {
private String email;
private String accessKey;
private String secretKey;

public MemberCredentialDto(String email, String accessKey, String secretKey) {
this.email = email;
this.accessKey = accessKey;
this.secretKey = secretKey;
}

// Getters and Setters
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,48 @@ public List<Member> getAllMembers() {
}


public String addOrUpdateAwsAndGcpKey(String email, String accessKey, String secretKey, String gcpKeyContent) {
public String addOrUpdateAwsAndGcpKey(String email, String companyName, String accessKey, String secretKey, String gcpKeyContent) {
// 회원 조회
Member member = getMemberByEmail(email);
if (member == null) {
throw new RuntimeException("Member not found with Email: " + email);
}

// 키가 하나의 회사에 대해 AWS와 GCP를 모두 처리할 수 있도록 합니다.
Optional<AiwaKey> existingAwsKey = member.getAiwaKeys().stream()
.filter(key -> "AWS".equalsIgnoreCase(key.getCompanyName()))
// AWS 키 처리: "AWS" 또는 다른 회사 키를 처리 가능하도록 수정
Optional<AiwaKey> existingKey = member.getAiwaKeys().stream()
.filter(key -> companyName.equalsIgnoreCase(key.getCompanyName()))
.findFirst();

if (existingAwsKey.isPresent()) {
AiwaKey awsKey = existingAwsKey.get();
awsKey.setAccessKey(accessKey);
awsKey.setSecretKey(secretKey);
// GCP 키를 추가할 경우 GCP Key Path를 업데이트
awsKey.setGcpKeyPath(s3Service.uploadGcpKeyFile(email, gcpKeyContent));
if (existingKey.isPresent()) {
// 기존 키 업데이트
AiwaKey key = existingKey.get();
key.setAccessKey(accessKey);
key.setSecretKey(secretKey);

// GCP 키 내용이 제공된 경우에만 GCP 키 경로 업데이트
if (gcpKeyContent != null && !gcpKeyContent.isEmpty()) {
String gcpKeyPath = s3Service.uploadGcpKeyFile(email, gcpKeyContent);
key.setGcpKeyPath(gcpKeyPath);
}
} else {
AiwaKey newKey = new AiwaKey("AWS", accessKey, secretKey, s3Service.uploadGcpKeyFile(email, gcpKeyContent), member);
// 새로운 키 추가
String gcpKeyPath = gcpKeyContent != null && !gcpKeyContent.isEmpty()
? s3Service.uploadGcpKeyFile(email, gcpKeyContent)
: null;

AiwaKey newKey = new AiwaKey(companyName, accessKey, secretKey, gcpKeyPath, member);
member.getAiwaKeys().add(newKey);
}

member = memberRepository.save(member);
// 회원 정보 저장
memberRepository.save(member);

return "AWS and GCP keys have been successfully added or updated.";
return String.format("%s keys have been successfully added or updated.", companyName);
}




public Member removeAwsKey(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new RuntimeException("Member not found"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public CommonResult getSuccessResult(){
/**
* 실패 결과만 처리하는 메소드
*/
public CommonResult getFailResult(){
public CommonResult getFailResult(String s){
CommonResult result= new CommonResult();
result.setSuccess(false);
result.setCode(CommonResponse.FAIL.getCode());
Expand Down

0 comments on commit 17db2be

Please sign in to comment.