Youtube 영상들이 표시될 페이지에 대한 구상은 대략 이런 식이라고 생각해주시면 될 것 같습니다.
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Youtube {
@Id @GeneratedValue
@Column(name = "youtube_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "candidate_id")
private Candidate candidate;
private String url;
private String title;
private String thumbnail;
private LocalDateTime runtime;
private String description;
private int views;
private int comments;
private LocalDateTime uploadDate;
@Builder
public Youtube(Candidate candidate, String url, String title, String thumbnail, LocalDateTime runtime, String description, int views, int comments, LocalDateTime uploadDate) {
this.candidate = candidate;
this.url = url;
this.title = title;
this.thumbnail = thumbnail;
this.runtime = runtime;
this.description = description;
this.views = views;
this.comments = comments;
this.uploadDate = uploadDate;
}
public void change(LocalDateTime runtime, int views, int comments) {
this.runtime = runtime;
this.views = views;
this.comments = comments;
}
}
@Repository
@RequiredArgsConstructor
public class YoutubeRepository {
private final EntityManager em;
public void save(Youtube youtube) {
em.persist(youtube);
}
public void remove(Youtube youtube) {
em.remove(youtube);
}
public Youtube findYoutube(Long id) {
return em.find(Youtube.class, id);
}
public List<Youtube> findYoutubeList(Long candidateId) {
return em.createQuery("select y from Youtube y" +
" where y.candidate.id = :candidateId", Youtube.class)
.setParameter("candidateId", candidateId)
.getResultList();
}
}
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class YoutubeService {
private final CandidateRepository candidateRepository;
private final YoutubeRepository youtubeRepository;
@Transactional
public Long join(YoutubeRequestDto youtubeRequestDto) {
Long candidateId = youtubeRequestDto.getCandidateId();
Candidate candidate = candidateRepository.findById(candidateId);
Youtube youtube = youtubeRequestDto.toEntity(candidate);
youtubeRepository.save(youtube);
return youtube.getId();
}
@Transactional
public void delete(Long youtubeId) {
Youtube youtube = youtubeRepository.findYoutube(youtubeId);
youtubeRepository.remove(youtube);
}
@Transactional
public void update(Long youtubeId, YoutubeUpdateDto youtubeUpdateDto) {
Youtube youtube = youtubeRepository.findYoutube(youtubeId);
youtube.change(
youtubeUpdateDto.getRuntime(),
youtubeUpdateDto.getViews(),
youtubeUpdateDto.getComments()
);
}
public YoutubeResponseDto findYoutube(Long youtubeId) {
Youtube youtube = youtubeRepository.findYoutube(youtubeId);
return new YoutubeResponseDto(youtube);
}
public List<YoutubeResponseDto> findYoutubeList(Long candidateId) {
List<Youtube> youtubeList = youtubeRepository.findYoutubeList(candidateId);
return youtubeList.stream()
.map(YoutubeResponseDto::new)
.collect(Collectors.toList());
}
}
@Getter @Setter
@Builder
@AllArgsConstructor
public class YoutubeRequestDto {
private Long candidateId;
private String url;
private String title;
private String thumbnail;
private LocalDateTime runtime;
private String description;
private int views;
private int comments;
private LocalDateTime uploadDate;
public Youtube toEntity(Candidate candidate) {
return Youtube.builder()
.candidate(candidate)
.url(url)
.title(title)
.thumbnail(thumbnail)
.runtime(runtime)
.description(description)
.views(views)
.comments(comments)
.uploadDate(uploadDate)
.build();
}
}
@Getter @Setter
public class YoutubeResponseDto {
private String url;
private String title;
private String thumbnail;
private LocalDateTime runtime;
private String description;
private int views;
private int comments;
private LocalDateTime uploadDate;
public YoutubeResponseDto(Youtube youtube) {
this.url = youtube.getUrl();
this.title = youtube.getTitle();
this.thumbnail = youtube.getThumbnail();
this.runtime = youtube.getRuntime();
this.description = youtube.getDescription();
this.views = youtube.getViews();
this.comments = youtube.getComments();
this.uploadDate = youtube.getUploadDate();
}
}
@Getter @Setter
@Builder
@AllArgsConstructor
public class YoutubeUpdateDto {
private LocalDateTime runtime;
private int views;
private int comments;
}