본문 바로가기

Playlist/Content

Content, DTO

*/domain/content/entity

 

Content.java

import com.codeit.playlist.domain.base.BaseUpdatableEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "contents")
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Content extends BaseUpdatableEntity {

    /**
     * TMDB ID
     */
    @Column(name = "api_id", nullable = false)
    private Long apiId;

    /**
     * 컨텐츠 타입
     */
    @Column(length = 20, nullable = false)
    private String type;

    /**
     * 컨텐츠 제목
     */
    @Column(length = 100, nullable = false)
    private String title;

    /**
     * 컨텐츠 설명
     */
    @Column(nullable = false, length = 2000)
    private String description;

    /**
     * 썸네일 이미지 URL
     */
    @Column(nullable = false)
    private String thumbnailUrl;

    /**
     * 평균 평점
     */
    @Column(nullable = false)
    private double averageRating;

    /**
     * 리뷰 개수
     */
    @Column(nullable = false)
    private int reviewCount;

    /**
     * 시청자 수
     */
    @Column(nullable = false)
    private long watcherCount;

    public void updateContent(String title, String description, String thumbnailUrl) {
        this.title = title;
        this.description = description;
        this.thumbnailUrl = thumbnailUrl;
    }

    public void setThumbnailUrl(String thumbnailUrl) {
        this.thumbnailUrl = thumbnailUrl;
    }

    public void setWatcherCount(long watcherCount) {
        this.watcherCount = watcherCount;
    }

    /**
     * 컨텐츠 편의 메서드(리뷰용)
     */

    // 리뷰 생성 시 호출
    public void applyReviewCreated(int newRating) {
        int beforeCount = this.reviewCount;
        double beforeAvg = this.averageRating;

        int afterCount = beforeCount + 1;
        double afterAvg = ((beforeAvg * beforeCount) + newRating) / afterCount;

        this.reviewCount = afterCount;
        this.averageRating = afterAvg;
    }

    // 리뷰 수정 시 호출 (기존 평점 -> 새로운 평점)
    public void applyReviewUpdated(int oldRating, int newRating) {
        if (this.reviewCount <= 0) {
            this.reviewCount = 1;
            this.averageRating = newRating;
            return;
        }

        double sum = this.averageRating * this.reviewCount;
        double newSum = sum - oldRating + newRating;
        double afterAvg = newSum / this.reviewCount;

        this.averageRating = afterAvg;
    }

    public void applyReviewDeleted(int deletedRating) {
        int beforeCount = this.reviewCount;

        if (beforeCount <= 0) {
            this.reviewCount = 0;
            this.averageRating = 0;
            return;
        }

        int afterCount = beforeCount - 1;

        if (afterCount == 0) {
            this.reviewCount = 0;
            this.averageRating = 0.0;
        } else {
            double afterAvg =
                    ((this.averageRating * beforeCount) - deletedRating)
                            / afterCount;

            this.reviewCount = afterCount;
            this.averageRating = afterAvg;
        }
    }
}

 

 


 

*/domain/content/dto/data

 

ContentDto.java

import java.util.List;
import java.util.UUID;

public record ContentDto(
        UUID id,    // 콘텐츠 ID
        String type,    // 콘텐츠 타입
        String title,   // 콘텐츠 제목
        String description,     // 콘텐츠 설명
        String thumbnailUrl,    // 썸네일 이미지 URL
        List<String> tags,  // 콘텐츠 태그 목록
        Double averageRating,   // 평균 평점
        Integer reviewCount,    // 리뷰 개수
        Integer watcherCount    // 시청자 수
) {
}

 

ContentSummary.java

import java.util.List;
import java.util.UUID;

public record ContentSummary(
        UUID id,    // 콘텐츠 ID
        String type,    // 콘텐츠 타입
        String title,   // 콘텐츠 제목
        String description,    // 콘텐츠 설명
        String thumbnailUrl,    // 썸네일 이미지 URL
        List<String> tags,  // 콘텐츠 태그 목록
        Double averageRating,   // 평균 평점
        Integer reviewCount     // 리뷰 개수
) {
}

 


 

*/domain/content/dto/request

 

ContentCreateRequest.java

import java.util.List;

public record ContentCreateRequest(
        String type, // 콘텐츠 타입
        String title, // 콘텐츠 제목
        String description, // 콘텐츠 설명
        List<String> tags // 콘텐츠 태그 목록
) {
}

 

 

ContentCursorRequest.java

import com.codeit.playlist.domain.base.SortDirection;

import java.util.List;

public record ContentCursorRequest(
        String typeEqual, // 콘텐츠 타입 Available values : movie, tvSeries, sport
        String keywordLike, // 검색 키워드
        List<String> tagsIn, // 태그 목록, tag가 리스트니까 컨텐츠 하나에 대한 request임
        String cursor, // 커서
        String idAfter, // 보조 커서
        int limit, // 한번에 가져올 개수
        SortDirection sortDirection, // 정렬 방향, Available values : ASCENDING, DECENDING
        String sortBy // 정렬 기준, Available values : createdAt, watcherCount, rate
) {
}

 

 

ContentUpdateRequest.java

import java.util.List;

public record ContentUpdateRequest(
        String title, // 콘텐츠 목록
        String description, // 콘텐츠 설명
        List<String> tags // 콘텐츠 태그 목록
) {
}

 

 


*/domain/content/dto/response

 

CursorResponseContentDto.java

import com.codeit.playlist.domain.content.dto.data.ContentDto;

import java.util.List;

public record CursorResponseContentDto(
        List<ContentDto> data, // 데이터 목록, 콘텐츠
        String nextCursor, // 다음 커서
        String nextIdAfter, // 다음 요청의 보조 커서
        Boolean hasNext, // 다음 데이터가 있는지 여부
        Integer pageSize, // 총 데이터 개수
        String sortBy, // 정렬 기준
        String sortDirection // 정렬 방향
) {
}

 

'Playlist > Content' 카테고리의 다른 글

ContentMapper  (0) 2025.12.17
ContentException, ErrorCode  (0) 2025.12.17
ContentController  (0) 2025.12.17
ContentService  (0) 2025.12.17
Service : BasicContentService  (0) 2025.11.25