Commit 0c7a8560 authored by Zac Wood's avatar Zac Wood

Merge branch '6-search' into 'master'

Resolve "Add search endpoint"

Closes #6

See merge request !5
parents e76685c0 96d56136
function search() {
const searchTable = document.getElementById('scheduleTable');
const crn = document.getElementById('search').value;
fetch(`api/search?crn=${crn}`)
.then(section => section.json())
.then(section => addRow(searchTable, section));
}
function addRow(table, section) {
const tr = document.createElement('tr');
const fields = [
section.name,
section.title,
section.crn,
section.instructor,
section.location,
section.days,
[section.start_time, section.end_time].join(' - '),
];
fields.forEach(field => {
const td = document.createElement('td');
const txt = document.createTextNode(field);
td.appendChild(txt);
tr.appendChild(td);
});
table.appendChild(tr);
}
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
// Place all the styles related to the Search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class SearchController < ApplicationController
def index
if params.key?(:crn)
crn = params[:crn]
@sections = Section.find_by_crn(crn)
render json: @sections
else
render status: 404
end
end
end
<h1>Schedules</h1>
<input type="text" title="search_text" placeholder="Enter CRN..."/>
<button title="search">Search</button>
<input type="text" id="search" title="search_text" placeholder="Enter CRN..."/>
<button title="search" onclick="search()">Search</button>
<!-- <h2>Search results</h2>
<table id="searchTable">
<tr>
<th>Course</th>
<th>Section Name</th>
<th>CRN</th>
<th>Professor</th>
<th>Location</th>
<th>Days</th>
<th>Times</th>
</tr>
</table>
-->
<!-- <br><br> -->
<h2>Your classes</h2>
<table>
<table id="scheduleTable">
<tr>
<th>Course</th>
<th>Section Name</th>
......
<h1>Search#index</h1>
<p>Find me in app/views/search/index.html.erb</p>
......@@ -4,6 +4,8 @@ Rails.application.routes.draw do
resources :courses, only: [:index] do # GET /api/courses
resources :sections, only: [:index] # GET /api/courses/:course_id/sections
end
get 'search', controller: 'search', action: 'index'
end
get 'home/index'
......
require 'test_helper'
class SearchControllerTest < ActionDispatch::IntegrationTest
test "should get index and search by crn" do
get url_for controller: 'search', action: 'index', crn: 'MyString'
assert_response :success
end
test "should 404 without crn" do
get url_for controller: 'search', action: 'index'
assert_response :missing
end
end
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