Skip to content
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 @@ -72,18 +72,18 @@ public class CardServiceImpl implements CardService {
private void connectHashtagsToCard(Card card, List<String> hashtags, User user) {
if (hashtags == null || hashtags.isEmpty()) return;
for (String hashtagName : hashtags) {
// 이미 존재하는 해시태그 찾기
// 이미 존재하는 해시태그 찾기
Hashtag hashtag = hashtagRepository.findByHashtagName(hashtagName)
.orElseGet(() -> {
// 없으면 새로 생성 & 저장
// 없으면 새로 생성 & 저장
Hashtag newHashtag = Hashtag.builder()
.hashtagName(hashtagName)
.user(user)
.build();
return hashtagRepository.save(newHashtag);
});

// CardHashtag 저장
// CardHashtag 저장
CardHashtag cardHashtag = CardHashtag.builder()
.card(card)
.hashtag(hashtag)
Expand All @@ -101,8 +101,6 @@ private void connectHashtagsToCard(Card card, List<String> hashtags, User user)
public CardResponse.CreateCardResponse createNewCard(CardCreateRequest.CreateCardRequest request, User user, MultipartFile cardImageFile) {

Long userId = user.getId();
// 아직 유저 관련 처리 안했음
//User user = userRepository.findUserById(userId);

// 오늘 날짜 00:00부터 23:59:59까지 범위 계산
LocalDate today = LocalDate.now();
Expand Down Expand Up @@ -134,8 +132,6 @@ public CardResponse.CreateCardResponse createNewCard(CardCreateRequest.CreateCar
// S3 업로드 시 예외 발생 가능성 있음, try-catch로 처리
cardImageUrl = s3Manager.uploadFile(keyName, cardImageFile);
} catch (Exception e) {
log.error("S3 파일 업로드 실패", e);
// 적절한 커스텀 예외 또는 공통 예외로 감싸서 던짐
throw new GeneralException(ErrorStatus.FILE_UPLOAD_FAILED);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package EatPic.spring.domain.user.controller;

import EatPic.spring.domain.user.dto.response.UserResponseDTO;
import EatPic.spring.domain.user.entity.User;
import EatPic.spring.domain.user.service.UserService;
import EatPic.spring.global.common.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -54,4 +58,14 @@ public ApiResponse<UserResponseDTO.UserActionResponseDto> unfollowUser(HttpServl
return ApiResponse.onSuccess(userService.unfollowUser(request,userId));
}

@PatchMapping(value = "/setting/{userId}/profile-image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "프로필 이미지 수정", description = "마이페이지에서 본인의 프로필 이미지를 수정합니다.")
public ApiResponse<UserResponseDTO.ProfileDto> updateUserProfileImage(
HttpServletRequest request,
@RequestPart(value = "profileImage") MultipartFile profileImage) {

UserResponseDTO.ProfileDto updatedProfile = userService.updateUserProfileImage(request, profileImage, userService.getLoginUser(request));
return ApiResponse.onSuccess(updatedProfile);
}

}
5 changes: 5 additions & 0 deletions src/main/java/EatPic/spring/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,10 @@ public void updateRefreshToken(String refreshToken) {
public void updateLastNotificationCheckAt(LocalDateTime time) {
this.lastNotificationCheckAt = time;
}

public void setProfileImageUrl(String profileImageUrl) {
this.profileImageUrl = profileImageUrl;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import EatPic.spring.domain.user.dto.response.SignupResponseDTO;
import EatPic.spring.domain.user.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;

public interface UserService {
// UserCommandService
Expand All @@ -25,6 +26,8 @@ public interface UserService {
UserResponseDTO.UserActionResponseDto followUser(HttpServletRequest request, Long targetUserId);
UserResponseDTO.UserActionResponseDto unfollowUser(HttpServletRequest request, Long targetUserId);
User getLoginUser(HttpServletRequest request);

UserResponseDTO.ProfileDto updateUserProfileImage(HttpServletRequest request, MultipartFile profileImage, User user);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import EatPic.spring.domain.user.repository.UserRepository;
import EatPic.spring.domain.user.repository.UserBlockRepository;
import EatPic.spring.global.common.code.status.ErrorStatus;
import EatPic.spring.global.common.exception.GeneralException;
import EatPic.spring.global.common.exception.handler.ExceptionHandler;
import EatPic.spring.global.config.jwt.JwtTokenProvider;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -26,8 +27,11 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import EatPic.spring.global.aws.s3.*;

import java.util.Collections;
import java.util.UUID;

import static EatPic.spring.global.common.code.status.ErrorStatus.*;

Expand All @@ -42,6 +46,9 @@ public class UserServiceImpl implements UserService{
private final PasswordEncoder passwordEncoder;
private final JwtTokenProvider jwtTokenProvider;

// s3 설정
private final AmazonS3Manager s3Manager;

// 회원가입
public SignupResponseDTO signup(SignupRequestDTO request) {
// 이메일 중복 검사
Expand Down Expand Up @@ -220,4 +227,33 @@ public UserResponseDTO.UserActionResponseDto followUser(HttpServletRequest reque
return UserConverter.toUserActionResponseDto(follow);

}

@Override
public UserResponseDTO.ProfileDto updateUserProfileImage(HttpServletRequest request, MultipartFile profileImage,User user) {

String profileImageUrl = null;
if (profileImage != null && !profileImage.isEmpty()) {
String uuid = UUID.randomUUID().toString();
String keyName = "userProfiles/" + uuid + "_" + profileImage.getOriginalFilename();

try {
profileImageUrl = s3Manager.uploadFile(keyName, profileImage);
} catch (Exception e) {
throw new GeneralException(ErrorStatus.FILE_UPLOAD_FAILED);
}

// 프로필 이미지 URL 업데이트
user.setProfileImageUrl(profileImageUrl);

// 유저 정보 DB 저장
userRepository.save(user);
}

// 업데이트 결과를 DTO로 반환
return UserResponseDTO.ProfileDto.builder()
.userId(user.getId())
.profileImageUrl(profileImageUrl) // 새 URL
.build();
}

}
Loading