Commit 15dce333 authored by Zac Wood's avatar Zac Wood

identify sections by CRN instead of id

parent c9161a32
Pipeline #3492 passed with stages
in 26 minutes and 15 seconds
......@@ -33,7 +33,7 @@ class Cart {
}
async toggleSection(section) {
const resp = await fetch(`/sessions/cart?&section_id=${section.id}`, { cache: 'no-store' });
const resp = await fetch(`/sessions/cart?&crn=${section.crn}`, { cache: 'no-store' });
const json = await resp.json();
this.courses = json;
}
......@@ -41,7 +41,7 @@ class Cart {
includesSection(obj) {
for (const key in this._courses) {
const list = this._courses[key];
if (list.includes(obj.id)) return true;
if (list.includes(obj.crn)) return true;
}
return false;
......
......@@ -31,18 +31,18 @@ const remove = async item => {
* and sets the link in the modal to it.
*/
const setUrlInModal = () => {
document.getElementById('calendar-link').innerText = `${window.location.protocol}//${window.location.hostname}/api/schedules?section_ids=${window.cart._courses.join(',')}`;
document.getElementById('calendar-link').innerText = `${window.location.protocol}//${window.location.hostname}/api/schedules?crns=${window.cart._courses.join(',')}`;
};
const downloadIcs = async () => {
const response = await fetch(`${window.location.protocol}//${window.location.hostname}/api/schedules?section_ids=${window.cart._courses.join(',')}`);
const response = await fetch(`${window.location.protocol}//${window.location.hostname}/api/schedules?crns=${window.cart._courses.join(',')}`);
const text = await response.text();
const blob = new Blob([text], { type: 'text/calendar;charset=utf-8' });
saveAs(blob, 'GMU Schedule.ics');
};
const addToSystemCalendar = () => {
window.open(`webcal://${window.location.hostname}/api/schedules?section_ids=${window.cart._courses.join(',')}`);
window.open(`webcal://${window.location.hostname}/api/schedules?crns=${window.cart._courses.join(',')}`);
};
const initListeners = () => {
......@@ -53,5 +53,5 @@ const initListeners = () => {
document.getElementById('download-ics').onclick = downloadIcs;
document.getElementById('add-to-system').onclick = addToSystemCalendar;
document.getElementById('share-url').innerText = `${window.location.protocol}//${window.location.hostname}/schedule/view?section_ids=${window.cart._courses.join(',')}`;
document.getElementById('share-url').innerText = `${window.location.protocol}//${window.location.hostname}/schedule/view?crns=${window.cart._courses.join(',')}`;
};
......@@ -5,10 +5,10 @@ class API::SchedulesController < ApplicationController
# Render an iCal file containing the schedules of all the
# course sections with the given CRNs.
api :GET, '/schedules', 'Generate an iCal file with events for the given CRNs'
param :section_ids, String, desc: 'Comma separated list of section ids to include as events in the calendar', required: true
param :crns, String, desc: 'Comma separated list of crns to include as events in the calendar', required: true
def index
ids = params["section_ids"].split ','
@schedule = Schedule.new ids
crns = params["crns"].split ','
@schedule = Schedule.new crns
render plain: @schedule.to_ical # render a plaintext iCal file
end
end
......@@ -16,13 +16,11 @@ class ApplicationController < ActionController::Base
def set_cart
@cart = JSON.parse(cookies[:cart])
@cart = @cart.reject { |id| CourseSection.find_by_id(id).nil? }
@cart = @cart.reject { |crn| CourseSection.find_by_crn(crn).nil? }
cookies[:cart] = @cart.to_json
end
def set_cookies
cookies[:crns] = "" if cookies[:crns].nil?
cookies[:section_ids] = "" if cookies[:section_ids].nil?
cookies[:cart] = "[]" if cookies[:cart].nil?
end
end
......@@ -3,17 +3,23 @@ class SchedulesController < ApplicationController
include SchedulesHelper
def show
valid_ids = @cart.reject { |id|
s = CourseSection.find_by_id(id)
valid_crns = @cart.reject { |crn|
s = CourseSection.find_by_crn(crn)
s.nil? || s.start_time == "TBA" || s.end_time == "TBA"
}
@all = valid_ids.map { |id| CourseSection.find_by_id id }
@events = generate_fullcalender_events(valid_ids)
@all = valid_crns.map { |crn|
a = CourseSection.where(crn: crn).sort_by { |s| s.course.semester.id }
a.first
}
@events = generate_fullcalender_events(@all)
end
def view
@all = params[:section_ids].split(',').map { |id| CourseSection.find_by_id id.to_s }
@events = generate_fullcalender_events(params[:section_ids].split(','))
@all = params[:crns].split(',').map { |crn|
a = CourseSection.where(crn: crn).sort_by { |s| s.course.semester.id }
a.first
}
@events = generate_fullcalender_events(@all)
end
end
......@@ -8,14 +8,15 @@ class SessionsController < ApplicationController
end
def cart
section_id = params[:section_id]
section_crn = params[:crn]
if @cart.include?(section_id)
@cart.reject! { |id| section_id == id }
if @cart.include?(section_crn.to_s)
@cart.reject! { |crn| section_crn.to_s == crn.to_s }
else
@cart << section_id
@cart << section_crn
end
puts @cart
cookies[:cart] = @cart.to_json
render json: @cart.to_json
end
......@@ -25,8 +26,7 @@ class SessionsController < ApplicationController
crns.each { |crn|
s = CourseSection.find_by_crn(crn)
next if s.nil?
section_id = s.id.to_s
@cart << section_id unless @cart.include?(section_id)
@cart << crn.to_s unless @cart.include?(crn.to_s)
}
cookies[:cart] = @cart.to_json
redirect_to schedule_path
......
......@@ -9,9 +9,8 @@ module SchedulesHelper
"U": Date.new(2019, 1, 20)
}.freeze
def generate_fullcalender_events(section_ids)
section_ids.map do |id|
s = CourseSection.find_by_id id
def generate_fullcalender_events(sections)
sections.map do |s|
s.days.split('').map do |day|
formatted_date = DAYS[day.to_sym].to_s.tr('-', '')
time = Time.parse(s.start_time).strftime("%H%M%S")
......
module SearchHelper
def in_cart?(id)
@cart.include? id.to_s
def in_cart?(crn)
@cart.include? crn.to_s
end
class GenericQueryData
......
......@@ -3,11 +3,11 @@ require 'time'
# Creates a iCal object given a list of section ids
class Schedule
def initialize(ids)
def initialize(crns)
@cal = Icalendar::Calendar.new
@cal.x_wr_calname = 'GMU Schedule'
@course_sections = ids.map { |id| CourseSection.find_by_id id }
@course_sections = crns.map { |crn| CourseSection.find_by_crn crn }
@course_sections.compact!
load_events
......
......@@ -12,7 +12,7 @@
<p><b class="subj"><%= "#{section.name}" %></b>: <%= section.title %></p>
<% if editable %>
<% if in_cart? section.id %>
<% if in_cart? section.crn %>
<span class="float-right text-center add-remove-btn"><i id="icon" class="fas fa-minus"></i><br/><span class="text">Remove</span></span>
<% else %>
<span class="float-right text-center add-remove-btn"><i id="icon" class="fas fa-plus"></i><br/><span class="text">Add</span></span>
......
......@@ -112,7 +112,7 @@ def main
# wipe_db
parser = PatriotWeb::Parser.new
semesters = parser.parse_semesters[0..12] # expand to include however many semesters you want
semesters = parser.parse_semesters[0..6] # expand to include however many semesters you want
courses = nil
semesters.each do |semester|
......
......@@ -2,9 +2,9 @@ require 'test_helper'
class API::SchedulesControllerTest < ActionDispatch::IntegrationTest
test "should generate schedule" do
ids = [course_sections(:cs112001).id, course_sections(:cs112002).id]
crns = [course_sections(:cs112001).crn, course_sections(:cs112002).crn]
get api_schedules_path section_ids: ids.join(','), semester_id: semesters(:fall2018).id
get api_schedules_path crns: crns.join(','), semester_id: semesters(:fall2018).id
# DTSTAMP and UID lines uniquely identify events, so we can't test against them.
# so remove all the lines starting with them.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment