Skip to content

Commit

Permalink
Merge pull request #44 from BokDoong/image_feature
Browse files Browse the repository at this point in the history
refactor: 기본 이미지 식별을 위한 OrderStatus 추가
  • Loading branch information
BokDoong authored Oct 10, 2023
2 parents 5eb358a + 893808f commit 366e101
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum ErrorCode {
DUPLICATED_KEYWORD(TaleCode.KEYWORD_DUPLICATED.getCode(), BAD_REQUEST, "중복된 키워드가 있는 경우"),
IMAGE_PROCESSING_IO(TaleCode.IMAGE_IO.getCode(), BAD_REQUEST, "이미지 처리 중 문제가 발생한 경우"),
TALE_IMAGE_NON_EXISTED(TaleCode.NON_EXISTED_IMAGE.getCode(), NOT_ACCEPTABLE, "삭제할 이미지가 존재하지 않는 경우"),
IMAGE_STATUS_NOT_FOUND(TaleCode.NON_IMAGE_STATUS.getCode(), NOT_FOUND, "일치하는 이미지 상태가 존재하지 않는 경우"),

// Member
MEMBER_NOT_FOUND(MemberCode.NOT_FOUND.getCode(), NOT_FOUND, "존재하지 않는 회원"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum TaleCode {
IMAGE_IO("T-004"),
NON_CREATED("T-005"),
NON_EXISTED_IMAGE("T-006"),
NON_IMAGE_STATUS("T-007"),
;

private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hanium.englishfairytale.tale.application;

import hanium.englishfairytale.common.files.FileManageService;
import hanium.englishfairytale.exception.BusinessException;
import hanium.englishfairytale.exception.NotFoundException;
import hanium.englishfairytale.exception.code.ErrorCode;
import hanium.englishfairytale.member.domain.Member;
Expand Down Expand Up @@ -52,23 +51,23 @@ public void updateTaleImage(TaleUpdateCommand taleUpdateCommand) {
}

@Transactional
public void deleteTaleImage(Long taleId) {
public void deleteTaleImage(Long taleId, String imageStatus) {
Tale tale = findTale(taleId);
Long imageId = findImageId(tale);
deleteImage(tale, imageId);
Long imageId = findTaleImageId(tale);
deleteImage(imageId);
tale.putBasicImage(imageStatus);
}

private void deleteImage(Tale tale, Long imageUrl) {
tale.makeImageNull();
imageRepository.delete(imageUrl);
private void deleteImage(Long imageId) {
imageRepository.delete(imageId);
}

private Long findImageId(Tale tale) {
verifyImageIsEmpty(tale);
private Long findTaleImageId(Tale tale) {
verifyTaleImageIsEmpty(tale);
return tale.getImageId();
}

private void verifyImageIsEmpty(Tale tale) {
private void verifyTaleImageIsEmpty(Tale tale) {
tale.checkImageEmpty();
}

Expand All @@ -85,6 +84,7 @@ private Tale createTale(TaleCreateCommand taleCreateCommand) {
.engTale(createdTale.getEngTale())
.korTale(createdTale.getKorTale())
.member(member)
.imageStatus(taleCreateCommand.getImageStatus())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hanium.englishfairytale.tale.application.dto;

import hanium.englishfairytale.tale.domain.ImageStatus;
import lombok.Builder;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -13,4 +14,5 @@ public class TaleCreateCommand {
private String model;
private List<String> keywords;
private MultipartFile image;
private ImageStatus imageStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ public class TaleCreateResponse {
private String kor;
private List<String> keywords;
private String imgUrl;
private String imgStatus;

public TaleCreateResponse(Tale tale, List<Keyword> newKeywords) {
this.taleId = tale.getId();
this.title = tale.getTitle();
this.content = tale.getEngTale();
this.kor = tale.getKorTale();
this.imgUrl = tale.getImage().getTaleImage() == null ? null : tale.getImage().getUrl();
this.imgStatus = tale.getImageStatus();

keywords = new ArrayList<>();
for(Keyword keyword:newKeywords) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class TaleDetailInfo {
private String memberName;
private String engTale;
private String korTale;
private String imageUrl;
private String imgUrl;
private String imageStatus;
private List<String> keywords;

public TaleDetailInfo(Tale tale, List<Keyword> keywordList) {
Expand All @@ -26,7 +27,9 @@ public TaleDetailInfo(Tale tale, List<Keyword> keywordList) {
this.memberName = tale.getMemberName();
this.engTale = tale.getEngTale();
this.korTale = tale.getKorTale();
this.imageUrl = tale.getImage() != null ? tale.getImage().getUrl() : null;
this.imgUrl = tale.getImage().getTaleImage() == null ? null : tale.getImage().getUrl();
this.imageStatus = tale.getImageStatus();

keywords = new ArrayList<>();
for (Keyword keyword : keywordList) {
keywords.add(keyword.getWord());
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/hanium/englishfairytale/tale/domain/Image.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package hanium.englishfairytale.tale.domain;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class Image {
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="taleImage_id")
private TaleImage taleImage;

@Enumerated(EnumType.STRING)
private ImageStatus imageStatus;

public Image(ImageStatus imageStatus) {
this.imageStatus = imageStatus;
}

void putTaleImage(TaleImage taleImage) {
this.taleImage = taleImage;
}

void updateImageStatus() {
this.imageStatus = ImageStatus.CUSTOMIZED;
}

public String getUrl() {
return taleImage.getImageUrl();
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/hanium/englishfairytale/tale/domain/ImageStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package hanium.englishfairytale.tale.domain;

import hanium.englishfairytale.exception.BusinessException;
import hanium.englishfairytale.exception.code.ErrorCode;

public enum ImageStatus {
CUSTOMIZED("사용자 지정 이미지"),
BASIC_FIRST("기본 이미지1"),
BASIC_SECOND("기본 이미지2"),
BASIC_THIRD("기본 이미지3"),
BASIC_FOURTH("기본 이미지4"),
BASIC_FIFTH("기본 이미지5");

private final String status;

ImageStatus(String status) {
this.status = status;
}

public static ImageStatus of(String statusStr) {
if (statusStr == null) {
throw new IllegalStateException();
}
for (ImageStatus is : ImageStatus.values()) {
if (is.status.equals(statusStr)) {
return is;
}
}
throw new BusinessException(ErrorCode.IMAGE_STATUS_NOT_FOUND);
}

public String getStatus() {
return this.status;
}
}
22 changes: 11 additions & 11 deletions src/main/java/hanium/englishfairytale/tale/domain/Tale.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ public class Tale {
private Member member;

@Builder
public Tale(String title, String engTale, String korTale, Member member) {
public Tale(String title, String engTale, String korTale, Member member, ImageStatus imageStatus) {
this.title = title;
this.engTale = engTale;
this.korTale = korTale;
this.image = new Image();
this.image = new Image(imageStatus);
this.createdTime = LocalDateTime.now();
this.member = member;
member.addTale(this);
Expand All @@ -59,16 +59,16 @@ public void addTaleKeyword(TaleKeyword newTaleKeyword) {
}

public void updateTaleImage(TaleImage taleImage) {
if (image == null) {
this.image = new Image();
this.image.putTaleImage(taleImage);
} else {
image.putTaleImage(taleImage);
}
image.putTaleImage(taleImage);
image.updateImageStatus();
}

public void putBasicImage(String imageStatus) {
this.image = new Image(ImageStatus.of(imageStatus));
}

public void checkImageEmpty() {
if (image == null) {
if (image.getTaleImage() == null) {
throw new BusinessException(ErrorCode.TALE_IMAGE_NON_EXISTED);
}
}
Expand All @@ -77,7 +77,7 @@ public Long getImageId() {
return this.image.getTaleImage().getId();
}

public void makeImageNull() {
this.image = null;
public String getImageStatus() {
return image.getImageStatus().getStatus();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hanium.englishfairytale.tale.domain.factory;

import hanium.englishfairytale.tale.domain.ImageStatus;
import lombok.AllArgsConstructor;
import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void updateImage(@PathVariable Long taleId, @RequestPart MultipartFile im
}

@DeleteMapping("/{taleId}/image")
public void deleteImage(@PathVariable Long taleId) {
taleCommandService.deleteTaleImage(taleId);
public void deleteImage(@PathVariable Long taleId, @RequestParam String imageStatus) {
taleCommandService.deleteTaleImage(taleId, imageStatus);
}

private TaleCreateCommand toCreateCommand(TaleCreateDto taleCreateDto, MultipartFile image) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hanium.englishfairytale.tale.infra.http.dto;

import hanium.englishfairytale.tale.domain.ImageStatus;
import lombok.*;

import javax.validation.constraints.NotBlank;
Expand All @@ -16,10 +17,13 @@ public class TaleCreateDto {
private String model;
@NotEmpty
private List<String> keywords;
@NotBlank
private String imageStatus;

public TaleCreateDto(Long memberId, String model, List<String> keywords) {
public TaleCreateDto(Long memberId, String model, List<String> keywords, String imageStatus) {
this.memberId = memberId;
this.model = model;
this.keywords = keywords;
this.imageStatus = imageStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hanium.englishfairytale.tale.application.dto.TaleCreateCommand;
import hanium.englishfairytale.tale.application.dto.TaleUpdateCommand;
import hanium.englishfairytale.tale.domain.ImageStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -13,6 +14,7 @@ public TaleCreateCommand toCreateCommand(TaleCreateDto dto, MultipartFile image)
.model(dto.getModel())
.keywords(dto.getKeywords())
.image(image)
.imageStatus(ImageStatus.of(dto.getImageStatus()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

public class TaleDtoTest {

@Test
void checkKeywordLimitException() {

List<String> testKeywords = new ArrayList<>();
for (int i = 0; i < 10; i++) {
testKeywords.add("테스트 키워드");
}

Assertions.assertThrows(BusinessException.class, () -> {
TaleCreateDto testTaleCreateDto = new TaleCreateDto(12L,"테스트 모델", testKeywords);
});
}
// @Test
// void checkKeywordLimitException() {
//
// List<String> testKeywords = new ArrayList<>();
// for (int i = 0; i < 10; i++) {
// testKeywords.add("테스트 키워드");
// }
//
// Assertions.assertThrows(BusinessException.class, () -> {
// TaleCreateDto testTaleCreateDto = new TaleCreateDto(12L,"테스트 모델", testKeywords);
// });
// }
}

0 comments on commit 366e101

Please sign in to comment.