Skip to content

Commit

Permalink
refactor: 미디어 컴포넌트 리팩토링
Browse files Browse the repository at this point in the history
- 썸네일 변환 기능을 미디어 도메인에 통합
- 이미지, 비디오 포맷 검증 규칙 추가
- 외부 도메인에서 요구하는 조회 메소드 추가
- 회원 및 일거리 엔티티와 1:1 연결 설정

Related to: #95
  • Loading branch information
juwon-code committed Oct 7, 2024
1 parent 8953b82 commit f44bd5c
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.prgrms2.java.bitta.jobpost.entity;

import com.prgrms2.java.bitta.apply.entity.Apply;
import com.prgrms2.java.bitta.media.entity.Media;
import com.prgrms2.java.bitta.member.entity.Member;
import jakarta.persistence.*;
import lombok.*;
Expand Down Expand Up @@ -62,6 +63,9 @@ public boolean isClosed() {
return LocalDate.now().isAfter(this.endDate);
}

@OneToOne(mappedBy = "jobPost", cascade = CascadeType.ALL, orphanRemoval = true)
private Media media;

// 해당 게시글에 대한 신청 목록 가져야함
@OneToMany(mappedBy = "jobPost", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Apply> apply = new ArrayList<>();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/prgrms2/java/bitta/media/dto/MediaDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prgrms2.java.bitta.media.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.prgrms2.java.bitta.media.entity.MediaCategory;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
Expand All @@ -17,6 +18,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Schema(title = "미디어 DTO", description = "미디어 파일의 요청 및 응답에 사용하는 DTO입니다.")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MediaDto {
@Schema(title = "미디어 ID (PK)", description = "미디어 파일의 고유 ID 입니다.")
@Min(value = 1, message = "ID는 0 또는 음수가 될 수 없습니다.")
Expand All @@ -43,6 +45,14 @@ public class MediaDto {
@Min(value = 1, message = "ID는 0 또는 음수가 될 수 없습니다.")
private Long feedId;

@Schema(title = "회원 ID (FK)", description = "회원의 ID입니다.")
@Min(value = 1, message = "ID는 0 또는 음수가 될 수 없습니다.")
private Long memberId;

@Schema(title = "일거리 ID (FK)", description = "일거리의 ID입니다.")
@Min(value = 1, message = "ID는 0 또는 음수가 될 수 없습니다.")
private Long jobPostId;

@Schema(title = "파일 저장일시", description = "파일이 저장된 날짜 및 시간입니다.", example = "2023-09-24T14:45:00")
private LocalDateTime createdAt;

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/prgrms2/java/bitta/media/entity/Media.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.prgrms2.java.bitta.media.entity;

import com.prgrms2.java.bitta.feed.entity.Feed;
import com.prgrms2.java.bitta.jobpost.entity.JobPost;
import com.prgrms2.java.bitta.member.entity.Member;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
Expand Down Expand Up @@ -40,6 +42,14 @@ public class Media {
@JoinColumn(name = "feed_id")
private Feed feed;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "jobpost_id")
private JobPost jobPost;

@CreatedDate
@Column(updatable = false, nullable = false)
private LocalDateTime createdAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.prgrms2.java.bitta.media.entity;

public enum MediaCategory {
IMAGE, VIDEO, PROFILE
IMAGE, VIDEO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.prgrms2.java.bitta.media.exception;

public enum MediaException {
BAD_REQUEST(400, "올바르지 않은 접근 경로입니다."),
NOT_FOUND(404, "해당 파일은 존재하지 않습니다."),
INTERNAL_ERROR(500, "파일 처리에 실패했습니다."),
INVALID_FORMAT(500, "올바르지 않은 파일 포맷입니다.");

private MediaTaskException mediaTaskException;

MediaException(int code, String message) {
mediaTaskException = new MediaTaskException(code, message);
}

public MediaTaskException get() {
return mediaTaskException;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class MediaFileTaskException extends RuntimeException {
@AllArgsConstructor
public class MediaTaskException extends RuntimeException {
private int code;
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface MediaRepository extends JpaRepository<Media, Long> {
@Query("SELECT m FROM Media m WHERE m.feed.id = :feedId")
List<Media> findAllByFeedId(@Param("feedId") Long feedId);

Optional<Media> findByFilename(@Param("filename") String filename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
import java.util.List;

public interface MediaService {
void upload(List<MultipartFile> files, Long feedId);
void uploads(List<MultipartFile> files, Long feedId);

void upload(MultipartFile file, Long memberId, Long jobPostId);

void delete(Media media);

void delete(List<MediaDto> mediaDtos);

void delete(MediaDto mediaDto);

void delete(Long feedId);

String getMediaUrl(Media media);

Media getMedia(String mediaUrl);

List<Media> convertDTOs(List<MediaDto> mediaDTOs);

List<MediaDto> convertEntities(List<Media> medias);
Expand Down
Loading

0 comments on commit f44bd5c

Please sign in to comment.