Skip to content

Commit

Permalink
Merge pull request #18 from Journey-Together/feature/#15
Browse files Browse the repository at this point in the history
Feature/#15
  • Loading branch information
sycuuui authored Jun 3, 2024
2 parents 4a32d55 + 6c0a188 commit e495f66
Show file tree
Hide file tree
Showing 13 changed files with 393 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void login(HttpServletResponse response) throws IOException {
@Operation(summary = "로그인 API")
@PostMapping("/sign-in")
public ApiResponse<LoginRes> signIn(@RequestHeader("Authorization") String token,
@RequestBody LoginReq loginReq) {
@RequestBody LoginReq loginReq) throws IOException {
return ApiResponse.success(Success.LOGIN_SUCCESS,authService.signIn(token,loginReq));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package Journey.Together.domain.member.controller;

import Journey.Together.domain.member.dto.MemberReq;
import Journey.Together.domain.member.dto.MemberRes;
import Journey.Together.domain.member.service.MemberService;
import Journey.Together.global.common.ApiResponse;
import Journey.Together.global.exception.Success;
import Journey.Together.global.security.PrincipalDetails;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/member")
@Tag(name = "Member", description = "사용자 관련 API")
public class MemberController {

private final MemberService memberService;

@PatchMapping ("")
public ApiResponse saveMemberInfo(@AuthenticationPrincipal PrincipalDetails principalDetails, @RequestBody MemberReq memberReq) {
memberService.saveInfo(principalDetails.getMember(),memberReq);
return ApiResponse.success(Success.UPDATE_USER_INFO_SUCCESS);
}

@GetMapping("")
public ApiResponse<MemberRes> getMember() {
return ApiResponse.success(Success.LOGIN_SUCCESS);
public ApiResponse<MemberRes> findMemberInfo(@AuthenticationPrincipal PrincipalDetails principalDetails){
MemberRes memberRes = memberService.findMemberInfo(principalDetails.getMember());
return ApiResponse.success(Success.GET_MYPAGE_SUCCESS,memberRes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public record LoginRes(
@NotNull Long memberId,
@NotNull String email,
@NotNull String name,
String profileUrl,
String profileUuid,
MemberType memberType,
LoginType loginType,
@NotNull String accessToken,
Expand All @@ -25,7 +25,7 @@ public static LoginRes of(Member member, TokenDto tokenDto) {
.memberId(member.getMemberId())
.email(member.getEmail())
.name(member.getName())
.profileUrl(member.getProfileUrl())
.profileUuid(member.getProfileUuid())
.memberType(member.getMemberType())
.loginType(member.getLoginType())
.accessToken(tokenDto.accessToken())
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/Journey/Together/domain/member/dto/MemberReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Journey.Together.domain.member.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.constraints.Null;
import org.springframework.web.multipart.MultipartFile;

@JsonInclude(JsonInclude.Include.NON_NULL)
public record MemberReq(
@Null
String name,
@Null
String phone,
@Null
MultipartFile profileImage,
@Null
String bloodType,
@Null
String birth,
@Null
String disease,
@Null
String allergy,
@Null
String medication,
@Null
String part1_rel,
@Null
String part1_phone,
@Null
String part2_rel,
@Null
String part2_phone
){

}
37 changes: 36 additions & 1 deletion src/main/java/Journey/Together/domain/member/dto/MemberRes.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
package Journey.Together.domain.member.dto;

public record MemberRes() {
import Journey.Together.domain.member.entity.Member;
import lombok.Builder;
import org.springframework.web.multipart.MultipartFile;

@Builder
public record MemberRes(
String name,
String phone,
String profileImage,
String bloodType,
String birth,
String disease,
String allergy,
String medication,
String part1_rel,
String part1_phone,
String part2_rel,
String part2_phone
) {
public static MemberRes of(Member member,String profileUrl){
return MemberRes.builder()
.name(member.getName())
.phone(member.getPhone())
.profileImage(profileUrl)
.bloodType(member.getBloodType())
.birth(member.getBirth())
.disease(member.getDisease())
.allergy(member.getAllergy())
.medication(member.getMedication())
.part1_rel(member.getPart1Rel())
.part1_phone(member.getPart1Phone())
.part2_rel(member.getPart2Rel())
.part2_phone(member.getPart2Phone())
.build();

}
}
26 changes: 20 additions & 6 deletions src/main/java/Journey/Together/domain/member/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package Journey.Together.domain.member.entity;

import Journey.Together.domain.member.enumerate.BloodType;
import Journey.Together.domain.member.enumerate.LoginType;
import Journey.Together.domain.member.enumerate.MemberType;
import Journey.Together.global.common.BaseTimeEntity;
Expand Down Expand Up @@ -34,15 +33,14 @@ public class Member extends BaseTimeEntity {
private String phone;

@Column(name = "profile_url", columnDefinition = "text")
private String profileUrl;
private String profileUuid;

@Column(name = "login_type", nullable = false, columnDefinition = "varchar(255)")
@Enumerated(EnumType.STRING)
private LoginType loginType;

@Column(name = "blood_type", columnDefinition = "varchar(255)")
@Enumerated(EnumType.STRING)
private BloodType bloodType;
private String bloodType;

@Column(name = "member_type", nullable = false, columnDefinition = "varchar(255)")
@Enumerated(EnumType.STRING)
Expand All @@ -60,21 +58,37 @@ public class Member extends BaseTimeEntity {
@Column(name = "medication", columnDefinition = "varchar(255)")
private String medication;

@Column(name = "part1_rel", columnDefinition = "varchar(255)")
private String part1Rel;

@Column(name = "part1_phone", columnDefinition = "varchar(255)")
private String part1Phone;

@Column(name = "part2_rel", columnDefinition = "varchar(255)")
private String part2Rel;

@Column(name = "part2_phone", columnDefinition = "varchar(255)")
private String part2Phone;

@Column(name = "refreshToken", columnDefinition = "varchar(255)")
private String refreshToken;

@Builder
public Member(String email, String name, String phone, String profileUrl, LoginType loginType,BloodType bloodType,MemberType memberType, String birth, String allergy, String medication, String refreshToken) {
public Member(String email, String name, String phone, String profileUuid, LoginType loginType,String bloodType,MemberType memberType, String birth, String allergy, String medication,String part1Rel,String part1Phone,String part2Rel,String part2Phone, String refreshToken) {
this.email = email;
this.name = name;
this.phone = phone;
this.profileUrl = profileUrl;
this.profileUuid = profileUuid;
this.loginType = loginType;
this.bloodType = bloodType;
this.memberType = memberType;
this.birth = birth;
this.allergy = allergy;
this.medication=medication;
this.part1Rel=part1Rel;
this.part1Phone=part1Phone;
this.part2Rel=part2Rel;
this.part2Phone=part2Phone;
this.refreshToken=refreshToken;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import Journey.Together.domain.member.enumerate.LoginType;
import Journey.Together.domain.member.enumerate.MemberType;
import Journey.Together.domain.member.repository.MemberRepository;
import Journey.Together.global.common.CustomMultipartFile;
import Journey.Together.global.exception.ApplicationException;
import Journey.Together.global.exception.ErrorCode;
import Journey.Together.global.security.kakao.KakaoClient;
import Journey.Together.global.security.kakao.dto.KakaoProfile;
import Journey.Together.global.security.jwt.TokenProvider;
import Journey.Together.global.security.jwt.dto.TokenDto;
import Journey.Together.global.security.naver.dto.NaverUserResponse;
import Journey.Together.global.util.S3Client;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -22,6 +24,14 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

@Service
@Transactional(readOnly = true)
Expand All @@ -30,11 +40,12 @@ public class AuthService {
private final KakaoClient kakaoClient;
private final TokenProvider tokenProvider;
private final MemberRepository memberRepository;
private final S3Client s3Client;

private final RestTemplate restTemplate = new RestTemplate();

@Transactional
public LoginRes signIn(String token, LoginReq loginReq) {
public LoginRes signIn(String token, LoginReq loginReq) throws IOException {
Member member = null;
TokenDto tokenDto = null;

Expand All @@ -46,10 +57,13 @@ public LoginRes signIn(String token, LoginReq loginReq) {
member = memberRepository.findMemberByEmailAndDeletedAtIsNull(kakaoProfile.kakao_account().email()).orElse(null);
// 이메일 존재 시 로그인 , 존재하지 않을 경우 회원가입 진행
if(member == null) {
MultipartFile imageFile = convertUrlToMultipartFile(kakaoProfile.kakao_account().profile().profile_image_url());
String uuid = s3Client.upload(imageFile);
Member newMember = Member.builder()
.email(kakaoProfile.kakao_account().email())
.name(kakaoProfile.kakao_account().profile().nickname())
.profileUrl(kakaoProfile.kakao_account().profile().profile_image_url())
.profileUuid(uuid)
.phone(null)
.memberType(MemberType.valueOf("GENERAL"))
.loginType(LoginType.valueOf("KAKAO"))
.build();
Expand All @@ -66,9 +80,12 @@ public LoginRes signIn(String token, LoginReq loginReq) {
member = memberRepository.findMemberByEmailAndDeletedAtIsNull(naverProfile.getEmail()).orElse(null);

if (member == null) {
MultipartFile imageFile = convertUrlToMultipartFile(naverProfile.getProfile_image() != null ? naverProfile.getProfile_image() : null);
String uuid = s3Client.upload(imageFile);

Member newMember = Member.builder()
.email(naverProfile.getEmail() != null ? naverProfile.getEmail() : "Unknown")
.profileUrl(naverProfile.getProfile_image() != null ? naverProfile.getProfile_image() : "Unknown")
.profileUuid(uuid)
.name(naverProfile.getName() != null ? naverProfile.getName() : "Unknown")
.memberType(MemberType.GENERAL)
.loginType(LoginType.NAVER)
Expand Down Expand Up @@ -128,4 +145,20 @@ public TokenDto reissue(String token, Member member) {
// Business Logic & Response - Access Token 새로 발급 + Refresh Token의 유효 기간이 Access Token의 유효 기간보다 짧아졌을 경우 Refresh Token도 재발급
return tokenProvider.reissue(member, refreshToken);
}

//url->multipartFile로 변환
private MultipartFile convertUrlToMultipartFile(String imageUrl) throws IOException {
URL url = new URL(imageUrl);

try(InputStream inputStream = url.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
// 1) image url -> byte[]
BufferedImage urlImage = ImageIO.read(inputStream);
ImageIO.write(urlImage, "jpg", bos);
byte[] byteArray = bos.toByteArray();
// 2) byte[] -> MultipartFile
MultipartFile multipartFile = new CustomMultipartFile(byteArray, imageUrl);
return multipartFile; // image를 storage에 저장하는 메서드 호출
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,77 @@
package Journey.Together.domain.member.service;

import Journey.Together.domain.member.dto.MemberReq;
import Journey.Together.domain.member.dto.MemberRes;
import Journey.Together.domain.member.entity.Member;
import Journey.Together.domain.member.repository.MemberRepository;
import Journey.Together.global.exception.ApplicationException;
import Journey.Together.global.exception.ErrorCode;
import Journey.Together.global.util.S3Client;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberService {

private final MemberRepository memberRepository;
private final S3Client s3Client;

@Transactional
public void saveInfo(Member member,MemberReq memberReq){
// Validation
memberRepository.findMemberByEmailAndDeletedAtIsNull(member.getEmail()).orElseThrow(()->new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION));
//Business
if (memberReq.name() != null) {
member.setName(memberReq.name());
}
if (memberReq.phone() != null) {
member.setPhone(memberReq.phone());
}
if (memberReq.profileImage() != null) {
String uuid = s3Client.update(member.getProfileUuid(),memberReq.profileImage());
member.setProfileUuid(uuid);
}
if (memberReq.bloodType() != null) {
member.setBloodType(memberReq.bloodType());
}
if (memberReq.birth() != null) {
member.setBirth(memberReq.birth());
}
if (memberReq.disease() != null) {
member.setDisease(memberReq.disease());
}
if (memberReq.allergy() != null) {
member.setAllergy(memberReq.allergy());
}
if (memberReq.medication() != null) {
member.setMedication(memberReq.medication());
}
if (memberReq.part1_rel() != null) {
member.setPart1Rel(memberReq.part1_rel());
}
if (memberReq.part1_phone() != null) {
member.setPart1Phone(memberReq.part1_phone());
}
if (memberReq.part2_rel() != null) {
member.setPart2Rel(memberReq.part2_rel());
}
if (memberReq.part2_phone() != null) {
member.setPart2Phone(memberReq.part2_phone());
}

}

public MemberRes findMemberInfo(Member member){
// Validation
memberRepository.findMemberByEmailAndDeletedAtIsNull(member.getEmail()).orElseThrow(()->new ApplicationException(ErrorCode.NOT_FOUND_EXCEPTION));
//Business
MemberRes memberRes = MemberRes.of(member, s3Client.getUrl()+member.getProfileUuid());
//Response
return memberRes;
}

}
Loading

0 comments on commit e495f66

Please sign in to comment.