diff --git a/app/assets/stylesheets/theses_crawler.scss b/app/assets/stylesheets/theses_crawler.scss index 25c9a4a..2c28234 100644 --- a/app/assets/stylesheets/theses_crawler.scss +++ b/app/assets/stylesheets/theses_crawler.scss @@ -1,3 +1,49 @@ -// Place all the styles related to the ThesesCrawler controller here. +// Place all the styles related to the thesesCrawler controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: https://sass-lang.com/ + +.theses-list { + list-style: none; + padding: 0; + margin: 0; + + a { + text-decoration: none; + color: black; + } + + .thesis { + line-height: 72px; + width: 100%; + padding: 8px 0 8px 0; + + .list-link { + float: left; + width: 89%; + height: 72px; + } + + .thesis-title { + width: 100%; + display: inline-block; + text-align: left; + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; + } + + .add-to-fav { + display: inline-block; + vertical-align: middle; + float: right; + width: 36px; + height: 36px; + + svg { + width: 100%; + height: auto; + vertical-align: middle; + } + } + } +} diff --git a/app/controllers/crawlers/theses_crawler_controller.rb b/app/controllers/crawlers/theses_crawler_controller.rb index 409a5e4..2bb8231 100644 --- a/app/controllers/crawlers/theses_crawler_controller.rb +++ b/app/controllers/crawlers/theses_crawler_controller.rb @@ -1,13 +1,42 @@ class Crawlers::ThesesCrawlerController < Crawlers::TissCrawlerController def show_basic + params[:api] = '/api/search/thesis/v1.0/quickSearch' + params[:search_parameter] = 'searchterm' + puts params[:search_context] + @host = TissCrawler.get_host + # TissCrawler performs general search over the available courses + @theses = TissCrawler.search(params) end def show_detail + params[:api] = '/api/thesis/' + @id = params[:id] + + puts params + # TissCrawler fetches the person's detail information + @thesis = TissCrawler.get_thesis_details(params) + # Host is needed for image rendering + @host = TissCrawler.get_host end def add_to_fav + params[:api] = '/api/thesis/' + puts params[:id] + @thesis = TissCrawler.get_thesis_details(params) + # create stores the object to the db after creation + favorite_hash = {id: params[:id], + # the user who is currently active + user_id: current_user.id, + title: @thesis['title']['de']} + if FavoriteThesis.create(favorite_hash).valid? + FavoriteThesis.create(favorite_hash) + flash[:alert] = 'Thesis added to your favorites!' + else + flash[:alert] = 'Thesis is already favorited!' + end + redirect_back(fallback_location: search) end end diff --git a/app/controllers/crawlers/tiss_crawler_controller.rb b/app/controllers/crawlers/tiss_crawler_controller.rb index fac3647..d7f2368 100644 --- a/app/controllers/crawlers/tiss_crawler_controller.rb +++ b/app/controllers/crawlers/tiss_crawler_controller.rb @@ -16,8 +16,10 @@ class Crawlers::TissCrawlerController < ApplicationController # redirect to courses_show_basic, propagate the search_term redirect_to :controller => 'crawlers/courses_crawler', :action => :show_basic, :search_term => params[:search_term] when 'Theses' + # redirect to thesis_show_basic, propagate the search_term + redirect_to :controller => 'crawlers/theses_crawler', :action => :show_basic, :search_term => params[:search_term] when 'Projects' - else + else puts 'Undefined search context' end end diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 12ac057..1ea293a 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -24,7 +24,14 @@ class FavoritesController < ApplicationController end def theses + @favorite_theses = FavoriteThesis.where('user_id': current_user.id) + .order(sort_column + " " + sort_direction) + @host = 'https://tiss.tuwien.ac.at' + end + def delete_thesis + FavoriteThesis.find_by(id: params[:id]).destroy + redirect_back(fallback_location: theses) end def projects diff --git a/app/models/favorite_thesis.rb b/app/models/favorite_thesis.rb new file mode 100644 index 0000000..f1618f9 --- /dev/null +++ b/app/models/favorite_thesis.rb @@ -0,0 +1,5 @@ +class FavoriteThesis < ApplicationRecord + self.primary_key = 'id' + + validates :id, uniqueness: true +end diff --git a/app/views/crawlers/theses_crawler/show_basic.html.erb b/app/views/crawlers/theses_crawler/show_basic.html.erb new file mode 100644 index 0000000..02ba658 --- /dev/null +++ b/app/views/crawlers/theses_crawler/show_basic.html.erb @@ -0,0 +1,20 @@ +

Results for "<%= params[:search_term] %>"

+ + diff --git a/app/views/crawlers/theses_crawler/show_detail.html.erb b/app/views/crawlers/theses_crawler/show_detail.html.erb new file mode 100644 index 0000000..6725ab5 --- /dev/null +++ b/app/views/crawlers/theses_crawler/show_detail.html.erb @@ -0,0 +1,5 @@ +<% if @thesis != nil %> +

<%= @thesis['title']['de'] %>

+
<%= @thesis['thesisType'] %>, <%= @thesis['instituteCode'] %> <%= @thesis['instituteName']['de'] %>
+ <%= button_to 'Add to favorites', action: :add_to_fav, id: @id, title: @thesis['title']['de'] %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index ad7b374..946e698 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,6 +22,14 @@ Rails.application.routes.draw do get 'add_to_fav' post 'add_to_fav' end + + namespace :theses_crawler do + get 'show_basic' + get 'show_detail' + post 'show_detail' + get 'add_to_fav' + post 'add_to_fav' + end end namespace :favorites do @@ -30,6 +38,7 @@ Rails.application.routes.draw do get 'courses' delete 'delete_course' get 'theses' + delete 'delete_thesis' get 'projects' end diff --git a/db/migrate/20200525132942_create_favorite_theses.rb b/db/migrate/20200525132942_create_favorite_theses.rb new file mode 100644 index 0000000..64c63db --- /dev/null +++ b/db/migrate/20200525132942_create_favorite_theses.rb @@ -0,0 +1,12 @@ +class CreateFavoriteTheses < ActiveRecord::Migration[6.0] + def change + create_table :favorite_theses, id: false do |t| + t.integer :id, null: false + t.integer :user_id, null: false + t.string :title + + t.timestamps + end + add_index :favorite_theses, :id, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2ea4a22..c1cc81e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_05_21_091231) do +ActiveRecord::Schema.define(version: 2020_05_25_132942) do create_table "favorite_courses", force: :cascade do |t| t.string "number", null: false @@ -33,6 +33,15 @@ ActiveRecord::Schema.define(version: 2020_05_21_091231) do t.index ["tiss_id"], name: "index_favorite_people_on_tiss_id", unique: true end + create_table "favorite_theses", id: false, force: :cascade do |t| + t.integer "id", null: false + t.integer "user_id", null: false + t.string "title" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["id"], name: "index_favorite_theses_on_id", unique: true + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false diff --git a/lib/tiss/tiss_crawler.rb b/lib/tiss/tiss_crawler.rb index c402b54..bc764cb 100644 --- a/lib/tiss/tiss_crawler.rb +++ b/lib/tiss/tiss_crawler.rb @@ -36,6 +36,16 @@ class TissCrawler response.parsed_response['tuvienna']['course'] end + def self.get_thesis_details(params) + api = params[:api] + id = params[:id] + url = $host + api + id + puts(url) + + response = HTTParty.get(url) + response.parsed_response['tuvienna']['thesis'] + end + def self.get_host $host end diff --git a/test/fixtures/favorite_theses.yml b/test/fixtures/favorite_theses.yml new file mode 100644 index 0000000..5181636 --- /dev/null +++ b/test/fixtures/favorite_theses.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/favorite_thesis_test.rb b/test/models/favorite_thesis_test.rb new file mode 100644 index 0000000..6c675a7 --- /dev/null +++ b/test/models/favorite_thesis_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class FavoriteThesisTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end