Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 기존에 등록된 프로젝트에 이미지 추가 시 예외처리 발생하지 않던 에러 해결 #157

Merged
merged 5 commits into from
Sep 19, 2024
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
4 changes: 4 additions & 0 deletions src/main/java/org/cotato/csquiz/common/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public enum ErrorCode {
ALREADY_ATTEND(HttpStatus.CONFLICT, "AT-301", "이미 해당 타입으로 출석한 기록이 있습니다."),
ATTENDANCE_NOT_OPEN(HttpStatus.BAD_REQUEST, "AT-401", "출석 시간이 아닙니다."),

//프로젝트 관련
LOGO_IMAGE_EXIST(HttpStatus.CONFLICT, "PJ-301", "이미 로고 이미지가 존재합니다."),
THUMBNAIL_IMAGE_EXIST(HttpStatus.CONFLICT, "PJ-302", "이미 썸네일 이미지가 존재합니다."),

// 500 오류 -> 서버측에서 처리가 실패한 부분들
WEBSOCKET_SEND_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, "S-001", "소캣 메세지 전송 실패"),
IMAGE_PROCESSING_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "S-002", "이미지 처리에 실패했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.cotato.csquiz.common.entity.BaseTimeEntity;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Project {
public class Project extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.cotato.csquiz.common.entity.BaseTimeEntity;
import org.cotato.csquiz.common.entity.S3Info;
import org.cotato.csquiz.domain.generation.enums.ProjectImageType;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProjectImage {
public class ProjectImage extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down Expand Up @@ -49,15 +50,15 @@ public static ProjectImage logoImage(S3Info s3Info, Long projectId) {
ProjectImageType.LOGO,
s3Info,
projectId,
1);
0);
}

public static ProjectImage thumbnailImage(S3Info s3Info, Long projectId) {
return new ProjectImage(
ProjectImageType.THUMBNAIL,
s3Info,
projectId,
1);
0);
}

public static ProjectImage detailImage(S3Info imageInfo, Long projectId, int imageOrder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.cotato.csquiz.common.entity.BaseTimeEntity;
import org.cotato.csquiz.domain.auth.enums.MemberPosition;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ProjectMember {
public class ProjectMember extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface ProjectImageRepository extends JpaRepository<ProjectImage, Long> {
List<ProjectImage> findAllByProjectId(Long projectId);
List<ProjectImage> findAllByProjectIdInAndProjectImageType(List<Long> projectIds, ProjectImageType projectImageType);
}

boolean existsByProjectIdAndProjectImageType(Long projectId, ProjectImageType projectImageType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.cotato.csquiz.common.entity.S3Info;
import org.cotato.csquiz.common.error.ErrorCode;
import org.cotato.csquiz.common.error.exception.AppException;
import org.cotato.csquiz.common.error.exception.ImageException;
import org.cotato.csquiz.common.s3.S3Uploader;
import org.cotato.csquiz.domain.generation.entity.ProjectImage;
import org.cotato.csquiz.domain.generation.enums.ProjectImageType;
import org.cotato.csquiz.domain.generation.repository.ProjectImageRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -26,8 +29,11 @@ public class ProjectImageService {

@Transactional
public void createProjectImage(Long projectId, MultipartFile logoImage, MultipartFile thumbNailImage,
List<MultipartFile> detailImages)
throws ImageException {
List<MultipartFile> detailImages) throws ImageException {

validateLogoImageExistence(projectId);
validateThumbNailImageExistence(projectId);

List<ProjectImage> newImages = new ArrayList<>();

File webpLogoImage = convertToWebp(convert(logoImage));
Expand All @@ -39,8 +45,8 @@ public void createProjectImage(Long projectId, MultipartFile logoImage, Multipar
newImages.add(ProjectImage.thumbnailImage(thumbNailInfo, projectId));

if (detailImages != null && !detailImages.isEmpty()) {
for (int orderIndex = 1; orderIndex <= detailImages.size(); orderIndex++) {
MultipartFile detailImage = detailImages.get(orderIndex - 1);
for (int orderIndex = 0; orderIndex < detailImages.size(); orderIndex++) {
MultipartFile detailImage = detailImages.get(orderIndex);
File webpDetailImage = convertToWebp(convert(detailImage));
S3Info detailImageInfo = s3Uploader.uploadFiles(webpDetailImage, PROJECT_IMAGE);
newImages.add(ProjectImage.detailImage(detailImageInfo, projectId, orderIndex));
Expand All @@ -49,4 +55,16 @@ public void createProjectImage(Long projectId, MultipartFile logoImage, Multipar

projectImageRepository.saveAll(newImages);
}

private void validateThumbNailImageExistence(Long projectId) {
if (projectImageRepository.existsByProjectIdAndProjectImageType(projectId, ProjectImageType.THUMBNAIL)) {
throw new AppException(ErrorCode.THUMBNAIL_IMAGE_EXIST);
}
}

private void validateLogoImageExistence(Long projectId) {
if (projectImageRepository.existsByProjectIdAndProjectImageType(projectId, ProjectImageType.LOGO)) {
throw new AppException(ErrorCode.LOGO_IMAGE_EXIST);
}
}
}
Loading