initial commit
This commit is contained in:
13
app/controllers/application_controller.rb
Normal file
13
app/controllers/application_controller.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ApplicationController < ActionController::Base
|
||||
# Database: You can store session data in your application's database. This
|
||||
# requires additional setup, but it can be useful if you need to store large
|
||||
# amounts of data or want to share session data between multiple web servers.
|
||||
#
|
||||
# To configure the session storage option, you can edit your Rails application's
|
||||
# config/application.rb file.
|
||||
# For example, to use the database for session
|
||||
# storage, you can add the following line:
|
||||
config.session_store :active_record_store
|
||||
end
|
||||
0
app/controllers/concerns/.keep
Normal file
0
app/controllers/concerns/.keep
Normal file
138
app/controllers/event_controller.rb
Normal file
138
app/controllers/event_controller.rb
Normal file
@@ -0,0 +1,138 @@
|
||||
# frozen_string_literal: true
|
||||
class EventController < ApplicationController
|
||||
include SessionsHelper
|
||||
before_action :logged_in_user
|
||||
skip_before_action :verify_authenticity_token, only: :lineup_email
|
||||
|
||||
def show
|
||||
team_id = params[:team_id]
|
||||
event_id = params[:event_id]
|
||||
team = TeamSnap::Team.find(@teamsnap_client, team_id)
|
||||
event = TeamSnap::Event.find(@teamsnap_client, event_id)
|
||||
availability_summary = TeamSnap::AvailabilitySummary.find(@teamsnap_client, event_id)
|
||||
render 'show', locals: { team:, event:, availability_summary: }
|
||||
end
|
||||
|
||||
def list
|
||||
team_id = params[:team_id]
|
||||
team = TeamSnap::Team.find(@teamsnap_client, team_id)
|
||||
events = TeamSnap::Event.search(@teamsnap_client, { team_id: })
|
||||
availability_summaries = TeamSnap::AvailabilitySummary.search(@teamsnap_client, { team_id: })
|
||||
availability_summaries_by_event = {}
|
||||
events.each_with_index do |event, _index|
|
||||
availability_summary = availability_summaries.find { |a| a.event_id == event.id }
|
||||
availability_summaries_by_event[event] = availability_summary
|
||||
end
|
||||
render 'list',
|
||||
locals: { team:, events:, availability_summaries_by_event: }
|
||||
end
|
||||
|
||||
def lineup
|
||||
team_id = params[:team_id].to_i
|
||||
event_id = params[:event_id].to_i
|
||||
context = helpers.event_context_with_timeline(team_id, event_id, 4)
|
||||
render 'lineup', locals: context
|
||||
end
|
||||
|
||||
def lineup_params(params)
|
||||
params.permit(
|
||||
:event_lineup_id,
|
||||
:event_lineup_entry_original,
|
||||
event_lineup_entries: [
|
||||
:label,
|
||||
:sequence,
|
||||
:member_id,
|
||||
:event_id,
|
||||
:id,
|
||||
:availability_status_code,
|
||||
member: [
|
||||
:last_name,
|
||||
:first_name,
|
||||
:jersey_number,
|
||||
:email_addresses
|
||||
]
|
||||
|
||||
]
|
||||
|
||||
)
|
||||
end
|
||||
|
||||
def lineup_email
|
||||
params_hash = lineup_params(params).to_h
|
||||
p params_hash
|
||||
render "lineup_email", locals:params_hash, layout: false
|
||||
end
|
||||
|
||||
def lineup_submit
|
||||
params_hash = lineup_params(params).to_h
|
||||
unless params_hash[:event_lineup_id].blank?
|
||||
event_lineup = TeamSnap::EventLineup.find(@teamsnap_client, params_hash[:event_lineup_id])
|
||||
event_lineup_id = event_lineup.id
|
||||
event_lineup_entries_existing = event_lineup.event_lineup_entries
|
||||
else
|
||||
event_lineup = TeamSnap::EventLineup.create(@teamsnap_client, {
|
||||
:team_id => params[:team_id].to_i,
|
||||
:event_id => params[:event_id].to_i
|
||||
})
|
||||
event_lineup_id = event_lineup.id
|
||||
event_lineup_entries_existing = []
|
||||
end
|
||||
|
||||
params_hash[:event_lineup_entries].each do |lue|
|
||||
unless lue[:label] == ""
|
||||
if existing_entry = event_lineup_entries_existing.find{|entry| entry.id.to_s == lue[:id]}
|
||||
# Exists, update
|
||||
# Check if exactly the same and update if not
|
||||
unless (
|
||||
existing_entry["member_id"].to_s ==lue[:member_id] and
|
||||
existing_entry["label"] ==lue[:label] and
|
||||
existing_entry["sequence"].to_s ==lue[:sequence]
|
||||
)
|
||||
TeamSnap::EventLineupEntry.update(@teamsnap_client, lue[:id],
|
||||
{:member_id => lue[:member_id],
|
||||
:label => lue[:label],
|
||||
:sequence => lue[:sequence]
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
# remove from list
|
||||
event_lineup_entries_existing.delete_at(event_lineup_entries_existing.index(existing_entry))
|
||||
else
|
||||
lue[:sequence]
|
||||
TeamSnap::EventLineupEntry.create(@teamsnap_client,
|
||||
{:member_id => lue[:member_id],
|
||||
:label => lue[:label],
|
||||
:sequence => lue[:sequence],
|
||||
:event_lineup_id => event_lineup_id
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
event_lineup_entries_existing.each do |lue_existing|
|
||||
response = TeamSnap::Api.run(@teamsnap_client, :delete, :event_lineup_entry, lue_existing["id"])
|
||||
unless response.status == 204
|
||||
raise "Unsuccessful"
|
||||
end
|
||||
end
|
||||
|
||||
render :plain => "Successfully Done!"
|
||||
end
|
||||
|
||||
def lineup_card
|
||||
team_id = params[:team_id].to_i
|
||||
event_id = params[:event_id].to_i
|
||||
context = helpers.event_context_with_timeline(team_id, event_id, 4)
|
||||
render 'lineup_card', locals: context, layout: false
|
||||
end
|
||||
|
||||
def lineup_card_template
|
||||
params[:team_id].to_i
|
||||
params[:event_id].to_i
|
||||
# context = helpers.event_data_with_additional_contexts(team_id, event_id)
|
||||
context = {}
|
||||
render 'lineup_card_template', locals: { context: }, layout: false
|
||||
end
|
||||
end
|
||||
18
app/controllers/main_controller.rb
Normal file
18
app/controllers/main_controller.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'teamsnap'
|
||||
class MainController < ApplicationController
|
||||
include SessionsHelper
|
||||
before_action :logged_in_user
|
||||
def index
|
||||
TeamSnap.init(token: @credentials['token'],
|
||||
headers: { 'Authorization' => "Bearer #{@credentials['token']}" })
|
||||
@teamsnap_client = TeamSnap.root_client
|
||||
if session[:team_id]
|
||||
redirect_to team_path(team_id: session[:team_id])
|
||||
else
|
||||
teams = TeamSnap::Team.search(@teamsnap_client, { user_id: @user_id })
|
||||
redirect_to team_list_url
|
||||
end
|
||||
end
|
||||
end
|
||||
55
app/controllers/opponent_controller.rb
Normal file
55
app/controllers/opponent_controller.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class OpponentController < ApplicationController
|
||||
include SessionsHelper
|
||||
before_action :logged_in_user
|
||||
protect_from_forgery with: :exception
|
||||
|
||||
def show
|
||||
team_id = params[:team_id]
|
||||
opponent_id = params[:opponent_id]
|
||||
team = TeamSnap::Team.find(@teamsnap_client, team_id)
|
||||
opponent = TeamSnap::Opponent.find(@teamsnap_client, opponent_id)
|
||||
team_media = TeamSnap::TeamMedium.search(@teamsnap_client, {team_id: team_id})
|
||||
team_media_groups = TeamSnap::TeamMedium.search(@teamsnap_client, {team_id: team_id})
|
||||
opponent_logo = team_media.find{|tm| tm.description == "opponent-logo-#{opponent_id}.png"}
|
||||
render 'show', locals: { team: team, opponent: opponent, opponent_logo: opponent_logo }
|
||||
end
|
||||
|
||||
def list
|
||||
team_id = params[:team_id]
|
||||
team = TeamSnap::Team.find(@teamsnap_client, team_id)
|
||||
opponents = TeamSnap::Opponent.search(@teamsnap_client, { team_id: })
|
||||
render 'list', locals: { team:, opponents: }
|
||||
end
|
||||
|
||||
def upload_logo_get
|
||||
team_id = params[:team_id]
|
||||
opponent_id = params[:opponent_id]
|
||||
member_id = @user_id
|
||||
render 'upload_logo', locals: { team_id:, opponent_id:, member_id: }
|
||||
end
|
||||
|
||||
|
||||
def upload_logo_post
|
||||
team_id = params[:team_id]
|
||||
opponent_id = params[:opponent_id]
|
||||
member_results = TeamSnap::Member.search(@teamsnap_client, {user_id: @user_id, team_id: team_id})
|
||||
member_id = member_results[0].id
|
||||
file = params['file']
|
||||
|
||||
|
||||
new_team_medium = TeamSnap::TeamMedium.upload_team_medium(@teamsnap_client,
|
||||
{:media_format=> "file",
|
||||
:file=> file.open(),
|
||||
:member_id=> member_id,
|
||||
:team_id=> team_id,
|
||||
:team_media_group_id=> "4927028",
|
||||
:description=> "team-logo-#{"test"}.png",position:0}
|
||||
)
|
||||
|
||||
render plain: "Posted."
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
37
app/controllers/sessions_controller.rb
Normal file
37
app/controllers/sessions_controller.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Returns true if the user is logged in, false otherwise.
|
||||
def logged_in?
|
||||
p 'session is ', session
|
||||
!session[:user].nil?
|
||||
end
|
||||
|
||||
# Confirms a logged-in user.
|
||||
def logged_in_user
|
||||
p 'checking login status'
|
||||
unless logged_in?
|
||||
p 'Please log in'
|
||||
flash[:danger] = 'Please log in.'
|
||||
redirect_to login_url
|
||||
end
|
||||
@user = session[:user]
|
||||
@user_id = session[:user_id]
|
||||
@credentials = session[:credentials]
|
||||
TeamSnap.init(token: @credentials['token'],
|
||||
headers: { 'Authorization' => "Bearer #{@credentials['token']}" })
|
||||
@teamsnap_client = TeamSnap.root_client
|
||||
end
|
||||
|
||||
class SessionsController < ApplicationController
|
||||
def new
|
||||
render :new
|
||||
end
|
||||
|
||||
def create
|
||||
user_info = request.env['omniauth.auth']
|
||||
session[:user] = user_info.info
|
||||
session[:user_id] = user_info.uid
|
||||
session[:credentials] = user_info.credentials
|
||||
redirect_to root_path, notice: 'Succesful Login'
|
||||
end
|
||||
end
|
||||
32
app/controllers/team_controller.rb
Normal file
32
app/controllers/team_controller.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'teamsnap'
|
||||
require 'net/http'
|
||||
require 'uri'
|
||||
require 'date'
|
||||
|
||||
class TeamController < ApplicationController
|
||||
include SessionsHelper
|
||||
before_action :logged_in_user
|
||||
|
||||
def show
|
||||
team_id = params[:team_id]
|
||||
session[:team_id] = team_id
|
||||
team = TeamSnap::Team.find(@teamsnap_client, team_id)
|
||||
upcoming_events = TeamSnap::Event.search(@teamsnap_client, {
|
||||
team_id: team_id, started_after: Date.today.strftime("%Y-%m-%d"), page_size: 4
|
||||
})
|
||||
past_events = TeamSnap::Event.search(@teamsnap_client, {
|
||||
team_id: team_id, started_before: Date.today.strftime("%Y-%m-%d"), page_size: 4, sort_start_date: "desc"
|
||||
})
|
||||
|
||||
items = TeamSnap::AvailabilitySummary.search(@teamsnap_client, {:event_id => upcoming_events.map{|e|e.id}.join(",")})
|
||||
|
||||
render 'show', locals: { team: team, upcoming_events: upcoming_events, past_events: past_events, availability_summaries: items }
|
||||
end
|
||||
|
||||
def list
|
||||
teams = TeamSnap::Team.search(@teamsnap_client, { user_id: @user_id })
|
||||
render 'list', locals: { teams: teams }
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user