From fbfc12e09674b6a25eeb6b50ad5d28c62572c8b0 Mon Sep 17 00:00:00 2001 From: Marco Zeisler Date: Tue, 21 Apr 2020 18:06:07 +0200 Subject: [PATCH] store and display favorite person by tiss_id --- app/assets/stylesheets/favorite.scss | 3 ++ .../stylesheets/favorite/favorite_person.scss | 3 ++ .../favorites/favorite_person.scss | 3 ++ .../favorites/courses_controller.rb | 3 -- .../favorites/favorite_person_controller.rb | 5 +++ .../favorites/people_controller.rb | 3 -- .../favorites/projects_controller.rb | 3 -- .../favorites/theses_controller.rb | 3 -- app/controllers/tiss_crawler_controller.rb | 19 +++++--- .../favorite/favorite_person_helper.rb | 2 + app/helpers/favorite_helper.rb | 2 + .../favorites/favorite_person_helper.rb | 2 + app/models/favorite_person.rb | 3 ++ app/services/tiss.rb | 43 ------------------- .../favorites/favorite_person/index.html.erb | 6 +++ app/views/layouts/application.html.erb | 4 +- app/views/tiss_crawler/people_search.html.erb | 3 -- .../tiss_crawler/people_show_basic.html.erb | 7 +-- .../tiss_crawler/people_show_detail.html.erb | 14 ------ .../tiss_crawler/person_show_detail.html.erb | 14 ++++++ config/routes.rb | 11 ++++- .../20200421152314_create_favorite_people.rb | 9 ++++ db/schema.rb | 8 +++- lib/tiss/tiss_crawler.rb | 11 ++++- .../favorite_person_controller_test.rb | 9 ++++ test/controllers/favorite_controller_test.rb | 7 +++ .../favorite_person_controller_test.rb | 9 ++++ test/fixtures/favorite_people.yml | 7 +++ test/fixtures/people.yml | 11 +++++ test/models/favorite_person_test.rb | 7 +++ test/models/person_test.rb | 7 +++ 31 files changed, 153 insertions(+), 88 deletions(-) create mode 100644 app/assets/stylesheets/favorite.scss create mode 100644 app/assets/stylesheets/favorite/favorite_person.scss create mode 100644 app/assets/stylesheets/favorites/favorite_person.scss delete mode 100644 app/controllers/favorites/courses_controller.rb create mode 100644 app/controllers/favorites/favorite_person_controller.rb delete mode 100644 app/controllers/favorites/people_controller.rb delete mode 100644 app/controllers/favorites/projects_controller.rb delete mode 100644 app/controllers/favorites/theses_controller.rb create mode 100644 app/helpers/favorite/favorite_person_helper.rb create mode 100644 app/helpers/favorite_helper.rb create mode 100644 app/helpers/favorites/favorite_person_helper.rb create mode 100644 app/models/favorite_person.rb delete mode 100644 app/services/tiss.rb create mode 100644 app/views/favorites/favorite_person/index.html.erb delete mode 100644 app/views/tiss_crawler/people_show_detail.html.erb create mode 100644 app/views/tiss_crawler/person_show_detail.html.erb create mode 100644 db/migrate/20200421152314_create_favorite_people.rb create mode 100644 test/controllers/favorite/favorite_person_controller_test.rb create mode 100644 test/controllers/favorite_controller_test.rb create mode 100644 test/controllers/favorites/favorite_person_controller_test.rb create mode 100644 test/fixtures/favorite_people.yml create mode 100644 test/fixtures/people.yml create mode 100644 test/models/favorite_person_test.rb create mode 100644 test/models/person_test.rb diff --git a/app/assets/stylesheets/favorite.scss b/app/assets/stylesheets/favorite.scss new file mode 100644 index 0000000..5c42b23 --- /dev/null +++ b/app/assets/stylesheets/favorite.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the favorites controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/favorite/favorite_person.scss b/app/assets/stylesheets/favorite/favorite_person.scss new file mode 100644 index 0000000..459023b --- /dev/null +++ b/app/assets/stylesheets/favorite/favorite_person.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the favorites/favorite_person controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/favorites/favorite_person.scss b/app/assets/stylesheets/favorites/favorite_person.scss new file mode 100644 index 0000000..459023b --- /dev/null +++ b/app/assets/stylesheets/favorites/favorite_person.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the favorites/favorite_person controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/controllers/favorites/courses_controller.rb b/app/controllers/favorites/courses_controller.rb deleted file mode 100644 index bcffa1a..0000000 --- a/app/controllers/favorites/courses_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class CoursesController < ApplicationController - -end diff --git a/app/controllers/favorites/favorite_person_controller.rb b/app/controllers/favorites/favorite_person_controller.rb new file mode 100644 index 0000000..8583836 --- /dev/null +++ b/app/controllers/favorites/favorite_person_controller.rb @@ -0,0 +1,5 @@ +class Favorites::FavoritePersonController < ApplicationController + def index + @favoritePeople = FavoritePerson.all + end +end diff --git a/app/controllers/favorites/people_controller.rb b/app/controllers/favorites/people_controller.rb deleted file mode 100644 index e521299..0000000 --- a/app/controllers/favorites/people_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class PeopleController < ApplicationController - -end diff --git a/app/controllers/favorites/projects_controller.rb b/app/controllers/favorites/projects_controller.rb deleted file mode 100644 index 02fdf3a..0000000 --- a/app/controllers/favorites/projects_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ProjectsController < ApplicationController - -end diff --git a/app/controllers/favorites/theses_controller.rb b/app/controllers/favorites/theses_controller.rb deleted file mode 100644 index 22b0f4d..0000000 --- a/app/controllers/favorites/theses_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ThesesController < ApplicationController - -end diff --git a/app/controllers/tiss_crawler_controller.rb b/app/controllers/tiss_crawler_controller.rb index 95b9f1c..8482bec 100644 --- a/app/controllers/tiss_crawler_controller.rb +++ b/app/controllers/tiss_crawler_controller.rb @@ -9,15 +9,20 @@ class TissCrawlerController < ApplicationController end def people_show_basic - result = TissCrawler.search(params) - $people = result.map { |person| Tiss::Person.new(person) } - @people = $people + params[:api] = '/api/person/v22/psuche' + params[:search_parameter] = 'q' + @people = TissCrawler.search(params) end - def people_show_detail - people = $people - index = params[:index] - @person = people[Integer(index)] + def person_show_detail + params[:api] = '/api/person/v22/id/' + @person = TissCrawler.get_details(params) + @host = TissCrawler.get_host + end + + def person_add_to_fav + puts params[:tiss_id] + person = FavoritePerson.create(tiss_id: params[:tiss_id]) end end diff --git a/app/helpers/favorite/favorite_person_helper.rb b/app/helpers/favorite/favorite_person_helper.rb new file mode 100644 index 0000000..2385884 --- /dev/null +++ b/app/helpers/favorite/favorite_person_helper.rb @@ -0,0 +1,2 @@ +module Favorite::FavoritePersonHelper +end diff --git a/app/helpers/favorite_helper.rb b/app/helpers/favorite_helper.rb new file mode 100644 index 0000000..20d6a45 --- /dev/null +++ b/app/helpers/favorite_helper.rb @@ -0,0 +1,2 @@ +module FavoriteHelper +end diff --git a/app/helpers/favorites/favorite_person_helper.rb b/app/helpers/favorites/favorite_person_helper.rb new file mode 100644 index 0000000..36e581b --- /dev/null +++ b/app/helpers/favorites/favorite_person_helper.rb @@ -0,0 +1,2 @@ +module Favorites::FavoritePersonHelper +end diff --git a/app/models/favorite_person.rb b/app/models/favorite_person.rb new file mode 100644 index 0000000..bee068e --- /dev/null +++ b/app/models/favorite_person.rb @@ -0,0 +1,3 @@ +class FavoritePerson < ApplicationRecord + self.primary_key = "tiss_id" +end diff --git a/app/services/tiss.rb b/app/services/tiss.rb deleted file mode 100644 index 03251a4..0000000 --- a/app/services/tiss.rb +++ /dev/null @@ -1,43 +0,0 @@ -module Tiss - class Base - - def base_uri - 'https://tiss.tuwien.ac.at/' - end - - def initialize(args = {}) - args.each do |name, value| - attr_name = name.to_s.underscore - send("#{attr_name}=", value) if respond_to?("#{attr_name}=") - end - end - - end - - class Person < Base - - # tiss json properties - attr_accessor :tiss_id, - :oid, - :old_tiss_ids, - :first_name, - :last_name, - :gender, - :pseudoperson, - :preceding_titles, - :postpositioned_titles, - :orcid, - :card_uri, - :picture_uri, - :main_phone_number, - :main_email, - :other_emails, - :main_addresses, - :employee - - def initialize(args = {}) - super(args) - end - - end -end \ No newline at end of file diff --git a/app/views/favorites/favorite_person/index.html.erb b/app/views/favorites/favorite_person/index.html.erb new file mode 100644 index 0000000..604245e --- /dev/null +++ b/app/views/favorites/favorite_person/index.html.erb @@ -0,0 +1,6 @@ +

Your favorite perople listed here ...

+

For now only the tiss_ids are stored and shown

+ +<% @favoritePeople.each do |favPerson| %> + Tiss_id: <%= favPerson.tiss_id %> +<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1fef818..5cfbd1c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -21,8 +21,8 @@
<%= link_to 'Home', root_path %> - <%= link_to 'People', tiss_crawler_people_search_path %> - <%#= link_to 'Courses', courses_index_path %> + <%= link_to 'Search', tiss_crawler_people_search_path %> + <%= link_to 'Favorite People', '/favorites/favorite_person/index' %> <%#= link_to 'Projects', projects_index_path %> <%#= link_to 'Theses', theses_index_path %>
diff --git a/app/views/tiss_crawler/people_search.html.erb b/app/views/tiss_crawler/people_search.html.erb index 17adb44..0a98dc3 100644 --- a/app/views/tiss_crawler/people_search.html.erb +++ b/app/views/tiss_crawler/people_search.html.erb @@ -1,7 +1,4 @@ <%= form_tag('/tiss_crawler/people_show_basic', :method => "get") do %> - <%= hidden_field_tag(:context, 'People') %> - <%= hidden_field_tag(:api, '/api/person/v22/psuche') %> - <%= hidden_field_tag(:search_parameter, 'q') %> <%= label_tag(:search_term, "Search for people @TU Vienna:") %> <%= text_field_tag(:search_term) %> <%= submit_tag("Search") %> diff --git a/app/views/tiss_crawler/people_show_basic.html.erb b/app/views/tiss_crawler/people_show_basic.html.erb index 2dad564..4420e47 100644 --- a/app/views/tiss_crawler/people_show_basic.html.erb +++ b/app/views/tiss_crawler/people_show_basic.html.erb @@ -2,8 +2,9 @@ This is the result for your basic people search: <% @people.each_with_index do |person, index| %>

- <%= person.first_name %> - <%= person.last_name %> - <%= button_to 'Details', action: :people_show_detail, index: index %> + <%= person['first_name'] %> + <%= person['last_name'] %> + <%= button_to 'Details', action: :person_show_detail, tiss_id: person['tiss_id'] %> + <%= button_to 'Add to favs.', action: :person_add_to_fav, tiss_id: person['tiss_id'] %>

<% end %> diff --git a/app/views/tiss_crawler/people_show_detail.html.erb b/app/views/tiss_crawler/people_show_detail.html.erb deleted file mode 100644 index 18f2a0d..0000000 --- a/app/views/tiss_crawler/people_show_detail.html.erb +++ /dev/null @@ -1,14 +0,0 @@ -Chosen person in detail view: - -<% if @person != nil %> - <% if @person.picture_uri != nil %> - <%= image_tag @person.base_uri + @person.picture_uri %> - <% end %> -

<%= @person.preceding_titles %> <%= @person.first_name %> <%= @person.last_name %>, - <%= @person.gender %> <%= @person.postpositioned_titles %>

-

Phone: <%= @person.main_phone_number %>

-

Mail: <%= @person.main_email %>

-

Other mails: <%= @person.other_emails %>

-

Main addresses: <%= @person.main_addresses %>

- -<% end %> \ No newline at end of file diff --git a/app/views/tiss_crawler/person_show_detail.html.erb b/app/views/tiss_crawler/person_show_detail.html.erb new file mode 100644 index 0000000..b929ed8 --- /dev/null +++ b/app/views/tiss_crawler/person_show_detail.html.erb @@ -0,0 +1,14 @@ +Chosen person in detail view: + +<% if @person != nil %> + <% if @person['picture_uri'] != nil %> + <%= image_tag @host + @person['picture_uri'] %> + <% end %> +

<%= @person['preceding_titles'] %> <%= @person['first_name'] %> <%= @person['last_name'] %>, + <%= @person['gender'] %> <%= @person['postpositioned_titles'] %>

+

Phone: <%= @person['main_phone_number'] %>

+

Mail: <%= @person['main_email'] %>

+

Other mails: <%= @person['other_emails'] %>

+

Main addresses: <%= @person['main_addresses'] %>

+ +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 893d67d..812aed1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,12 +1,19 @@ Rails.application.routes.draw do + devise_for :users, path_names: {sign_in: 'log_in', sign_out: 'log out'} get 'login/index' get 'tiss_crawler/people_search' get 'tiss_crawler/people_show_basic' - get 'tiss_crawler/people_show_detail' - post 'tiss_crawler/people_show_detail' + get 'tiss_crawler/person_show_detail' + post 'tiss_crawler/person_show_detail' + get 'tiss_crawler/person_add_to_fav' + post 'tiss_crawler/person_add_to_fav' + + namespace :favorites do + get 'favorite_person/index' + end # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root 'login#index' diff --git a/db/migrate/20200421152314_create_favorite_people.rb b/db/migrate/20200421152314_create_favorite_people.rb new file mode 100644 index 0000000..a676b38 --- /dev/null +++ b/db/migrate/20200421152314_create_favorite_people.rb @@ -0,0 +1,9 @@ +class CreateFavoritePeople < ActiveRecord::Migration[6.0] + def change + create_table :favorite_people do |t| + t.string :tiss_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 94b49bc..60420b9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_04_14_101256) do +ActiveRecord::Schema.define(version: 2020_04_21_152314) do + + create_table "favorite_people", force: :cascade do |t| + t.string "tiss_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false diff --git a/lib/tiss/tiss_crawler.rb b/lib/tiss/tiss_crawler.rb index f43baa0..dfabff7 100644 --- a/lib/tiss/tiss_crawler.rb +++ b/lib/tiss/tiss_crawler.rb @@ -15,9 +15,18 @@ class TissCrawler JSON.parse(response.body)["results"] end + def self.get_details(params) + api = params[:api] + id = params[:tiss_id] + url = $host + api + id + puts(url) - def details + response = HTTParty.get(url) + JSON.parse(response.body) + end + def self.get_host + $host end end \ No newline at end of file diff --git a/test/controllers/favorite/favorite_person_controller_test.rb b/test/controllers/favorite/favorite_person_controller_test.rb new file mode 100644 index 0000000..b18cd15 --- /dev/null +++ b/test/controllers/favorite/favorite_person_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class Favorite::FavoritePersonControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get favorite_favorite_person_index_url + assert_response :success + end + +end diff --git a/test/controllers/favorite_controller_test.rb b/test/controllers/favorite_controller_test.rb new file mode 100644 index 0000000..9f3c115 --- /dev/null +++ b/test/controllers/favorite_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class FavoriteControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/favorites/favorite_person_controller_test.rb b/test/controllers/favorites/favorite_person_controller_test.rb new file mode 100644 index 0000000..6d74511 --- /dev/null +++ b/test/controllers/favorites/favorite_person_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class Favorites::FavoritePersonControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get favorites_favorite_person_index_url + assert_response :success + end + +end diff --git a/test/fixtures/favorite_people.yml b/test/fixtures/favorite_people.yml new file mode 100644 index 0000000..ec463bf --- /dev/null +++ b/test/fixtures/favorite_people.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + tiss_id: MyString + +two: + tiss_id: MyString diff --git a/test/fixtures/people.yml b/test/fixtures/people.yml new file mode 100644 index 0000000..5181636 --- /dev/null +++ b/test/fixtures/people.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_person_test.rb b/test/models/favorite_person_test.rb new file mode 100644 index 0000000..c580917 --- /dev/null +++ b/test/models/favorite_person_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class FavoritePersonTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/person_test.rb b/test/models/person_test.rb new file mode 100644 index 0000000..ad04ed8 --- /dev/null +++ b/test/models/person_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class PersonTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end