|
| 1 | +class Api::V1::Scouting::PlayersController < Api::V1::BaseController |
| 2 | + before_action :set_scouting_target, only: [:show, :update, :destroy, :sync] |
| 3 | + |
| 4 | + def index |
| 5 | + targets = organization_scoped(ScoutingTarget).includes(:added_by, :assigned_to) |
| 6 | + |
| 7 | + # Apply filters |
| 8 | + targets = targets.by_role(params[:role]) if params[:role].present? |
| 9 | + targets = targets.by_status(params[:status]) if params[:status].present? |
| 10 | + targets = targets.by_priority(params[:priority]) if params[:priority].present? |
| 11 | + targets = targets.by_region(params[:region]) if params[:region].present? |
| 12 | + |
| 13 | + # Special filters |
| 14 | + targets = targets.active if params[:active] == 'true' |
| 15 | + targets = targets.high_priority if params[:high_priority] == 'true' |
| 16 | + targets = targets.needs_review if params[:needs_review] == 'true' |
| 17 | + targets = targets.assigned_to_user(params[:assigned_to_id]) if params[:assigned_to_id].present? |
| 18 | + |
| 19 | + # Search |
| 20 | + if params[:search].present? |
| 21 | + search_term = "%#{params[:search]}%" |
| 22 | + targets = targets.where('summoner_name ILIKE ? OR real_name ILIKE ?', search_term, search_term) |
| 23 | + end |
| 24 | + |
| 25 | + # Sorting |
| 26 | + sort_by = params[:sort_by] || 'created_at' |
| 27 | + sort_order = params[:sort_order] || 'desc' |
| 28 | + targets = targets.order("#{sort_by} #{sort_order}") |
| 29 | + |
| 30 | + # Pagination |
| 31 | + result = paginate(targets) |
| 32 | + |
| 33 | + render_success({ |
| 34 | + scouting_targets: ScoutingTargetSerializer.render_as_hash(result[:data]), |
| 35 | + pagination: result[:pagination] |
| 36 | + }) |
| 37 | + end |
| 38 | + |
| 39 | + def show |
| 40 | + render_success({ |
| 41 | + scouting_target: ScoutingTargetSerializer.render_as_hash(@target) |
| 42 | + }) |
| 43 | + end |
| 44 | + |
| 45 | + def create |
| 46 | + target = organization_scoped(ScoutingTarget).new(scouting_target_params) |
| 47 | + target.organization = current_organization |
| 48 | + target.added_by = current_user |
| 49 | + |
| 50 | + if target.save |
| 51 | + log_user_action( |
| 52 | + action: 'create', |
| 53 | + entity_type: 'ScoutingTarget', |
| 54 | + entity_id: target.id, |
| 55 | + new_values: target.attributes |
| 56 | + ) |
| 57 | + |
| 58 | + render_created({ |
| 59 | + scouting_target: ScoutingTargetSerializer.render_as_hash(target) |
| 60 | + }, message: 'Scouting target added successfully') |
| 61 | + else |
| 62 | + render_error( |
| 63 | + message: 'Failed to add scouting target', |
| 64 | + code: 'VALIDATION_ERROR', |
| 65 | + status: :unprocessable_entity, |
| 66 | + details: target.errors.as_json |
| 67 | + ) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def update |
| 72 | + old_values = @target.attributes.dup |
| 73 | + |
| 74 | + if @target.update(scouting_target_params) |
| 75 | + log_user_action( |
| 76 | + action: 'update', |
| 77 | + entity_type: 'ScoutingTarget', |
| 78 | + entity_id: @target.id, |
| 79 | + old_values: old_values, |
| 80 | + new_values: @target.attributes |
| 81 | + ) |
| 82 | + |
| 83 | + render_updated({ |
| 84 | + scouting_target: ScoutingTargetSerializer.render_as_hash(@target) |
| 85 | + }) |
| 86 | + else |
| 87 | + render_error( |
| 88 | + message: 'Failed to update scouting target', |
| 89 | + code: 'VALIDATION_ERROR', |
| 90 | + status: :unprocessable_entity, |
| 91 | + details: @target.errors.as_json |
| 92 | + ) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + def destroy |
| 97 | + if @target.destroy |
| 98 | + log_user_action( |
| 99 | + action: 'delete', |
| 100 | + entity_type: 'ScoutingTarget', |
| 101 | + entity_id: @target.id, |
| 102 | + old_values: @target.attributes |
| 103 | + ) |
| 104 | + |
| 105 | + render_deleted(message: 'Scouting target removed successfully') |
| 106 | + else |
| 107 | + render_error( |
| 108 | + message: 'Failed to remove scouting target', |
| 109 | + code: 'DELETE_ERROR', |
| 110 | + status: :unprocessable_entity |
| 111 | + ) |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + def sync |
| 116 | + # This will sync the scouting target with Riot API |
| 117 | + # Will be implemented when Riot API service is ready |
| 118 | + render_error( |
| 119 | + message: 'Sync functionality not yet implemented', |
| 120 | + code: 'NOT_IMPLEMENTED', |
| 121 | + status: :not_implemented |
| 122 | + ) |
| 123 | + end |
| 124 | + |
| 125 | + private |
| 126 | + |
| 127 | + def set_scouting_target |
| 128 | + @target = organization_scoped(ScoutingTarget).find(params[:id]) |
| 129 | + end |
| 130 | + |
| 131 | + def scouting_target_params |
| 132 | + params.require(:scouting_target).permit( |
| 133 | + :summoner_name, :real_name, :role, :region, :nationality, |
| 134 | + :age, :status, :priority, :current_team, |
| 135 | + :current_tier, :current_rank, :current_lp, |
| 136 | + :peak_tier, :peak_rank, |
| 137 | + :riot_puuid, :riot_summoner_id, |
| 138 | + :email, :phone, :discord_username, :twitter_handle, |
| 139 | + :scouting_notes, :contact_notes, |
| 140 | + :availability, :salary_expectations, |
| 141 | + :performance_trend, :assigned_to_id, |
| 142 | + champion_pool: [] |
| 143 | + ) |
| 144 | + end |
| 145 | +end |
0 commit comments