File size: 1,470 Bytes
874ae95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class TargetsController < ApplicationController
  include CamelizeKeys
  include StringifyIds
  include DiscordAccountRequirable

  before_action :preview_or_authenticate
  before_action :require_discord_account, only: %i[show]

  # GET /targets/:id/(:slug)
  def show
    @presenter = Targets::ShowPresenter.new(view_context, @target)
    render "courses/curriculum", layout: "student_course"
  end

  # GET /targets/:id/details
  def details_v2
    student =
      current_user
        .students
        .joins(:course)
        .where(courses: { id: @course.id })
        .first if current_user.present?

    render json:
             camelize_keys(
               stringify_ids(
                 Targets::DetailsService.new(
                   @target,
                   student,
                   public_preview: current_user.blank?
                 ).details
               )
             )
  end

  # POST /targets/:id/mark_as_read
  def mark_as_read
    student =
      current_user
        .students
        .joins(:course)
        .where(courses: { id: course.id })
        .first if current_user.present?

    student.page_reads.create_or_find_by!(target: @target)
    head :ok
  end

  private

  def course
    @course ||= Target.find(params[:id]).course
  end

  def preview_or_authenticate
    target = Target.find(params[:id])
    @course = target.course

    authenticate_user! unless @course.public_preview?

    @target = authorize(target)
  end
end