From 3f7f0428daba121582654f793940cc3397de0ff6 Mon Sep 17 00:00:00 2001 From: DH_Choi <58378676+vectorch9@users.noreply.github.com> Date: Sun, 5 Nov 2023 00:21:44 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor]=20=EC=82=AC=EC=A7=84=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=EC=9D=84=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81=ED=95=98=EA=B3=A0=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=9C=EB=8B=A4=20(#89)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: Image를 도메인 패키지로 변경한다 * refactor: Images의 upload 메서드가 Image 도메인 객체를 반환하도록 리팩토링한다 * refactor: ImageIdentifierGenerator를 인터페이스로 추상화하여 분리한다 * refactor: Image의 imageIdentifier 네이밍을 identifier로 변경한다 * feature: Image에 검증 로직을 추가한다 * fix: ImageIdentifier 수정으로 인한 버그를 수정한다 * test: Image 테스트를 작성한다 --- .../badge/application/SaveBadgeService.java | 12 ++-- .../java/daybyquest/badge/domain/Badge.java | 2 +- .../daybyquest/badge/query/BadgeData.java | 4 +- .../global/config/PropertiesConfig.java | 2 +- .../group/application/SaveGroupService.java | 16 +++--- .../java/daybyquest/group/domain/Group.java | 2 +- .../daybyquest/group/query/GroupData.java | 4 +- .../{vo => domain}/BaseImageProperties.java | 2 +- .../java/daybyquest/image/domain/Image.java | 45 +++++++++++++++ .../domain/ImageIdentifierGenerator.java | 6 ++ .../java/daybyquest/image/domain/Images.java | 10 ++++ .../java/daybyquest/image/infra/S3Images.java | 6 +- .../infra/UUIDImageIdentifierGenerator.java | 19 +++++++ src/main/java/daybyquest/image/vo/Image.java | 36 ------------ .../image/vo/ImageIdentifierGenerator.java | 13 ----- src/main/java/daybyquest/image/vo/Images.java | 10 ---- .../application/SaveInterestService.java | 12 ++-- .../daybyquest/interest/domain/Interest.java | 4 +- .../post/application/SavePostService.java | 11 ++-- .../java/daybyquest/post/domain/Post.java | 2 +- .../post/dto/response/PostResponse.java | 4 +- .../dto/response/PostWithQuestResponse.java | 4 +- .../post/query/PostDaoQuerydslImpl.java | 4 +- .../java/daybyquest/post/query/PostData.java | 2 +- .../quest/application/SaveQuestService.java | 11 ++-- .../java/daybyquest/quest/domain/Quest.java | 2 +- .../daybyquest/quest/query/QuestData.java | 4 +- .../application/DeleteUserImageService.java | 6 +- .../user/application/SaveUserService.java | 4 +- .../application/UpdateUserImageService.java | 14 ++--- .../java/daybyquest/user/domain/User.java | 4 +- .../user/query/ProfileDaoQuerydslImpl.java | 4 +- .../daybyquest/image/domain/ImageTest.java | 55 +++++++++++++++++++ .../interest/domain/InterestTest.java | 2 +- .../java/daybyquest/post/domain/PostTest.java | 2 +- .../daybyquest/quest/domain/QuestTest.java | 2 +- .../support/fixture/BadgeFixtures.java | 4 +- .../support/fixture/GroupFixtures.java | 2 +- .../support/fixture/InterestFixtures.java | 2 +- .../support/fixture/PostFixtures.java | 2 +- .../support/fixture/QuestFixtures.java | 2 +- .../support/fixture/UserFixtures.java | 2 +- 42 files changed, 216 insertions(+), 140 deletions(-) rename src/main/java/daybyquest/image/{vo => domain}/BaseImageProperties.java (95%) create mode 100644 src/main/java/daybyquest/image/domain/Image.java create mode 100644 src/main/java/daybyquest/image/domain/ImageIdentifierGenerator.java create mode 100644 src/main/java/daybyquest/image/domain/Images.java create mode 100644 src/main/java/daybyquest/image/infra/UUIDImageIdentifierGenerator.java delete mode 100644 src/main/java/daybyquest/image/vo/Image.java delete mode 100644 src/main/java/daybyquest/image/vo/ImageIdentifierGenerator.java delete mode 100644 src/main/java/daybyquest/image/vo/Images.java create mode 100644 src/test/java/daybyquest/image/domain/ImageTest.java diff --git a/src/main/java/daybyquest/badge/application/SaveBadgeService.java b/src/main/java/daybyquest/badge/application/SaveBadgeService.java index e85b303..a7f72c5 100644 --- a/src/main/java/daybyquest/badge/application/SaveBadgeService.java +++ b/src/main/java/daybyquest/badge/application/SaveBadgeService.java @@ -3,9 +3,9 @@ import daybyquest.badge.domain.Badge; import daybyquest.badge.domain.Badges; import daybyquest.global.utils.MultipartFileUtils; -import daybyquest.image.vo.Image; -import daybyquest.image.vo.ImageIdentifierGenerator; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.ImageIdentifierGenerator; +import daybyquest.image.domain.Images; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -30,9 +30,9 @@ public SaveBadgeService(final Badges badges, final Images images, @Transactional public void invoke(final String name, final MultipartFile file) { - final String identifier = generator.generateIdentifier(CATEGORY, file.getOriginalFilename()); - images.upload(identifier, MultipartFileUtils.getInputStream(file)); - final Badge badge = new Badge(name, new Image(identifier)); + final String identifier = generator.generate(CATEGORY, file.getOriginalFilename()); + final Image image = images.upload(identifier, MultipartFileUtils.getInputStream(file)); + final Badge badge = new Badge(name, image); badges.save(badge); } } diff --git a/src/main/java/daybyquest/badge/domain/Badge.java b/src/main/java/daybyquest/badge/domain/Badge.java index 35a18d6..17bff51 100644 --- a/src/main/java/daybyquest/badge/domain/Badge.java +++ b/src/main/java/daybyquest/badge/domain/Badge.java @@ -3,7 +3,7 @@ import static jakarta.persistence.GenerationType.IDENTITY; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import jakarta.persistence.Column; import jakarta.persistence.Embedded; import jakarta.persistence.Entity; diff --git a/src/main/java/daybyquest/badge/query/BadgeData.java b/src/main/java/daybyquest/badge/query/BadgeData.java index 0cfbe63..e41a78b 100644 --- a/src/main/java/daybyquest/badge/query/BadgeData.java +++ b/src/main/java/daybyquest/badge/query/BadgeData.java @@ -1,6 +1,6 @@ package daybyquest.badge.query; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import java.time.LocalDateTime; import lombok.Getter; @@ -23,6 +23,6 @@ public BadgeData(final Long id, final String name, final Image image, final Loca } public String getImageIdentifier() { - return image.getImageIdentifier(); + return image.getIdentifier(); } } diff --git a/src/main/java/daybyquest/global/config/PropertiesConfig.java b/src/main/java/daybyquest/global/config/PropertiesConfig.java index c0b3adc..31b02f7 100644 --- a/src/main/java/daybyquest/global/config/PropertiesConfig.java +++ b/src/main/java/daybyquest/global/config/PropertiesConfig.java @@ -1,6 +1,6 @@ package daybyquest.global.config; -import daybyquest.image.vo.BaseImageProperties; +import daybyquest.image.domain.BaseImageProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; diff --git a/src/main/java/daybyquest/group/application/SaveGroupService.java b/src/main/java/daybyquest/group/application/SaveGroupService.java index 441f128..3ebde02 100644 --- a/src/main/java/daybyquest/group/application/SaveGroupService.java +++ b/src/main/java/daybyquest/group/application/SaveGroupService.java @@ -4,9 +4,9 @@ import daybyquest.group.domain.Group; import daybyquest.group.domain.Groups; import daybyquest.group.dto.request.SaveGroupRequest; -import daybyquest.image.vo.Image; -import daybyquest.image.vo.ImageIdentifierGenerator; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.ImageIdentifierGenerator; +import daybyquest.image.domain.Images; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -31,14 +31,14 @@ public SaveGroupService(final Groups groups, final Images images, @Transactional public Long invoke(final Long loginId, final SaveGroupRequest request, final MultipartFile file) { - final String identifier = generator.generateIdentifier(CATEGORY, file.getOriginalFilename()); - images.upload(identifier, MultipartFileUtils.getInputStream(file)); - final Group group = toEntity(request, identifier); + final String identifier = generator.generate(CATEGORY, file.getOriginalFilename()); + final Image image = images.upload(identifier, MultipartFileUtils.getInputStream(file)); + final Group group = toEntity(request, image); return groups.save(loginId, group); } - public Group toEntity(final SaveGroupRequest request, final String imageIdentifier) { + public Group toEntity(final SaveGroupRequest request, final Image image) { return new Group(request.getInterest(), request.getName(), request.getDescription(), - new Image(imageIdentifier)); + image); } } diff --git a/src/main/java/daybyquest/group/domain/Group.java b/src/main/java/daybyquest/group/domain/Group.java index c59af24..f35c72a 100644 --- a/src/main/java/daybyquest/group/domain/Group.java +++ b/src/main/java/daybyquest/group/domain/Group.java @@ -4,7 +4,7 @@ import static lombok.AccessLevel.PROTECTED; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import jakarta.persistence.AttributeOverride; import jakarta.persistence.Column; import jakarta.persistence.Embedded; diff --git a/src/main/java/daybyquest/group/query/GroupData.java b/src/main/java/daybyquest/group/query/GroupData.java index 19f8455..d87e4be 100644 --- a/src/main/java/daybyquest/group/query/GroupData.java +++ b/src/main/java/daybyquest/group/query/GroupData.java @@ -1,6 +1,6 @@ package daybyquest.group.query; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import lombok.Getter; @Getter @@ -32,6 +32,6 @@ public GroupData(final Long id, final String name, final String description, fin } public String getImageIdentifier() { - return image.getImageIdentifier(); + return image.getIdentifier(); } } diff --git a/src/main/java/daybyquest/image/vo/BaseImageProperties.java b/src/main/java/daybyquest/image/domain/BaseImageProperties.java similarity index 95% rename from src/main/java/daybyquest/image/vo/BaseImageProperties.java rename to src/main/java/daybyquest/image/domain/BaseImageProperties.java index b53ab2f..df2f2b5 100644 --- a/src/main/java/daybyquest/image/vo/BaseImageProperties.java +++ b/src/main/java/daybyquest/image/domain/BaseImageProperties.java @@ -1,4 +1,4 @@ -package daybyquest.image.vo; +package daybyquest.image.domain; import lombok.Getter; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/src/main/java/daybyquest/image/domain/Image.java b/src/main/java/daybyquest/image/domain/Image.java new file mode 100644 index 0000000..bedcc97 --- /dev/null +++ b/src/main/java/daybyquest/image/domain/Image.java @@ -0,0 +1,45 @@ +package daybyquest.image.domain; + +import static lombok.AccessLevel.PROTECTED; + +import daybyquest.global.error.exception.InvalidDomainException; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import java.util.Objects; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Embeddable +@NoArgsConstructor(access = PROTECTED) +public class Image { + + private static final int MAX_IDENTIFIER_LENGTH = 255; + + @Column(nullable = false) + private String identifier; + + public Image(String identifier) { + this.identifier = identifier; + validateIdentifier(); + } + + private void validateIdentifier() { + if (identifier == null || identifier.isEmpty() || identifier.length() > MAX_IDENTIFIER_LENGTH) { + throw new InvalidDomainException(); + } + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof Image image)) { + return false; + } + return this.identifier.equals(image.identifier); + } + + @Override + public int hashCode() { + return Objects.hashCode(identifier); + } +} diff --git a/src/main/java/daybyquest/image/domain/ImageIdentifierGenerator.java b/src/main/java/daybyquest/image/domain/ImageIdentifierGenerator.java new file mode 100644 index 0000000..b7bb9ef --- /dev/null +++ b/src/main/java/daybyquest/image/domain/ImageIdentifierGenerator.java @@ -0,0 +1,6 @@ +package daybyquest.image.domain; + +public interface ImageIdentifierGenerator { + + String generate(final String category, final String originalName); +} diff --git a/src/main/java/daybyquest/image/domain/Images.java b/src/main/java/daybyquest/image/domain/Images.java new file mode 100644 index 0000000..4993316 --- /dev/null +++ b/src/main/java/daybyquest/image/domain/Images.java @@ -0,0 +1,10 @@ +package daybyquest.image.domain; + +import java.io.InputStream; + +public interface Images { + + Image upload(final String identifier, final InputStream imageStream); + + void remove(final String identifier); +} diff --git a/src/main/java/daybyquest/image/infra/S3Images.java b/src/main/java/daybyquest/image/infra/S3Images.java index 79885bc..b88c66c 100644 --- a/src/main/java/daybyquest/image/infra/S3Images.java +++ b/src/main/java/daybyquest/image/infra/S3Images.java @@ -5,7 +5,8 @@ import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; import daybyquest.global.error.exception.InvalidFileException; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.Images; import java.io.IOException; import java.io.InputStream; import org.springframework.beans.factory.annotation.Value; @@ -24,7 +25,7 @@ public S3Images(final AmazonS3 amazonS3, @Value("${aws.bucket}") final String bu } @Override - public void upload(final String identifier, final InputStream imageStream) { + public Image upload(final String identifier, final InputStream imageStream) { try { final ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(imageStream.available()); @@ -32,6 +33,7 @@ public void upload(final String identifier, final InputStream imageStream) { bucket, identifier, imageStream, metadata ); amazonS3.putObject(putObjectRequest); + return new Image(identifier); } catch (IOException e) { throw new InvalidFileException(); } diff --git a/src/main/java/daybyquest/image/infra/UUIDImageIdentifierGenerator.java b/src/main/java/daybyquest/image/infra/UUIDImageIdentifierGenerator.java new file mode 100644 index 0000000..9a8090d --- /dev/null +++ b/src/main/java/daybyquest/image/infra/UUIDImageIdentifierGenerator.java @@ -0,0 +1,19 @@ +package daybyquest.image.infra; + +import daybyquest.image.domain.ImageIdentifierGenerator; +import java.util.UUID; +import org.springframework.stereotype.Component; + +@Component +public class UUIDImageIdentifierGenerator implements ImageIdentifierGenerator { + + private static final String CATEGORY_DELIMITER = "/"; + + private static final String UUID_DELIMITER = "-"; + + @Override + public String generate(final String category, final String originalName) { + final String uuid = UUID.randomUUID().toString(); + return category + CATEGORY_DELIMITER + uuid + UUID_DELIMITER + originalName; + } +} diff --git a/src/main/java/daybyquest/image/vo/Image.java b/src/main/java/daybyquest/image/vo/Image.java deleted file mode 100644 index 5bb40a7..0000000 --- a/src/main/java/daybyquest/image/vo/Image.java +++ /dev/null @@ -1,36 +0,0 @@ -package daybyquest.image.vo; - -import static lombok.AccessLevel.PROTECTED; - -import jakarta.persistence.Column; -import jakarta.persistence.Embeddable; -import java.util.Objects; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@Embeddable -@NoArgsConstructor(access = PROTECTED) -public class Image { - - @Column(nullable = false) - private String imageIdentifier; - - public Image(String imageIdentifier) { - this.imageIdentifier = imageIdentifier; - } - - @Override - public boolean equals(Object o) { - if (!(o instanceof Image image)) { - return false; - } - return this.imageIdentifier.equals(image.imageIdentifier); - } - - @Override - public int hashCode() { - return Objects.hashCode(imageIdentifier); - } - -} diff --git a/src/main/java/daybyquest/image/vo/ImageIdentifierGenerator.java b/src/main/java/daybyquest/image/vo/ImageIdentifierGenerator.java deleted file mode 100644 index 2676fd6..0000000 --- a/src/main/java/daybyquest/image/vo/ImageIdentifierGenerator.java +++ /dev/null @@ -1,13 +0,0 @@ -package daybyquest.image.vo; - -import java.util.UUID; -import org.springframework.stereotype.Component; - -@Component -public class ImageIdentifierGenerator { - - public String generateIdentifier(final String category, final String originalName) { - final String uuid = UUID.randomUUID().toString(); - return category + "/" + uuid + originalName; - } -} diff --git a/src/main/java/daybyquest/image/vo/Images.java b/src/main/java/daybyquest/image/vo/Images.java deleted file mode 100644 index d896093..0000000 --- a/src/main/java/daybyquest/image/vo/Images.java +++ /dev/null @@ -1,10 +0,0 @@ -package daybyquest.image.vo; - -import java.io.InputStream; - -public interface Images { - - void upload(final String identifier, final InputStream imageStream); - - void remove(final String identifier); -} diff --git a/src/main/java/daybyquest/interest/application/SaveInterestService.java b/src/main/java/daybyquest/interest/application/SaveInterestService.java index 516d58a..7fa7bdb 100644 --- a/src/main/java/daybyquest/interest/application/SaveInterestService.java +++ b/src/main/java/daybyquest/interest/application/SaveInterestService.java @@ -1,9 +1,9 @@ package daybyquest.interest.application; import daybyquest.global.utils.MultipartFileUtils; -import daybyquest.image.vo.Image; -import daybyquest.image.vo.ImageIdentifierGenerator; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.ImageIdentifierGenerator; +import daybyquest.image.domain.Images; import daybyquest.interest.domain.Interest; import daybyquest.interest.domain.Interests; import daybyquest.interest.dto.request.SaveInterestRequest; @@ -31,9 +31,9 @@ public SaveInterestService(final Interests interests, final Images images, @Transactional public void invoke(final SaveInterestRequest request, final MultipartFile file) { - final String identifier = generator.generateIdentifier(CATEGORY, file.getOriginalFilename()); - images.upload(identifier, MultipartFileUtils.getInputStream(file)); - final Interest interest = new Interest(request.getName(), new Image(identifier)); + final String identifier = generator.generate(CATEGORY, file.getOriginalFilename()); + final Image image = images.upload(identifier, MultipartFileUtils.getInputStream(file)); + final Interest interest = new Interest(request.getName(), image); interests.save(interest); } } diff --git a/src/main/java/daybyquest/interest/domain/Interest.java b/src/main/java/daybyquest/interest/domain/Interest.java index 5838929..326b132 100644 --- a/src/main/java/daybyquest/interest/domain/Interest.java +++ b/src/main/java/daybyquest/interest/domain/Interest.java @@ -1,7 +1,7 @@ package daybyquest.interest.domain; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import jakarta.persistence.Column; import jakarta.persistence.Embedded; import jakarta.persistence.Entity; @@ -31,7 +31,7 @@ public Interest(final String name, final Image image) { } public String getImageIdentifier() { - return image.getImageIdentifier(); + return image.getIdentifier(); } private void validateName() { diff --git a/src/main/java/daybyquest/post/application/SavePostService.java b/src/main/java/daybyquest/post/application/SavePostService.java index 6d36509..5feddba 100644 --- a/src/main/java/daybyquest/post/application/SavePostService.java +++ b/src/main/java/daybyquest/post/application/SavePostService.java @@ -1,9 +1,9 @@ package daybyquest.post.application; import daybyquest.global.utils.MultipartFileUtils; -import daybyquest.image.vo.Image; -import daybyquest.image.vo.ImageIdentifierGenerator; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.ImageIdentifierGenerator; +import daybyquest.image.domain.Images; import daybyquest.post.domain.Post; import daybyquest.post.domain.Posts; import daybyquest.post.dto.request.SavePostRequest; @@ -39,9 +39,8 @@ public Long invoke(final Long loginId, final SavePostRequest request, private List toImageList(final List files) { return files.stream().map((file) -> { - final String identifier = generator.generateIdentifier(CATEGORY, file.getOriginalFilename()); - images.upload(identifier, MultipartFileUtils.getInputStream(file)); - return new Image(identifier); + final String identifier = generator.generate(CATEGORY, file.getOriginalFilename()); + return images.upload(identifier, MultipartFileUtils.getInputStream(file)); }).toList(); } diff --git a/src/main/java/daybyquest/post/domain/Post.java b/src/main/java/daybyquest/post/domain/Post.java index 1277bf9..5e5f97a 100644 --- a/src/main/java/daybyquest/post/domain/Post.java +++ b/src/main/java/daybyquest/post/domain/Post.java @@ -6,7 +6,7 @@ import static jakarta.persistence.GenerationType.IDENTITY; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import jakarta.persistence.CollectionTable; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; diff --git a/src/main/java/daybyquest/post/dto/response/PostResponse.java b/src/main/java/daybyquest/post/dto/response/PostResponse.java index e4943ec..c1473e9 100644 --- a/src/main/java/daybyquest/post/dto/response/PostResponse.java +++ b/src/main/java/daybyquest/post/dto/response/PostResponse.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat.Shape; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.post.query.PostData; import daybyquest.user.dto.response.ProfileResponse; import daybyquest.user.query.Profile; @@ -42,7 +42,7 @@ public PostResponse(final ProfileResponse author, final Long id, final String co public static PostResponse of(final PostData postData, final Profile profile) { return new PostResponse(ProfileResponse.of(profile), postData.getId(), postData.getContent(), postData.getUpdatedAt(), postData.isLiked(), - postData.getImages().stream().map(Image::getImageIdentifier).toList() + postData.getImages().stream().map(Image::getIdentifier).toList() ); } } diff --git a/src/main/java/daybyquest/post/dto/response/PostWithQuestResponse.java b/src/main/java/daybyquest/post/dto/response/PostWithQuestResponse.java index 334bc16..6a045b7 100644 --- a/src/main/java/daybyquest/post/dto/response/PostWithQuestResponse.java +++ b/src/main/java/daybyquest/post/dto/response/PostWithQuestResponse.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat.Shape; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.post.domain.PostState; import daybyquest.post.query.PostData; import daybyquest.user.dto.response.ProfileResponse; @@ -50,7 +50,7 @@ public static PostWithQuestResponse of(final PostData postData, final Profile pr return new PostWithQuestResponse( ProfileResponse.of(profile), postData.getId(), postData.getContent(), postData.getUpdatedAt(), postData.isLiked(), - postData.getImages().stream().map(Image::getImageIdentifier).toList(), + postData.getImages().stream().map(Image::getIdentifier).toList(), new SimpleQuestResponse(postData.getQuestId(), postData.getQuestTitle(), postData.getState()) ); } diff --git a/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java b/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java index f428c3a..0b3f36c 100644 --- a/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java +++ b/src/main/java/daybyquest/post/query/PostDaoQuerydslImpl.java @@ -2,7 +2,7 @@ import static com.querydsl.core.group.GroupBy.groupBy; import static com.querydsl.core.group.GroupBy.list; -import static daybyquest.image.vo.QImage.image; +import static daybyquest.image.domain.QImage.image; import static daybyquest.like.domain.QPostLike.postLike; import static daybyquest.post.domain.QPost.post; import static daybyquest.quest.domain.QQuest.quest; @@ -16,7 +16,7 @@ import daybyquest.global.error.exception.NotExistPostException; import daybyquest.global.query.LongIdList; import daybyquest.global.query.NoOffsetIdPage; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import java.util.Collection; import java.util.List; import java.util.Map; diff --git a/src/main/java/daybyquest/post/query/PostData.java b/src/main/java/daybyquest/post/query/PostData.java index 079e4cc..f9f9106 100644 --- a/src/main/java/daybyquest/post/query/PostData.java +++ b/src/main/java/daybyquest/post/query/PostData.java @@ -1,6 +1,6 @@ package daybyquest.post.query; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.post.domain.PostState; import java.time.LocalDateTime; import java.util.List; diff --git a/src/main/java/daybyquest/quest/application/SaveQuestService.java b/src/main/java/daybyquest/quest/application/SaveQuestService.java index 3b4267f..2a1e501 100644 --- a/src/main/java/daybyquest/quest/application/SaveQuestService.java +++ b/src/main/java/daybyquest/quest/application/SaveQuestService.java @@ -1,9 +1,9 @@ package daybyquest.quest.application; import daybyquest.global.utils.MultipartFileUtils; -import daybyquest.image.vo.Image; -import daybyquest.image.vo.ImageIdentifierGenerator; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.ImageIdentifierGenerator; +import daybyquest.image.domain.Images; import daybyquest.quest.domain.Quest; import daybyquest.quest.domain.Quests; import daybyquest.quest.dto.request.SaveQuestRequest; @@ -38,9 +38,8 @@ public Long invoke(final SaveQuestRequest request, final List fil private List toImageList(final List files) { return files.stream().map((file) -> { - final String identifier = generator.generateIdentifier(CATEGORY, file.getOriginalFilename()); - images.upload(identifier, MultipartFileUtils.getInputStream(file)); - return new Image(identifier); + final String identifier = generator.generate(CATEGORY, file.getOriginalFilename()); + return images.upload(identifier, MultipartFileUtils.getInputStream(file)); }).toList(); } diff --git a/src/main/java/daybyquest/quest/domain/Quest.java b/src/main/java/daybyquest/quest/domain/Quest.java index 7dc766d..0063b9f 100644 --- a/src/main/java/daybyquest/quest/domain/Quest.java +++ b/src/main/java/daybyquest/quest/domain/Quest.java @@ -5,7 +5,7 @@ import static jakarta.persistence.GenerationType.IDENTITY; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import jakarta.persistence.CollectionTable; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; diff --git a/src/main/java/daybyquest/quest/query/QuestData.java b/src/main/java/daybyquest/quest/query/QuestData.java index 5acff0a..f82641e 100644 --- a/src/main/java/daybyquest/quest/query/QuestData.java +++ b/src/main/java/daybyquest/quest/query/QuestData.java @@ -2,7 +2,7 @@ import daybyquest.badge.domain.Badge; import daybyquest.group.domain.Group; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.participant.domain.ParticipantState; import daybyquest.quest.domain.QuestCategory; import java.time.LocalDateTime; @@ -53,7 +53,7 @@ public QuestData(final Long id, final Long groupId, final Long badgeId, final St } public String getImageIdentifier() { - return image.getImageIdentifier(); + return image.getIdentifier(); } public void setState(final ParticipantState state) { diff --git a/src/main/java/daybyquest/user/application/DeleteUserImageService.java b/src/main/java/daybyquest/user/application/DeleteUserImageService.java index 36ef0d9..b0ed448 100644 --- a/src/main/java/daybyquest/user/application/DeleteUserImageService.java +++ b/src/main/java/daybyquest/user/application/DeleteUserImageService.java @@ -1,8 +1,8 @@ package daybyquest.user.application; -import daybyquest.image.vo.BaseImageProperties; -import daybyquest.image.vo.Image; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.BaseImageProperties; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.Images; import daybyquest.user.domain.User; import daybyquest.user.domain.Users; import org.springframework.stereotype.Service; diff --git a/src/main/java/daybyquest/user/application/SaveUserService.java b/src/main/java/daybyquest/user/application/SaveUserService.java index cf59d9f..16c517e 100644 --- a/src/main/java/daybyquest/user/application/SaveUserService.java +++ b/src/main/java/daybyquest/user/application/SaveUserService.java @@ -1,7 +1,7 @@ package daybyquest.user.application; -import daybyquest.image.vo.BaseImageProperties; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.BaseImageProperties; +import daybyquest.image.domain.Image; import daybyquest.user.domain.User; import daybyquest.user.domain.Users; import daybyquest.user.dto.request.SaveUserRequest; diff --git a/src/main/java/daybyquest/user/application/UpdateUserImageService.java b/src/main/java/daybyquest/user/application/UpdateUserImageService.java index 957d950..62ceeb6 100644 --- a/src/main/java/daybyquest/user/application/UpdateUserImageService.java +++ b/src/main/java/daybyquest/user/application/UpdateUserImageService.java @@ -1,10 +1,10 @@ package daybyquest.user.application; import daybyquest.global.utils.MultipartFileUtils; -import daybyquest.image.vo.BaseImageProperties; -import daybyquest.image.vo.Image; -import daybyquest.image.vo.ImageIdentifierGenerator; -import daybyquest.image.vo.Images; +import daybyquest.image.domain.BaseImageProperties; +import daybyquest.image.domain.Image; +import daybyquest.image.domain.ImageIdentifierGenerator; +import daybyquest.image.domain.Images; import daybyquest.user.domain.User; import daybyquest.user.domain.Users; import org.springframework.stereotype.Service; @@ -36,9 +36,9 @@ public UpdateUserImageService(final Users users, final Images images, public void invoke(final Long loginId, final MultipartFile file) { final User user = users.getById(loginId); final String oldIdentifier = user.getImageIdentifier(); - final String identifier = generator.generateIdentifier(CATEGORY, file.getOriginalFilename()); - images.upload(identifier, MultipartFileUtils.getInputStream(file)); - user.updateImage(new Image(identifier)); + final String identifier = generator.generate(CATEGORY, file.getOriginalFilename()); + final Image image = images.upload(identifier, MultipartFileUtils.getInputStream(file)); + user.updateImage(image); if (properties.isNotBase(oldIdentifier)) { images.remove(oldIdentifier); } diff --git a/src/main/java/daybyquest/user/domain/User.java b/src/main/java/daybyquest/user/domain/User.java index f81d6c5..afb497c 100644 --- a/src/main/java/daybyquest/user/domain/User.java +++ b/src/main/java/daybyquest/user/domain/User.java @@ -3,7 +3,7 @@ import static jakarta.persistence.GenerationType.IDENTITY; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import jakarta.persistence.CollectionTable; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; @@ -78,7 +78,7 @@ public User(String username, String email, String name, Image image) { } public String getImageIdentifier() { - return image.getImageIdentifier(); + return image.getIdentifier(); } private void validate() { diff --git a/src/main/java/daybyquest/user/query/ProfileDaoQuerydslImpl.java b/src/main/java/daybyquest/user/query/ProfileDaoQuerydslImpl.java index d03e656..aadb1f8 100644 --- a/src/main/java/daybyquest/user/query/ProfileDaoQuerydslImpl.java +++ b/src/main/java/daybyquest/user/query/ProfileDaoQuerydslImpl.java @@ -44,7 +44,7 @@ private ConstructorExpression profileProjection(final Long userId) { user.id, user.username, user.name, - user.image.imageIdentifier, + user.image.identifier, JPAExpressions.select(post.count()) .from(post) .where(post.userId.eq(userId)), @@ -77,7 +77,7 @@ public Profile getMine(final Long userId) { user.id, user.username, user.name, - user.image.imageIdentifier, + user.image.identifier, JPAExpressions.select(post.count()) .from(post) .where(post.userId.eq(userId)), diff --git a/src/test/java/daybyquest/image/domain/ImageTest.java b/src/test/java/daybyquest/image/domain/ImageTest.java new file mode 100644 index 0000000..0843722 --- /dev/null +++ b/src/test/java/daybyquest/image/domain/ImageTest.java @@ -0,0 +1,55 @@ +package daybyquest.image.domain; + +import static daybyquest.support.util.StringUtils.문자열을_만든다; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import daybyquest.global.error.exception.InvalidDomainException; +import org.junit.jupiter.api.Test; + +public class ImageTest { + + @Test + void 식별자가_없으면_예외를_던진다() { + // given & when & then + assertThatThrownBy(() -> new Image("")) + .isInstanceOf(InvalidDomainException.class); + } + + @Test + void 식별자가_255자를_넘으면_예외를_던진다() { + // given + final String identifier = 문자열을_만든다(256); + + // when & then + assertThatThrownBy(() -> new Image(identifier)) + .isInstanceOf(InvalidDomainException.class); + } + + @Test + void 식별자가_다르면_비교_시_True를_반환한다() { + // given + final String identifier = "식별자"; + final Image image1 = new Image(identifier); + final Image image2 = new Image(identifier); + + // when + final boolean actual = image1.equals(image2); + + // then + assertThat(actual).isTrue(); + } + + @Test + void 식별자가_다르면_비교_시_False를_반환한다() { + // given + final Image image1 = new Image("식별자1"); + final Image image2 = new Image("식별자2"); + + // when + final boolean actual = image1.equals(image2); + + // then + assertThat(actual).isFalse(); + } +} diff --git a/src/test/java/daybyquest/interest/domain/InterestTest.java b/src/test/java/daybyquest/interest/domain/InterestTest.java index 5301e6d..18c2308 100644 --- a/src/test/java/daybyquest/interest/domain/InterestTest.java +++ b/src/test/java/daybyquest/interest/domain/InterestTest.java @@ -3,7 +3,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; diff --git a/src/test/java/daybyquest/post/domain/PostTest.java b/src/test/java/daybyquest/post/domain/PostTest.java index 96a1574..ac1d8b4 100644 --- a/src/test/java/daybyquest/post/domain/PostTest.java +++ b/src/test/java/daybyquest/post/domain/PostTest.java @@ -5,7 +5,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import java.util.Collections; import java.util.List; import org.junit.jupiter.api.Nested; diff --git a/src/test/java/daybyquest/quest/domain/QuestTest.java b/src/test/java/daybyquest/quest/domain/QuestTest.java index ecc7d54..a3ee962 100644 --- a/src/test/java/daybyquest/quest/domain/QuestTest.java +++ b/src/test/java/daybyquest/quest/domain/QuestTest.java @@ -8,7 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertAll; import daybyquest.global.error.exception.InvalidDomainException; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import java.time.LocalDateTime; import java.util.List; import org.junit.jupiter.api.Nested; diff --git a/src/test/java/daybyquest/support/fixture/BadgeFixtures.java b/src/test/java/daybyquest/support/fixture/BadgeFixtures.java index b6ee63f..c85fe55 100644 --- a/src/test/java/daybyquest/support/fixture/BadgeFixtures.java +++ b/src/test/java/daybyquest/support/fixture/BadgeFixtures.java @@ -1,11 +1,11 @@ package daybyquest.support.fixture; import daybyquest.badge.domain.Badge; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import org.springframework.test.util.ReflectionTestUtils; public enum BadgeFixtures { - + BADGE_1("뱃지1", "badge1.png"), BADGE_2("뱃지2", "badge2.png"), BADGE_3("뱃지3", "badge3.png"), diff --git a/src/test/java/daybyquest/support/fixture/GroupFixtures.java b/src/test/java/daybyquest/support/fixture/GroupFixtures.java index d64af4a..d215e4b 100644 --- a/src/test/java/daybyquest/support/fixture/GroupFixtures.java +++ b/src/test/java/daybyquest/support/fixture/GroupFixtures.java @@ -1,7 +1,7 @@ package daybyquest.support.fixture; import daybyquest.group.domain.Group; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import org.springframework.test.util.ReflectionTestUtils; public enum GroupFixtures { diff --git a/src/test/java/daybyquest/support/fixture/InterestFixtures.java b/src/test/java/daybyquest/support/fixture/InterestFixtures.java index 7023753..d4c5e82 100644 --- a/src/test/java/daybyquest/support/fixture/InterestFixtures.java +++ b/src/test/java/daybyquest/support/fixture/InterestFixtures.java @@ -1,6 +1,6 @@ package daybyquest.support.fixture; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.interest.domain.Interest; public enum InterestFixtures { diff --git a/src/test/java/daybyquest/support/fixture/PostFixtures.java b/src/test/java/daybyquest/support/fixture/PostFixtures.java index 9f69ad1..5870d54 100644 --- a/src/test/java/daybyquest/support/fixture/PostFixtures.java +++ b/src/test/java/daybyquest/support/fixture/PostFixtures.java @@ -3,7 +3,7 @@ import static daybyquest.post.domain.PostState.SUCCESS; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.post.domain.Post; import daybyquest.quest.domain.Quest; import daybyquest.user.domain.User; diff --git a/src/test/java/daybyquest/support/fixture/QuestFixtures.java b/src/test/java/daybyquest/support/fixture/QuestFixtures.java index 65f5cc2..001a762 100644 --- a/src/test/java/daybyquest/support/fixture/QuestFixtures.java +++ b/src/test/java/daybyquest/support/fixture/QuestFixtures.java @@ -2,7 +2,7 @@ import daybyquest.badge.domain.Badge; import daybyquest.group.domain.Group; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.quest.domain.Quest; import java.time.LocalDateTime; import java.util.List; diff --git a/src/test/java/daybyquest/support/fixture/UserFixtures.java b/src/test/java/daybyquest/support/fixture/UserFixtures.java index 3ef42a8..9f4468d 100644 --- a/src/test/java/daybyquest/support/fixture/UserFixtures.java +++ b/src/test/java/daybyquest/support/fixture/UserFixtures.java @@ -1,6 +1,6 @@ package daybyquest.support.fixture; -import daybyquest.image.vo.Image; +import daybyquest.image.domain.Image; import daybyquest.user.domain.User; import org.springframework.test.util.ReflectionTestUtils;