From 946aef74e1965c918f527b8caaa8cd006c369483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8C=E1=85=A5=E1=86=BC=E1=84=83=E1=85=AE=E1=84=8B?= =?UTF-8?q?=E1=85=AF=E1=86=AB?= Date: Mon, 2 Oct 2023 04:24:58 +0900 Subject: [PATCH] Fix : add course repository used in lecture service --- .../course/repository/CourseRepository.java | 368 ++++++++++++++++++ 1 file changed, 368 insertions(+) create mode 100644 src/main/java/com/m9d/sroom/course/repository/CourseRepository.java diff --git a/src/main/java/com/m9d/sroom/course/repository/CourseRepository.java b/src/main/java/com/m9d/sroom/course/repository/CourseRepository.java new file mode 100644 index 00000000..1ee74799 --- /dev/null +++ b/src/main/java/com/m9d/sroom/course/repository/CourseRepository.java @@ -0,0 +1,368 @@ +package com.m9d.sroom.course.repository; +import com.m9d.sroom.global.mapper.*; +import com.m9d.sroom.course.dto.response.CourseInfo; +import com.m9d.sroom.course.exception.CourseNotFoundException; +import com.m9d.sroom.course.exception.CourseVideoNotFoundException; +import com.m9d.sroom.course.sql.CourseSqlQuery; +import com.m9d.sroom.lecture.dto.response.CourseBrief; +import com.m9d.sroom.lecture.dto.response.LastVideoInfo; +import com.m9d.sroom.lecture.dto.response.VideoWatchInfo; +import com.m9d.sroom.material.model.CourseAndVideoId; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.dao.IncorrectResultSizeDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Repository; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.util.*; +import static com.m9d.sroom.course.sql.CourseSqlQuery.*; +import static com.m9d.sroom.lecture.sql.LectureSqlQuery.*; +import java.util.HashSet; +import java.util.List; +@Repository +public class CourseRepository { + private final JdbcTemplate jdbcTemplate; + @Autowired + public CourseRepository(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + public List getCourseListByMemberId(Long memberId) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + return jdbcTemplate.query(CourseSqlQuery.GET_COURSES_BY_MEMBER_ID_QUERY, + (rs, rowNum) -> CourseInfo.builder() + .courseId(rs.getLong("course_id")) + .duration(rs.getInt("course_duration")) + .thumbnail(rs.getString("thumbnail")) + .progress(rs.getInt("progress")) + .courseTitle(rs.getString("course_title")) + .lastViewTime(dateFormat.format(rs.getTimestamp("last_view_time"))) + .build(), + memberId); + } + public HashSet getChannelSetByCourseId(Long courseId) { + return new HashSet<>(jdbcTemplate.query(CourseSqlQuery.GET_CHANNELS_BY_COURSE_ID_QUERY, (rs, rowNum) -> rs.getString("channel"), courseId)); + } + public int getTotalLectureCountByCourseId(Long courseId) { + return jdbcTemplate.queryForObject(CourseSqlQuery.GET_TOTAL_LECTURE_COUNT_BY_COURSE_ID_QUERY, (rs, rowNum) -> rs.getInt("lecture_count"), courseId); + } + public int getCompletedVideoCountByCourseId(Long courseId) { + return jdbcTemplate.queryForObject(CourseSqlQuery.GET_COMPLETED_LECTURE_COUNT_BY_COURSE_ID_QUERY, (rs, rowNum) -> rs.getInt("completed_lecture_count"), courseId); + } + public Long saveCourse(Long memberId, String courseTitle, int courseDuration, String thumbnail) { + jdbcTemplate.update(SAVE_COURSE_QUERY, memberId, courseTitle, courseDuration, thumbnail); + return jdbcTemplate.queryForObject(GET_LAST_INSERT_ID_QUERY, Long.class); + } + public Long saveCourseWithSchedule(Long memberId, String courseTitle, int courseDuration, String thumbnail, int weeks, int dailyTargetTime, Date expectedEndDate) { + jdbcTemplate.update(SAVE_COURSE_WITH_SCHEDULE_QUERY, memberId, courseTitle, courseDuration, thumbnail, weeks, dailyTargetTime, 1, expectedEndDate); + return jdbcTemplate.queryForObject(GET_LAST_INSERT_ID_QUERY, Long.class); + } + public Long saveVideo(Video video) { + jdbcTemplate.update(SAVE_VIDEO_QUERY, video.getVideoCode(), video.getDuration(), video.getChannel(), video.getThumbnail(), video.getDescription(), video.getTitle(), video.getLanguage(), video.getLicense(), video.getViewCount(), video.getPublishedAt()); + return jdbcTemplate.queryForObject(GET_LAST_INSERT_ID_QUERY, Long.class); + } + public Long savePlaylist(Playlist playlist) { + jdbcTemplate.update(SAVE_PLAYLIST_QUERY, playlist.getPlaylistCode(), playlist.getTitle(), playlist.getChannel(), playlist.getThumbnail(), playlist.getDescription(), playlist.getPublishedAt(), playlist.getVideoCount()); + return jdbcTemplate.queryForObject(GET_LAST_INSERT_ID_QUERY, Long.class); + } + public void savePlaylistVideo(Long playlistId, Long videoId, int videoIndex) { + jdbcTemplate.update(SAVE_PLAYLIST_VIDEO_QUERY, playlistId, videoId, videoIndex); + } + public Long saveLecture(Long memberId, Long courseId, Long sourceId, String channel, boolean isPlaylist, int lectureIndex) { + jdbcTemplate.update(SAVE_LECTURE_QUERY, memberId, courseId, sourceId, channel, isPlaylist ? 1 : 0, lectureIndex); + return jdbcTemplate.queryForObject(GET_LAST_INSERT_ID_QUERY, Long.class); + } + public void saveCourseVideo(Long memberId, Long courseId, Long lectureId, Long videoId, int section, int videoIndex, int lectureIndex, Long summaryId) { + jdbcTemplate.update(SAVE_COURSE_VIDEO_QUERY, memberId, courseId, lectureId, videoId, section, videoIndex, lectureIndex, summaryId); + } + public Long getCourseIdByLectureId(Long lectureId) { + return jdbcTemplate.queryForObject(GET_COURSE_ID_BY_LECTURE_ID_QUERY, Long.class, lectureId); + } + public List getCourseBriefListByMember(Long memberId) { + return jdbcTemplate.query(GET_COURSE_LIST_QUERY, + ((rs, rowNum) -> CourseBrief.builder() + .courseTitle(rs.getString("course_title")) + .courseId(rs.getLong("course_id")) + .totalVideoCount(rs.getInt("video_count")) + .build()), memberId + ); + } + public Optional findPlaylist(String lectureCode) { + try { + Playlist playlist = queryForObjectOrNull(FIND_PLAYLIST_QUERY, (rs, rowNum) -> Playlist.builder() + .playlistId(rs.getLong("playlist_id")) + .title(rs.getString("title")) + .channel(rs.getString("channel")) + .description(rs.getString("description")) + .duration(rs.getInt("duration")) + .thumbnail(rs.getString("thumbnail")) + .updatedAt(rs.getTimestamp("updated_at")) + .build(), lectureCode); + return Optional.ofNullable(playlist); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); + } + } + public Optional