sessions_controller.rb 1.27 KB
Newer Older
Zac Wood's avatar
Zac Wood committed
1
2
class SessionsController < ApplicationController
  def update
3
    update_cookie :crns
Zac Wood's avatar
Zac Wood committed
4
    update_cookie :section_ids
5
    update_cookie :semester_id
Zac Wood's avatar
Zac Wood committed
6
7
8
9

    head :ok
  end

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
  def cart
    cart = if cookies[:cart].nil?
             {}
           else
             JSON.parse cookies[:cart]
           end

    course_id, section_ids, pair_ids = params[:course_id], params[:section_ids], params[:pair_ids]
    cart[course_id] ||= []

    unless section_ids.nil?
      ids = section_ids.split(',')
      ids.each do |section_id|
        if cart[course_id].include?(section_id)
          cart[course_id] = cart[course_id].reject do |a|
            a == section_id
          end
        else
          cart[course_id].push(section_id)
        end
      end
    end

    unless pair_ids.nil?
      pair = pair_ids.split(',')
      if cart[course_id].include?(pair)
        cart[course_id] = cart[course_id].reject do |a|
          a == pair
        end
      else
        cart[course_id].push(pair)
      end
    end

    to_delete = cart.keys.select do |cid|
      cart[cid].empty?
    end

    to_delete.each { |key| cart.delete(key) }

    cookies[:cart] = cart.to_json
    render json: cart.to_json
  end

Zac Wood's avatar
Zac Wood committed
54
55
56
57
58
59
  private

  def update_cookie(sym)
    cookies[sym] = params[sym] unless params[sym].nil?
  end
end