diff --git a/app/controllers/watch_video_histories_controller.rb b/app/controllers/watch_video_histories_controller.rb index 996c76d24..15ee62113 100644 --- a/app/controllers/watch_video_histories_controller.rb +++ b/app/controllers/watch_video_histories_controller.rb @@ -4,5 +4,7 @@ class WatchVideoHistoriesController < ApplicationController def create watch_log = CreateWatchVideoService.new(current_user, request, params).call render_ok(log_id: watch_log&.id) + rescue CreateWatchVideoService::Error => ex + render_error(ex.message) end end diff --git a/app/models/watch_video_history.rb b/app/models/watch_video_history.rb index 0f9ab4a8a..36f7be0f3 100644 --- a/app/models/watch_video_history.rb +++ b/app/models/watch_video_history.rb @@ -2,4 +2,6 @@ class WatchVideoHistory < ApplicationRecord belongs_to :user belongs_to :video belongs_to :watch_course_video, optional: true + + validates :duration, numericality: { greater_than_or_equal_to: 0 } end diff --git a/app/services/create_watch_video_service.rb b/app/services/create_watch_video_service.rb index b16a65535..c7b3bb3bb 100644 --- a/app/services/create_watch_video_service.rb +++ b/app/services/create_watch_video_service.rb @@ -11,20 +11,25 @@ class CreateWatchVideoService < ApplicationService ActiveRecord::Base.transaction do current_time = Time.now if params[:log_id].present? + if params[:total_duration].to_f < params[:watch_duration].to_f || params[:watch_duration].to_f < 0 + raise Error, '观看时长错误' + end # 更新观看时长 watch_video_history = user.watch_video_histories.find(params[:log_id]) - if watch_video_history.present? && watch_video_history.watch_duration < params[:watch_duration].to_f - # 如果观看时长少于原有的,说明拖放到前面重新观看了,不必再去记录 + if watch_video_history.present? && watch_video_history.watch_duration <= params[:watch_duration].to_f && params[:total_duration].to_f > watch_video_history.total_duration + # 如果观看总时长没变,说明视频没有播放,无需再去记录 + watch_video_history.end_at = current_time - watch_video_history.watch_duration = params[:watch_duration] + watch_video_history.total_duration = params[:total_duration] + watch_video_history.watch_duration = params[:watch_duration].to_f > watch_video_history.duration ? watch_video_history.duration : params[:watch_duration] watch_video_history.is_finished = (watch_video_history.duration <= params[:watch_duration].to_f) watch_video_history.save! watch_course_video = watch_video_history.watch_course_video if watch_course_video.present? && !watch_course_video.is_finished && watch_course_video.watch_duration < params[:watch_duration].to_f - # 更新课程视频的时长及是否看完 + # 更新课程视频的时长及是否看完状态 watch_course_video.watch_duration = params[:watch_duration] watch_course_video.is_finished = (watch_course_video.duration <= params[:watch_duration].to_f) watch_course_video.end_at = current_time @@ -35,7 +40,7 @@ class CreateWatchVideoService < ApplicationService # 开始播放时记录一次 if params[:course_id].present? # 课堂视频 - course_video = CourseVideo.find_by(course_id: params[:course_id], video_id: params[:video_id]) + course_video = CourseVideo.find_by(params[:course_video_id]) watch_course_video = WatchCourseVideo.find_or_initialize_by(course_video_id: course_video.id, user_id: user.id) do |d| d.start_at = current_time d.duration = params[:duration] @@ -57,7 +62,7 @@ class CreateWatchVideoService < ApplicationService end - def build_video_log(current_time, video_id, watch_course_video_id) + def build_video_log(current_time, video_id, watch_course_video_id=nil) WatchVideoHistory.new( user_id: user.id, watch_course_video_id: watch_course_video_id, diff --git a/db/migrate/20200312014100_add_total_duration_to_watch_video_histories.rb b/db/migrate/20200312014100_add_total_duration_to_watch_video_histories.rb new file mode 100644 index 000000000..e595384e0 --- /dev/null +++ b/db/migrate/20200312014100_add_total_duration_to_watch_video_histories.rb @@ -0,0 +1,5 @@ +class AddTotalDurationToWatchVideoHistories < ActiveRecord::Migration[5.2] + def change + add_column :watch_video_histories, :total_duration, :float, default: 0 + end +end