diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 40a6ca1..7740d5e 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -1,7 +1,7 @@ class SchoolsController < ApplicationController - before_action :set_school, only: [:show, :edit, :update, :destroy, :stats] + before_action :set_school, only: [:import_baumspage_roster, :show, :edit, :update, :destroy, :stats] before_action :check_access_director, only: [:new,:create,:destroy] - before_action :check_access_delegate, only: [:update,:edit] + before_action :check_access_delegate, only: [:import_baumspage_roster, :update,:edit] def stats @@ -70,6 +70,16 @@ class SchoolsController < ApplicationController end end + def import_baumspage_roster + import_text = params[:school][:baums_text] + respond_to do |format| + if BaumspageRosterImport.new(@school,import_text).import_roster + format.html { redirect_to "/schools/#{@school.id}", notice: 'Import successful' } + format.json { render action: 'show', status: :created, location: @school } + end + end + end + private # Use callbacks to share common setup or constraints between actions. def set_school @@ -78,7 +88,7 @@ class SchoolsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def school_params - params.require(:school).permit(:name, :score, :tournament_id) + params.require(:school).permit(:name, :score, :tournament_id, :baums_text) end def check_access_director diff --git a/app/models/school.rb b/app/models/school.rb index 2181e74..738da56 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -6,6 +6,8 @@ class School < ActiveRecord::Base validates :name, presence: true + attr_accessor :baums_text + before_destroy do self.tournament.destroy_all_matches end diff --git a/app/services/school_services/baumspage_roster_import.rb b/app/services/school_services/baumspage_roster_import.rb new file mode 100644 index 0000000..e0acccf --- /dev/null +++ b/app/services/school_services/baumspage_roster_import.rb @@ -0,0 +1,65 @@ +class BaumspageRosterImport + def initialize( school, import_text ) + @school = school + @import_text = import_text + end + + def import_roster + @school.wrestlers.each do |wrestler| + wrestler.destroy + end + parse_import_text + end + + def parse_import_text + extra = false + @import_text.each_line do |line| + if line !~ /Extra Wrestlers/ + if extra == false + parse_starter(line) + else + parse_extra(line) + end + else + extra = true + end + end + end + + def parse_starter(line) + extra = false + wrestler_array = line.split(',') + wrestler_losses_array_spot = wrestler_array.size - 1 + wrestler_wins_array_spot = wrestler_array.size - 2 + last_criteria_array_spot = wrestler_wins_array_spot - 1 + wrestler_criteria = "" + (4..last_criteria_array_spot).each do |criteria_line| + wrestler_criteria = wrestler_criteria + ", " + wrestler_array[criteria_line] + end + if wrestler_array[1] + create_wrestler("#{wrestler_array[2]} #{wrestler_array[1]}", "#{wrestler_array[0]}", "#{wrestler_criteria}", "#{wrestler_array[wrestler_wins_array_spot]}", "#{wrestler_array[wrestler_losses_array_spot]}",extra) + end + end + + def parse_extra(line) + extra = true + wrestler_array = line.split(',') + wrestler_losses_array_spot = wrestler_array.size - 1 + wrestler_wins_array_spot = wrestler_array.size - 2 + if wrestler_array[1] + create_wrestler("#{wrestler_array[2]} #{wrestler_array[1]}", "#{wrestler_array[0]}", "", "#{wrestler_array[wrestler_wins_array_spot]}", "#{wrestler_array[wrestler_losses_array_spot]}",extra) + end + end + + def create_wrestler(name,weight,criteria,season_win,season_loss,extra) + wrestler = Wrestler.new + wrestler.name = name + wrestler.school_id = @school.id + wrestler.weight_id = Weight.where("tournament_id = ? and max = ?", @school.tournament.id, weight).first.id + wrestler.criteria = criteria + wrestler.season_win = season_win + wrestler.season_loss = season_loss + wrestler.extra = extra + wrestler.save + end +end \ No newline at end of file diff --git a/app/views/schools/_baums_roster_import.html.erb b/app/views/schools/_baums_roster_import.html.erb new file mode 100644 index 0000000..4af48b8 --- /dev/null +++ b/app/views/schools/_baums_roster_import.html.erb @@ -0,0 +1,11 @@ +<% if can? :manage, @school %> +

+

Import Baumspage Roster

+ <%= form_for(School.new, url: import_baumspage_roster_url(@school)) do |f| %> +
+ <%= f.label 'Baumspage roster text' %>
+ <%= f.text_area :baums_text, cols: "30", rows: "20" %> +
+ <%= submit_tag "Import", :class=>"btn btn-success", data: { confirm: 'Are you sure? This will delete your current roster.' }%> + <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/schools/show.html.erb b/app/views/schools/show.html.erb index 2415bf3..aeebd19 100644 --- a/app/views/schools/show.html.erb +++ b/app/views/schools/show.html.erb @@ -74,3 +74,6 @@ +<% if can? :manage, @school %> + <%= render 'baums_roster_import' %> +<% end %> diff --git a/config/application.rb b/config/application.rb index 5ef7f5e..01dd468 100644 --- a/config/application.rb +++ b/config/application.rb @@ -39,5 +39,7 @@ module Wrestling config.autoload_paths += %W(#{config.root}/app/services/wrestler_services) config.autoload_paths += %W(#{config.root}/app/services/weight_services) config.autoload_paths += %W(#{config.root}/app/services/bracket_advancement) + config.autoload_paths += %W(#{config.root}/app/services/school_services) + end end diff --git a/config/routes.rb b/config/routes.rb index be8b91d..f95cc1c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -56,6 +56,8 @@ Wrestling::Application.routes.draw do post "/wrestlers/update_pool" => "wrestlers#update_pool" get "schools/:id/stats" => "schools#stats" + post "/schools/:id/import_baumspage_roster" => "schools#import_baumspage_roster", :as => :import_baumspage_roster + #API get "/api/tournaments" => "api#tournaments" get "/api/tournaments/user" => "api#currentUserTournaments" diff --git a/test/controllers/schools_controller_test.rb b/test/controllers/schools_controller_test.rb index ce4ca3f..1a5dbb1 100644 --- a/test/controllers/schools_controller_test.rb +++ b/test/controllers/schools_controller_test.rb @@ -53,6 +53,21 @@ class SchoolsControllerTest < ActionController::TestCase assert_redirected_to '/static_pages/not_allowed' end + def baums_import + baums_text = "***** 2019-01-09 13:36:50 ***** +Some School +Some Guy +106,,,,,,,,, +113,Guy,Another,9,,,,,5,7 +120,Guy2,Another,9,,,,,0,0 +126,Guy3,Another,10,,,,5@120,2,2 +******* Extra Wrestlers ******* +120,Guy4,Another,10,0,3 +126,Guy5,Another,9,," + + post :import_baumspage_roster, params: { id: @school.id, school: { baums_text: baums_text } } + end + test "logged in tournament owner should get edit school page" do sign_in_owner get_edit @@ -172,4 +187,22 @@ class SchoolsControllerTest < ActionController::TestCase success end + test "logged in school delegate can import baumspage roster" do + sign_in_school_delegate + baums_import + assert_redirected_to "/schools/#{@school.id}" + end + + test "logged in tournament delegate can import baumspage roster" do + sign_in_tournament_delegate + baums_import + assert_redirected_to "/schools/#{@school.id}" + end + + test "logged in user cannot import baumspage roster" do + sign_in_non_owner + baums_import + redirect + end + end diff --git a/test/integration/baumspage_importer_test.rb b/test/integration/baumspage_importer_test.rb new file mode 100644 index 0000000..4ad2f05 --- /dev/null +++ b/test/integration/baumspage_importer_test.rb @@ -0,0 +1,28 @@ +require 'test_helper' + +class BaumspageImporterTest < ActionDispatch::IntegrationTest + def setup + @school = School.find(1) + @baums_text = "***** 2019-01-09 13:36:50 ***** +Some School +Some Guy +106,,,,,,,,, +113,Guy,Another,9,,,,,5,7 +120,Guy2,Another,9,,,,,0,0 +126,Guy3,Another,10,,,,5@120,2,2 +******* Extra Wrestlers ******* +120,Guy4,Another,10,0,3 +126,Guy5,Another,9,," + end + + test "5 wrestlers get created with Baumspage Importer" do + BaumspageRosterImport.new(@school,@baums_text).import_roster + assert @school.reload.wrestlers.size == 5 + extras = @school.wrestlers.select{|w| w.extra == true} + assert extras.size == 2 + guy = @school.wrestlers.select{|w| w.name == "Another Guy"}.first + assert guy.season_win == 5 + assert guy.season_loss == 7 + end + +end \ No newline at end of file