File size: 1,335 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
module Schools
  class TargetGroupsController < SchoolsController
    # POST /school/levels/:level_id/target_groups(.:format)
    def create
      level = current_school.levels.find(params[:level_id])
      target_group =
        authorize(
          TargetGroup.new(level: level),
          policy_class: Schools::TargetGroupPolicy
        )
      form = ::Schools::TargetGroups::CreateForm.new(target_group)
      if form.validate(params)
        target_group = form.save
        render json: {
                 id: target_group.id.to_s,
                 sortIndex: target_group.sort_index,
                 error: nil
               }
      else
        render json: { error: form.errors.full_messages.join(", ") }
      end
    end

    # PATCH /school/target_groups/:id
    def update
      target_group =
        authorize(
          TargetGroup.find(params[:id]),
          policy_class: Schools::TargetGroupPolicy
        )
      form = ::Schools::TargetGroups::UpdateForm.new(target_group)
      if form.validate(params)
        target_group = form.save
        render json: {
                 id: target_group.id.to_s,
                 sortIndex: target_group.sort_index,
                 error: nil
               }
      else
        render json: { error: form.errors.full_messages.join(", ") }
      end
    end
  end
end