Skip to content

Commit

Permalink
✨ Feat: s3를 이용한 프로필 이미지 업로드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhacandy authored Sep 23, 2024
2 parents c77b8f1 + 18c7f8c commit 3bf2350
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cotato.growingpain.member.controller;

import cotato.growingpain.common.Response;
import cotato.growingpain.common.exception.ImageException;
import cotato.growingpain.member.dto.request.AdditionalInfoRequest;
import cotato.growingpain.member.dto.request.UpdateDefaultInfoRequest;
import cotato.growingpain.member.dto.request.UpdateMemberProfileShowingRequest;
Expand All @@ -21,8 +22,10 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "마이페이지", description = "마이페이지 관련된 api")
@RestController
Expand Down Expand Up @@ -74,4 +77,15 @@ public Response<MemberInfoResponse> getMemberInfo(@AuthenticationPrincipal Long
MemberInfoResponse memberInfo = memberService.getMemberInfo(memberId);
return Response.createSuccess("[마이페이지] 프로필 공개 여부에 따른 정보 반환", memberInfo);
}

@Operation(summary = "프로필 이미지 등록", description = "멤버 프로필 이미지 등록 및 저장")
@ApiResponse(content = @Content(schema = @Schema(implementation = Response.class)))
@GetMapping("/profile-image")
@ResponseStatus(HttpStatus.OK)
public Response<?> registerProfileImage(@RequestParam("profile-image") MultipartFile profileImage,
@AuthenticationPrincipal Long memberId) throws ImageException {
log.info("프로필 이미지를 업로드한 memberId: {}", memberId);
memberService.registerProfileImage(memberId, profileImage);
return Response.createSuccessWithNoData("[마이페이지] 프로필 이미지 업로드 완료");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,15 @@ public void updateDefaultInfo(String field, String belong, MemberJob job, String
}

public void updateAdditionalInfo(String career, String aboutMe) {
this.educationBackground = educationBackground;
this.skill = skill;
this.activityHistory = activityHistory;
this.award = award;
this.languageScore = languageScore;
this.career = career;
this.aboutMe = aboutMe;
}

public void updateProfilePublic(MemberProfileShowing memberProfileShowing) {
this.memberProfileShowing = memberProfileShowing;
}

public void updateProfileImage(String imageUrl) {
this.profileImageUrl = imageUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public record MemberInfoResponse(
String name,
String profileImageUrl,
String field,
String belong,
MemberJob job,
Expand All @@ -19,6 +20,7 @@ public record MemberInfoResponse(
public static MemberInfoResponse fromMember(Member member) {
return new MemberInfoResponse(
member.getName(),
member.getProfileImageUrl(),
member.getField(),
member.getBelong(),
member.getJob(),
Expand All @@ -35,6 +37,7 @@ public static MemberInfoResponse fromMember(Member member) {
public static MemberInfoResponse defaultInfoFromMember(Member member) {
return new MemberInfoResponse(
member.getName(),
member.getProfileImageUrl(),
member.getField(),
member.getBelong(),
member.getJob(),
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/cotato/growingpain/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

import cotato.growingpain.common.exception.AppException;
import cotato.growingpain.common.exception.ErrorCode;
import cotato.growingpain.common.exception.ImageException;
import cotato.growingpain.member.domain.MemberProfileShowing;
import cotato.growingpain.member.domain.entity.Member;
import cotato.growingpain.member.dto.request.AdditionalInfoRequest;
import cotato.growingpain.member.dto.request.UpdateDefaultInfoRequest;
import cotato.growingpain.member.dto.response.MemberInfoResponse;
import cotato.growingpain.member.repository.MemberRepository;
import cotato.growingpain.s3.S3Uploader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
@RequiredArgsConstructor
public class MemberService {

private final MemberRepository memberRepository;
private final S3Uploader s3Uploader;

@Transactional
public void updateDefaultInfo(UpdateDefaultInfoRequest request, Long memberId) {
Expand Down Expand Up @@ -70,4 +74,16 @@ public MemberInfoResponse getMemberInfo(Long memberId) {
return MemberInfoResponse.defaultInfoFromMember(member);
}
}

@Transactional
public void registerProfileImage(Long memberId, MultipartFile profileImage) throws ImageException {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new AppException(ErrorCode.MEMBER_NOT_FOUND));

if (profileImage != null && !profileImage.isEmpty()) {
String imageUrl = s3Uploader.uploadFileToS3(profileImage, "profile-image");
member.updateProfileImage(imageUrl);
memberRepository.save(member);
}
}
}

0 comments on commit 3bf2350

Please sign in to comment.