-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate home page and CSV parsing logic (#32)
* Migrate homepage to schedule index * Fixed RSpec failures * Migrate CSV logic to separate service * Update Cucumber scenarios * Change class name to satisfy autoloader * Remove unused require
- Loading branch information
1 parent
c0c0c98
commit 0e047d2
Showing
19 changed files
with
225 additions
and
252 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# frozen_string_literal: true | ||
|
||
class CsvHandler | ||
require 'csv' | ||
|
||
def upload(file) | ||
@file = file | ||
end | ||
|
||
def parse_room_csv(schedule_id) | ||
begin | ||
ActiveRecord::Base.transaction do | ||
room_data = CSV.parse(@file.read, headers: true) | ||
room_data.each do |row| | ||
Room.create!( | ||
schedule_id: schedule_id, | ||
campus: row['campus'], | ||
building_code: row['building_code'], | ||
room_number: row['room_number'], | ||
capacity: row['capacity'].to_i, | ||
is_lecture_hall: row['is_lecture_hall'] == 'True', | ||
is_learning_studio: row['is_learning_studio'] == 'True', | ||
is_lab: row['is_lab'] == 'True', | ||
is_active: row['is_active'] == 'True', | ||
comments: row['comments'] | ||
) | ||
end | ||
end | ||
# Flash data to be received by controller | ||
return { notice: 'Rooms successfully uploaded.' } | ||
rescue StandardError => e | ||
return { alert: "There was an error uploading the CSV file: #{e.message}" } | ||
end | ||
end | ||
|
||
def parse_instructor_csv(schedule_id) | ||
begin | ||
ActiveRecord::Base.transaction do | ||
instructor_data = CSV.parse(@file.read) | ||
actual_headers = instructor_data[1] | ||
required_headers = [ | ||
'anonimized ID', | ||
'First Name', | ||
'Last Name', | ||
'Email', | ||
'Teaching before 9:00 am.', | ||
'Teaching after 3:00 pm.', | ||
'Middle Name', | ||
'Is there anything else we should be aware of regarding your teaching load (special course reduction, ...)' # Optional: Include if needed | ||
] | ||
|
||
missing_headers = required_headers - actual_headers | ||
unless missing_headers.empty? | ||
return {alert: "Missing required headers: #{missing_headers.join(', ')}"} | ||
end | ||
|
||
instructor_data[2..].each do |row| | ||
# Extracting values and checking for nulls | ||
id_number = row[actual_headers.index('anonimized ID')] | ||
first_name = row[actual_headers.index('First Name')] | ||
last_name = row[actual_headers.index('Last Name')] | ||
middle_name = row[actual_headers.index('Middle Name')] | ||
email = row[actual_headers.index('Email')] | ||
before_9 = row[actual_headers.index('Teaching before 9:00 am.')] | ||
after_3 = row[actual_headers.index('Teaching after 3:00 pm.')] | ||
beaware_of = row[actual_headers.index('Is there anything else we should be aware of regarding your teaching load (special course reduction, ...)')] | ||
|
||
instructor_data = { | ||
schedule_id: schedule_id, | ||
id_number: id_number, | ||
first_name: first_name, | ||
last_name: last_name, | ||
middle_name: middle_name, | ||
email: email, | ||
before_9: before_9, | ||
after_3: after_3, | ||
beaware_of: beaware_of | ||
} | ||
|
||
Instructor.create(instructor_data) | ||
end | ||
end | ||
return {notice: 'Instructors successfully uploaded.'} | ||
rescue StandardError => e | ||
return {alert: "There was an error uploading the CSV file: #{e.message}"} | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.