Skip to content

Commit

Permalink
Merge pull request #146 from 4m9d/#144/test-code-for-refactoring
Browse files Browse the repository at this point in the history
#144/test code for refactoring
  • Loading branch information
D-w-nJ authored Dec 12, 2023
2 parents 51f2ca5 + 027edcb commit f97a9be
Show file tree
Hide file tree
Showing 39 changed files with 2,687 additions and 1,076 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public CourseEntity save(CourseEntity course) {
course.getMemberId(),
course.getCourseTitle(),
course.getDuration(),
course.getLastViewTime(),
course.getThumbnail(),
course.isScheduled(),
course.getWeeks(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class CourseRepositorySql {

public static final String SAVE = """
INSERT
INTO COURSE (member_id, course_title, course_duration, last_view_time, thumbnail, is_scheduled, weeks,
expected_end_date, daily_target_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
INTO COURSE (member_id, course_title, course_duration, thumbnail, is_scheduled, weeks, expected_end_date,
daily_target_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
"""

public static final String GET_BY_ID = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public ReviewEntity save(ReviewEntity review) {
review.getMemberId(),
review.getLectureId(),
review.getSubmittedRating(),
review.getContent(),
review.getSubmittedDate());
review.getContent());
return getById(jdbcTemplate.queryForObject(LectureRepositorySql.GET_LAST_ID, Long.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class ReviewRepositorySql {

public static final String SAVE = """
INSERT
INTO REVIEW (source_code, member_id, lecture_id, submitted_rating, content, submitted_date)
VALUES (?, ?, ?, ?, ?, ?)
INTO REVIEW (source_code, member_id, lecture_id, submitted_rating, content)
VALUES (?, ?, ?, ?, ?)
"""

public static final String GET_BY_ID = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public class NewLecture {
@Schema(description = "예상 종료 시간", example = "2023-12-31")
private String expectedEndDate;

public static NewLecture createWithoutSchedule(String lectureCode) {
return NewLecture.builder()
.lectureCode(lectureCode)
.build();
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/m9d/sroom/material/MaterialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public List<SubmittedQuizInfoResponse> submitQuizResults(Long memberId, Long cou

List<SubmittedQuizInfoResponse> quizInfoResponseList = new ArrayList<>();
for (SubmittedQuizRequest submittedQuizRequest : submittedQuizList) {
if (quizService.isSubmittedAlready(courseVideoId, submittedQuizRequest.getQuizId())) {
if (quizService.isSubmittedAlready(courseVideoId, submittedQuizRequest.getId())) {
throw new CourseQuizDuplicationException();
}

CourseQuizEntity courseQuizEntity = quizService.createCourseQuizEntity(courseVideoEntity.getCourseId(),
courseVideoEntity.getVideoId(), courseVideoId, submittedQuizRequest.getQuizId(),
courseVideoEntity.getVideoId(), courseVideoId, submittedQuizRequest.getId(),
submittedQuizRequest.toVo(), memberId);
quizInfoResponseList.add(new SubmittedQuizInfoResponse(submittedQuizRequest.getQuizId(),
quizInfoResponseList.add(new SubmittedQuizInfoResponse(submittedQuizRequest.getId(),
courseQuizEntity.getId()));
}
log.info("subject = quizSubmitted, correctAnswerRate = {}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.m9d.sroom.material.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@AllArgsConstructor
@NoArgsConstructor
public class FeedbackRequest {

@JsonProperty("is_satisfactory")
private Boolean is_satisfactory;

public boolean isSatisfactory() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package com.m9d.sroom.material.dto.request;

import com.m9d.sroom.quiz.vo.QuizSubmittedInfo;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.sql.Timestamp;

@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SubmittedQuizRequest {

@Getter
private Long id;

private String submitted_answer;

private Boolean is_correct;

public Long getQuizId() {
return id;
}

public String getSubmittedAnswer() {
return submitted_answer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.m9d.sroom.material.dto.request;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SummaryEditRequest {

private String content;
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/m9d/sroom/member/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public MemberService(MemberRepository memberRepository, JwtUtil jwtUtil) {
public Login authenticateMember(String credential) throws Exception {
GoogleIdToken idToken = verifyCredential(credential);

MemberEntity member = findOrCreateMemberByMemberCode(idToken.getPayload().getSubject());
log.info("member login. memberId = {}", member.getMemberId());
return generateLogin(member, (String) idToken.getPayload().get("picture"));
MemberEntity memberEntity = findOrCreateMember(idToken.getPayload().getSubject());
log.info("member login. memberId = {}", memberEntity.getMemberId());
return generateLogin(memberEntity, (String) idToken.getPayload().get("picture"));
}

public GoogleIdToken verifyCredential(String credential) throws Exception {
Expand All @@ -62,7 +62,7 @@ public GoogleIdToken verifyCredential(String credential) throws Exception {
return idToken;
}

public MemberEntity findOrCreateMemberByMemberCode(String memberCode) {
public MemberEntity findOrCreateMember(String memberCode) {
return memberRepository.findByCode(memberCode)
.orElseGet(() -> createNewMember(memberCode));
}
Expand Down Expand Up @@ -107,19 +107,19 @@ public Login renewTokens(Long memberId, String pictureUrl) {
return generateLogin(member, pictureUrl);
}

public Login generateLogin(MemberEntity member, String picture) {
String accessToken = jwtUtil.generateAccessToken(member.getMemberId(), picture);
public Login generateLogin(MemberEntity memberEntity, String picture) {
String accessToken = jwtUtil.generateAccessToken(memberEntity.getMemberId(), picture);

member.setRefreshToken(jwtUtil.generateRefreshToken(member.getMemberId(), picture));
memberRepository.updateById(member.getMemberId(), member);
memberEntity.setRefreshToken(jwtUtil.generateRefreshToken(memberEntity.getMemberId(), picture));
memberRepository.updateById(memberEntity.getMemberId(), memberEntity);

return Login.builder()
.accessToken(accessToken)
.refreshToken(member.getRefreshToken())
.refreshToken(memberEntity.getRefreshToken())
.accessExpiresAt((Long) jwtUtil.getDetailFromToken(accessToken).get(EXPIRATION_TIME))
.name(member.getMemberName())
.name(memberEntity.getMemberName())
.profile(picture)
.bio(member.getBio())
.bio(memberEntity.getBio())
.build();
}

Expand Down
30 changes: 26 additions & 4 deletions src/main/java/com/m9d/sroom/playlist/PlaylistService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.m9d.sroom.playlist;

import com.m9d.sroom.common.entity.PlaylistEntity;
import com.m9d.sroom.common.entity.PlaylistVideoEntity;
import com.m9d.sroom.common.entity.VideoEntity;
import com.m9d.sroom.common.repository.playlist.PlaylistRepository;
import com.m9d.sroom.common.repository.playlistvideo.PlaylistVideoRepository;
import com.m9d.sroom.common.repository.video.VideoRepository;
import com.m9d.sroom.playlist.vo.Playlist;
import com.m9d.sroom.video.vo.PlaylistItem;
import com.m9d.sroom.playlist.vo.PlaylistWithItemList;
Expand All @@ -14,25 +17,28 @@
import com.m9d.sroom.youtube.YoutubeMapper;
import com.m9d.sroom.youtube.vo.PlaylistItemInfo;
import com.m9d.sroom.youtube.vo.PlaylistVideoInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PlaylistService {

private final PlaylistRepository playlistRepository;

private final YoutubeMapper youtubeService;
private final VideoService videoService;
private final VideoRepository videoRepository;
private final PlaylistVideoRepository playlistVideoRepository;

public PlaylistService(PlaylistRepository playlistRepository, YoutubeMapper youtubeService, VideoService videoService, PlaylistVideoRepository playlistVideoRepository) {
public PlaylistService(PlaylistRepository playlistRepository, YoutubeMapper youtubeService, VideoService videoService, VideoRepository videoRepository, PlaylistVideoRepository playlistVideoRepository) {
this.playlistRepository = playlistRepository;
this.youtubeService = youtubeService;
this.videoService = videoService;
this.videoRepository = videoRepository;
this.playlistVideoRepository = playlistVideoRepository;
}

Expand All @@ -44,11 +50,11 @@ public Playlist getRecentPlaylist(String playlistCode) {
if (playlistEntityOptional.isPresent()) {
reviewCount = playlistEntityOptional.get().getReviewCount();
accumulatedRating = playlistEntityOptional.get().getAccumulatedRating();
if(DateUtil.hasRecentUpdate(playlistEntityOptional.get().getUpdatedAt(), PlaylistConstant.PLAYLIST_UPDATE_THRESHOLD_HOURS)) {
if (DateUtil.hasRecentUpdate(playlistEntityOptional.get().getUpdatedAt(), PlaylistConstant.PLAYLIST_UPDATE_THRESHOLD_HOURS)) {
return playlistEntityOptional.get().toPlaylist();
}
}

return youtubeService.getPlaylist(playlistCode, reviewCount, accumulatedRating);
}

Expand All @@ -57,6 +63,12 @@ public PlaylistWithItemList getRecentPlaylistWithItemList(String playlistCode) {
}

private List<PlaylistItem> getRecentPlaylistItemList(String playlistCode) {
Optional<PlaylistEntity> playlistEntityOptional = playlistRepository.findByCode(playlistCode);
if (playlistEntityOptional.isPresent() && DateUtil.hasRecentUpdate(playlistEntityOptional.get().getUpdatedAt(),
PlaylistConstant.PLAYLIST_UPDATE_THRESHOLD_HOURS)) {
return getPlaylistItemFromDB(playlistEntityOptional.get().getPlaylistId());
}

String nextPageToken = null;
int pageCount = PlaylistConstant.MAX_PLAYLIST_ITEM / PlaylistConstant.DEFAULT_INDEX_COUNT;

Expand Down Expand Up @@ -123,6 +135,16 @@ public void putPlaylistWithItemList(PlaylistWithItemList playlistWithItemList) {
}
}

private List<PlaylistItem> getPlaylistItemFromDB(Long playlistId) {
List<VideoEntity> videoEntityList = videoRepository.getListByPlaylistId(playlistId);

List<PlaylistItem> playlistItemList = new ArrayList<>();
for (int i = 0; i < videoEntityList.size(); i++) {
playlistItemList.add(new PlaylistItem(videoEntityList.get(i).toVideo(), i));
}
return playlistItemList;
}

public EnrollContentInfo getEnrollContentInfo(String playlistCode) {
PlaylistEntity playlistEntity = playlistRepository.getByCode(playlistCode);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/m9d/sroom/quiz/QuizService.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ private QuizSubmittedInfo getSubmittedInfo(Long quizId, Long courseVideoId) {

public void validateSubmittedQuizzes(Long videoId, Long courseVideoId,
List<SubmittedQuizRequest> submittedQuizRequestList) {
QuizEntity quiz = quizRepository.findById(submittedQuizRequestList.get(0).getQuizId())
QuizEntity quiz = quizRepository.findById(submittedQuizRequestList.get(0).getId())
.orElseThrow(QuizNotFoundException::new);

if (!quiz.getVideoId().equals(videoId)) {
throw new QuizIdNotMatchException();
}

for (SubmittedQuizRequest submittedQuiz : submittedQuizRequestList) {
if (courseQuizRepository.findByQuizIdAndCourseVideoId(submittedQuiz.getQuizId(),
if (courseQuizRepository.findByQuizIdAndCourseVideoId(submittedQuiz.getId(),
courseVideoId).isPresent()) {
throw new CourseQuizDuplicationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.relational.core.sql.In;

@Data
@Builder
Expand All @@ -15,4 +16,11 @@ public class ReviewSubmitRequest {
private Integer submittedRating;

private String reviewContent;

public static ReviewSubmitRequest createReview(Integer submittedRating, String reviewContent) {
return ReviewSubmitRequest.builder()
.submittedRating(submittedRating)
.reviewContent(reviewContent)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.m9d.sroom.search.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotNull;

@Setter
@AllArgsConstructor
@NoArgsConstructor
public class LectureTimeRecord {

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class LectureNotFoundException extends NotFoundException {

private static final String MESSAGE = "잘못된 입력입니다. 강의를 찾을 수 없습니다.";
public static final String MESSAGE = "잘못된 입력입니다. 강의를 찾을 수 없습니다.";

public LectureNotFoundException() {
super(MESSAGE);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/m9d/sroom/util/DateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DateUtil {
public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Seoul"));


public Long convertTimeToSeconds(String time) {
public static Long convertTimeToSeconds(String time) {
String[] parts = time.split(":");
int length = parts.length;

Expand Down
Loading

0 comments on commit f97a9be

Please sign in to comment.