[ElectionPJT] 6. Youtube_Entity, Repository, Service

Jake·2022년 3월 17일
0

프로젝트

목록 보기
6/9

1. 지원 기능

  • 유튜브 게시물 추가
  • 유튜브 게시물 삭제
  • 유튜브 게시물 수정
    • 수정 가능한 사항은 재생 시간(runtime), 조회 수(views), 댓글 수(comments)로 제한했습니다.
    • 이 외의 링크 삭제, 썸네일 변경 등의 사항은 삭제 후 다시 게시해야 할 부분이라고 생각했기 때문입니다.

Youtube 영상들이 표시될 페이지에 대한 구상은 대략 이런 식이라고 생각해주시면 될 것 같습니다.


2. Entity

@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;
    }
}

변경 사항

  • 이전에 제목(title)을 넣는 것을 깜빡했어서 추가했습니다.
  • 빌더를 추가했습니다.
  • update를 위한 change 메서드를 추가했습니다.

3. Repository

@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();
    }
}

4. Service

@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());
    }
}

5. DTO

RequestDto

@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();
    }
}

ResponseDto

@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();
    }
}

UpdateDto

@Getter @Setter
@Builder
@AllArgsConstructor
public class YoutubeUpdateDto {

    private LocalDateTime runtime;
    private int views;
    private int comments;

}
profile
Java/Spring Back-End Developer

0개의 댓글