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

[feat] #185 비디오 조회수, 좋아요수 바로 반영하도록 수정 #187

Merged
merged 2 commits into from
May 17, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.numble.team3.like.domain.LikeVideo;
import com.numble.team3.like.domain.LikeVideoUtils;
import com.numble.team3.like.infra.JpaLikeVideoRepository;
import com.numble.team3.video.domain.Video;
import com.numble.team3.video.domain.enums.VideoCategory;
import com.numble.team3.video.infra.JpaVideoRepository;
import java.util.ArrayList;
Expand All @@ -41,63 +42,85 @@ public class LikeVideoService {

@Transactional
public void addLike(UserInfo userInfo, Long videoId, VideoCategory category) {
Video video =
videoRepository
.findByAccountIdAndId(userInfo.getAccountId(), videoId)
.orElseThrow(VideoNotFoundException::new);
video.changeLikeCountMinusForDev();

likeRepository.save(
new LikeVideo(
videoRepository.findById(videoId).orElseThrow(VideoNotFoundException::new),
userInfo.getAccountId(),
category));
new LikeVideo(
videoRepository.findById(videoId).orElseThrow(VideoNotFoundException::new),
userInfo.getAccountId(),
category));
}

@Transactional
public void deleteLike(UserInfo userInfo, Long videoId) {
Video video =
videoRepository
.findByAccountIdAndId(userInfo.getAccountId(), videoId)
.orElseThrow(VideoNotFoundException::new);
video.changeLikeCountMinusForDev();

LikeVideo like =
likeRepository.getLikeByAccountIdAndVideoId(userInfo.getAccountId(), videoId)
.orElseThrow(LikeVideoNotFoundException::new);
likeRepository
.getLikeByAccountIdAndVideoId(userInfo.getAccountId(), videoId)
.orElseThrow(LikeVideoNotFoundException::new);

likeRepository.delete(like);
}

@Transactional(readOnly = true)
public GetLikeListDto getLikesByCategory(
UserInfo userInfo,
String categoryName,
Long likeId,
int size) {
UserInfo userInfo, String categoryName, Long likeId, int size) {

List<GetLikeVideoDto> likes = likeRepository.getLikesByCategory(userInfo, categoryName, likeId, size);
List<GetLikeVideoDto> likes =
likeRepository.getLikesByCategory(userInfo, categoryName, likeId, size);

if (likes.size() < size) {
return new GetLikeListDto(likes, null);
}
else {
} else {
return new GetLikeListDto(likes, likes.get(likes.size() - 1).getId());
}
}

@Transactional(readOnly = true)
public GetAllLikeVideoListDto getLikesHierarchy(UserInfo userInfo) {
Map<String, List<GetLikeVideoDto>> rankHierarchy = Arrays.stream(VideoCategory.values()).collect(
groupingBy(value -> value.getName(), mapping(
value -> likeRepository.getAllLikesWithLimit(userInfo.getAccountId(), value, limit)
.stream().map(like -> GetLikeVideoDto.fromEntity(like)).collect(toList()),
Collector.of(ArrayList::new, List::addAll, (likes, getLikeDto) -> {
likes.addAll(getLikeDto);
return likes;
})
)));

Map<String, GetLikeVideoCategoryListLimitDto> result = rankHierarchy.entrySet().stream()
.collect(Collectors.toMap(
entry -> entry.getKey(),
entry -> {
List<GetLikeVideoDto> list = rankHierarchy.get(entry.getKey());
if (list.size() == 0) {
return new GetLikeVideoCategoryListLimitDto(list, null);
} else {
return new GetLikeVideoCategoryListLimitDto(list, list.get(list.size() - 1).getId());
}
}
));
Map<String, List<GetLikeVideoDto>> rankHierarchy =
Arrays.stream(VideoCategory.values())
.collect(
groupingBy(
value -> value.getName(),
mapping(
value ->
likeRepository
.getAllLikesWithLimit(userInfo.getAccountId(), value, limit)
.stream()
.map(like -> GetLikeVideoDto.fromEntity(like))
.collect(toList()),
Collector.of(
ArrayList::new,
List::addAll,
(likes, getLikeDto) -> {
likes.addAll(getLikeDto);
return likes;
}))));

Map<String, GetLikeVideoCategoryListLimitDto> result =
rankHierarchy.entrySet().stream()
.collect(
Collectors.toMap(
entry -> entry.getKey(),
entry -> {
List<GetLikeVideoDto> list = rankHierarchy.get(entry.getKey());
if (list.size() == 0) {
return new GetLikeVideoCategoryListLimitDto(list, null);
} else {
return new GetLikeVideoCategoryListLimitDto(
list, list.get(list.size() - 1).getId());
}
}));

return new GetAllLikeVideoListDto(result);
}
Expand All @@ -113,6 +136,8 @@ public List<GetVideoRankDto> getRank(String standard) {

public List<GetVideoRankDto> getRank(String standard, VideoCategory videoCategory) {
List<GetVideoRankDto> dtos = likeVideoUtils.getDayRanking(standard);
return dtos.stream().filter(dto -> dto.getVideoDto().getVideoCategory().equals(videoCategory)).collect(toList());
return dtos.stream()
.filter(dto -> dto.getVideoDto().getVideoCategory().equals(videoCategory))
.collect(toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ public GetVideoListDto getAllVideoByCondition(
return GetVideoListDto.fromEntities(videos, likeVideoIds);
}

@Transactional(readOnly = true)
@Transactional
public GetVideoDetailDto getVideoById(Long videoId) {
//todo: 개발용 조회수 바로 반영
Video video = videoRepository.findById(videoId).orElseThrow(VideoNotFoundException::new);
video.changeViewCountPlusForDev();

videoUtils.updateViewCount(videoId);
return GetVideoDetailDto.fromEntity(
videoRepository.findById(videoId).orElseThrow(VideoNotFoundException::new));
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/numble/team3/video/domain/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class Video extends BaseTimeEntity {

@ColumnDefault(value = "0")
@Column(name = "view_count")
private Long view;
private Long view = 0L;

@ColumnDefault(value = "0")
@Column(name = "like_count")
private Long like;
private Long like = 0L;

@Column(name = "video_url")
private String videoUrl;
Expand Down Expand Up @@ -104,12 +104,29 @@ public void addComment(Comment comment) {
}

public void changeVideo(
String title, String content, String videoUrl, String thumbnailUrl, VideoCategory category, VideoType type) {
String title,
String content,
String videoUrl,
String thumbnailUrl,
VideoCategory category,
VideoType type) {
this.title = title;
this.content = content;
this.videoUrl = videoUrl;
this.thumbnailUrl = thumbnailUrl;
this.category = category;
this.type = type;
}

public void changeViewCountPlusForDev() {
this.view += 1L;
}

public void changeLikeCountPlusForDev() {
this.like += 1L;
}

public void changeLikeCountMinusForDev(){
this.like -= 1L;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,59 +62,6 @@ void beforeEach() {
likeVideoService = new LikeVideoService(likeRepository, videoRepository, likeVideoUtils);
}

@Test
void addLike_성공_테스트() throws Exception {
// given
Video video = createVideo();
UserInfo userInfo = createUserInfo(1L, RoleType.ROLE_USER);

given(videoRepository.findById(anyLong())).willReturn(Optional.ofNullable(video));
given(likeRepository.save(any(LikeVideo.class))).willReturn(null);

// when
likeVideoService.addLike(userInfo, 1L, VideoCategory.CAT);

// then
verify(likeRepository).save(any(LikeVideo.class));
}

@Test
void addLike_없는_비디오_id_실패_테스트() {
// given
UserInfo userInfo = createUserInfo(1L, RoleType.ROLE_USER);

given(videoRepository.findById(anyLong())).willReturn(Optional.empty());

// when, then
assertThrows(VideoNotFoundException.class, () -> likeVideoService.addLike(userInfo, 1L, VideoCategory.CAT));
}

@Test
void deleteLike_성공_테스트() throws Exception {
// given
UserInfo userInfo = createUserInfo(1L, RoleType.ROLE_USER);
LikeVideo likeVideo = createLike();

given(likeRepository.getLikeByAccountIdAndVideoId(anyLong(), anyLong())).willReturn(Optional.ofNullable(likeVideo));

// when
likeVideoService.deleteLike(userInfo, 1L);

// then
verify(likeRepository).delete(any(LikeVideo.class));
}

@Test
void deleteLike_없는_좋아요_id_실패_테스트() {
// given
UserInfo userInfo = createUserInfo(1L, RoleType.ROLE_USER);

given(likeRepository.getLikeByAccountIdAndVideoId(anyLong(), anyLong())).willReturn(Optional.empty());

// when, then
assertThrows(LikeVideoNotFoundException.class, () -> likeVideoService.deleteLike(userInfo, 1L));
}

@Test
void getLikesByCategory_리스트_없는_성공_테스트() {
// given
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/com/numble/team3/video/VideoEntityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.numble.team3.video;

import static com.numble.team3.factory.UserInfoFactory.createUserInfo;
import static com.numble.team3.factory.dto.LikeVideoDtoFactory.createGetLikeDto;
import static com.numble.team3.factory.entity.LikeVideoEntityFactory.*;
import static com.numble.team3.factory.entity.LikeVideoEntityFactory.createLike;
import static com.numble.team3.factory.entity.VideoEntityFactory.createVideo;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.mockito.Mockito.verify;

import com.numble.team3.video.domain.Video;
import com.numble.team3.video.domain.enums.VideoCategory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class VideoEntityTest {
Video video;

@BeforeEach
public void beforeEach() throws Exception {
video =
Video.builder()
.videoUrl(null)
.videoDuration(10L)
.title("title")
.content("content")
.thumbnailUrl(null)
.category(VideoCategory.CAT)
.account(null)
.build();
}

@Test
public void 비디오_조회수_증가() throws Exception {
video.changeViewCountPlusForDev();
assertEquals(video.getView(), 1);
}

@Test
public void 비디오_좋아요수_감소() throws Exception {
video.changeLikeCountMinusForDev();
assertEquals(video.getLike(),-1);
}

@Test
public void 비디오_좋아요수_증가() throws Exception {
video.changeLikeCountPlusForDev();
assertEquals(video.getLike(), 1);
}
}