-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
프로필 업데이트 API 구현 완료
- Loading branch information
Showing
20 changed files
with
463 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/munecting/api/domain/user/dao/UuidRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.munecting.api.domain.user.dao; | ||
|
||
import com.munecting.api.domain.user.entity.Uuid; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface UuidRepository extends JpaRepository<Uuid, Long> { | ||
|
||
Optional<Uuid> findByUserId(Long userId); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/munecting/api/domain/user/dto/request/UpdateProfileRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.munecting.api.domain.user.dto.request; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public record UpdateProfileRequestDto( | ||
String nickname, | ||
MultipartFile profileImage | ||
) { | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/munecting/api/domain/user/dto/response/UpdateProfileResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.munecting.api.domain.user.dto.response; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record UpdateProfileResponseDto( | ||
String nickname, | ||
String imageUrl | ||
) { | ||
|
||
public static UpdateProfileResponseDto of(String nickname, String imgUrl) { | ||
return UpdateProfileResponseDto.builder() | ||
.nickname(nickname) | ||
.imageUrl(imgUrl) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/munecting/api/domain/user/entity/Uuid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.munecting.api.domain.user.entity; | ||
|
||
import com.munecting.api.global.common.domain.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.util.UUID; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class Uuid extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(unique = true) | ||
private String uuid; | ||
|
||
private Long userId; | ||
|
||
public static Uuid toEntity(Long userId) { | ||
return Uuid.builder() | ||
.uuid(UUID.randomUUID().toString()) | ||
.userId(userId) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/munecting/api/domain/user/service/UserProfileImageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.munecting.api.domain.user.service; | ||
|
||
import com.munecting.api.domain.user.entity.User; | ||
import com.munecting.api.domain.user.entity.Uuid; | ||
import com.munecting.api.global.aws.s3.AmazonS3Manager; | ||
import com.munecting.api.global.error.exception.InvalidValueException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.Objects; | ||
|
||
import static com.munecting.api.global.common.dto.response.Status.BAD_REQUEST; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserProfileImageService { | ||
|
||
private final UuidService uuidService; | ||
private final AmazonS3Manager s3Manager; | ||
|
||
private static final long MAX_FILE_SIZE = 3 * 1024 * 1024; // 3MB | ||
private static final String OVER_FILE_SIZE_ERROR_MESSAGE = "업로드 가능한 파일 사이즈는 최대 3MB입니다."; | ||
|
||
public String updateImage(User user, MultipartFile imgFile) { | ||
if (Objects.isNull(imgFile)) { | ||
return getImageUrl(user); | ||
} | ||
|
||
if (user.getProfileImageUrl() != null) { | ||
deleteOldImage(user); | ||
} | ||
|
||
String imageUrl = uploadNewImage(user.getId(), imgFile); | ||
return user.updateProfileImageUrl(imageUrl); | ||
} | ||
|
||
private String getImageUrl(User user) { | ||
if (user.getProfileImageUrl() == null) { | ||
// 프론트엔드 측 기본 이미지 사용 | ||
return null; | ||
} | ||
|
||
return user.getProfileImageUrl(); | ||
} | ||
|
||
private void deleteOldImage(User user) { | ||
Uuid uuid = uuidService.findByUserId(user.getId()); | ||
String presentKeyName = s3Manager.generateProfileImageKeyName(uuid); | ||
s3Manager.deleteFile(presentKeyName); | ||
uuidService.delete(uuid); | ||
} | ||
|
||
private String uploadNewImage(Long userId, MultipartFile image) { | ||
validateImageSize(image); | ||
Uuid uuid = uuidService.createUuid(userId); | ||
String keyName = s3Manager.generateProfileImageKeyName(uuid); | ||
|
||
return s3Manager.uploadFile(keyName, image); | ||
} | ||
|
||
private void validateImageSize(MultipartFile image) { | ||
if (image.getSize() > MAX_FILE_SIZE) { | ||
throw new InvalidValueException(BAD_REQUEST, OVER_FILE_SIZE_ERROR_MESSAGE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/munecting/api/domain/user/service/UuidService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.munecting.api.domain.user.service; | ||
|
||
import com.munecting.api.domain.user.entity.Uuid; | ||
import com.munecting.api.domain.user.dao.UuidRepository; | ||
import com.munecting.api.global.error.exception.InternalServerException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class UuidService { | ||
|
||
private final UuidRepository uuidRepository; | ||
|
||
public Uuid createUuid(Long userId) { | ||
Uuid uuid = Uuid.toEntity(userId); | ||
|
||
return uuidRepository.save(uuid); | ||
} | ||
|
||
public Uuid findByUserId(Long userId) { | ||
return uuidRepository.findByUserId(userId).orElseThrow(()-> { | ||
log.warn("userId {}과 매핑된 UUID 객체가 존재하지 않습니다.",userId); | ||
throw new InternalServerException(); | ||
}); | ||
} | ||
|
||
public void delete(Uuid uuid) { | ||
uuidRepository.delete(uuid); | ||
} | ||
} |
Oops, something went wrong.