|
| 1 | +module Api |
| 2 | + module V1 |
| 3 | + module Scrims |
| 4 | + class ScrimsController < Api::V1::BaseController |
| 5 | + include TierAuthorization |
| 6 | + |
| 7 | + before_action :set_scrim, only: [:show, :update, :destroy, :add_game] |
| 8 | + |
| 9 | + # GET /api/v1/scrims |
| 10 | + def index |
| 11 | + scrims = current_organization.scrims |
| 12 | + .includes(:opponent_team, :match) |
| 13 | + .order(scheduled_at: :desc) |
| 14 | + |
| 15 | + # Filters |
| 16 | + scrims = scrims.by_type(params[:scrim_type]) if params[:scrim_type].present? |
| 17 | + scrims = scrims.by_focus_area(params[:focus_area]) if params[:focus_area].present? |
| 18 | + scrims = scrims.where(opponent_team_id: params[:opponent_team_id]) if params[:opponent_team_id].present? |
| 19 | + |
| 20 | + # Status filter |
| 21 | + case params[:status] |
| 22 | + when 'upcoming' |
| 23 | + scrims = scrims.upcoming |
| 24 | + when 'past' |
| 25 | + scrims = scrims.past |
| 26 | + when 'completed' |
| 27 | + scrims = scrims.completed |
| 28 | + when 'in_progress' |
| 29 | + scrims = scrims.in_progress |
| 30 | + end |
| 31 | + |
| 32 | + # Pagination |
| 33 | + page = params[:page] || 1 |
| 34 | + per_page = params[:per_page] || 20 |
| 35 | + |
| 36 | + scrims = scrims.page(page).per(per_page) |
| 37 | + |
| 38 | + render json: { |
| 39 | + data: { |
| 40 | + scrims: scrims.map { |scrim| ScrimSerializer.new(scrim).as_json }, |
| 41 | + meta: pagination_meta(scrims) |
| 42 | + } |
| 43 | + } |
| 44 | + end |
| 45 | + |
| 46 | + # GET /api/v1/scrims/calendar |
| 47 | + def calendar |
| 48 | + start_date = params[:start_date]&.to_date || Date.current.beginning_of_month |
| 49 | + end_date = params[:end_date]&.to_date || Date.current.end_of_month |
| 50 | + |
| 51 | + scrims = current_organization.scrims |
| 52 | + .includes(:opponent_team) |
| 53 | + .where(scheduled_at: start_date..end_date) |
| 54 | + .order(scheduled_at: :asc) |
| 55 | + |
| 56 | + render json: { |
| 57 | + data: { |
| 58 | + scrims: scrims.map { |scrim| ScrimSerializer.new(scrim, calendar_view: true).as_json }, |
| 59 | + start_date: start_date, |
| 60 | + end_date: end_date |
| 61 | + } |
| 62 | + } |
| 63 | + end |
| 64 | + |
| 65 | + # GET /api/v1/scrims/analytics |
| 66 | + def analytics |
| 67 | + service = ::Scrims::Services::ScrimAnalyticsService.new(current_organization) |
| 68 | + date_range = (params[:days]&.to_i || 30).days |
| 69 | + |
| 70 | + render json: { |
| 71 | + overall_stats: service.overall_stats(date_range: date_range), |
| 72 | + by_opponent: service.stats_by_opponent, |
| 73 | + by_focus_area: service.stats_by_focus_area, |
| 74 | + success_patterns: service.success_patterns, |
| 75 | + improvement_trends: service.improvement_trends |
| 76 | + } |
| 77 | + end |
| 78 | + |
| 79 | + # GET /api/v1/scrims/:id |
| 80 | + def show |
| 81 | + render json: { data: ScrimSerializer.new(@scrim, detailed: true).as_json } |
| 82 | + end |
| 83 | + |
| 84 | + # POST /api/v1/scrims |
| 85 | + def create |
| 86 | + # Check scrim creation limit |
| 87 | + unless current_organization.can_create_scrim? |
| 88 | + return render json: { |
| 89 | + error: 'Scrim Limit Reached', |
| 90 | + message: 'You have reached your monthly scrim limit. Upgrade to create more scrims.' |
| 91 | + }, status: :forbidden |
| 92 | + end |
| 93 | + |
| 94 | + scrim = current_organization.scrims.new(scrim_params) |
| 95 | + |
| 96 | + if scrim.save |
| 97 | + render json: { data: ScrimSerializer.new(scrim).as_json }, status: :created |
| 98 | + else |
| 99 | + render json: { errors: scrim.errors.full_messages }, status: :unprocessable_entity |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + # PATCH /api/v1/scrims/:id |
| 104 | + def update |
| 105 | + if @scrim.update(scrim_params) |
| 106 | + render json: { data: ScrimSerializer.new(@scrim).as_json } |
| 107 | + else |
| 108 | + render json: { errors: @scrim.errors.full_messages }, status: :unprocessable_entity |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + # DELETE /api/v1/scrims/:id |
| 113 | + def destroy |
| 114 | + @scrim.destroy |
| 115 | + head :no_content |
| 116 | + end |
| 117 | + |
| 118 | + # POST /api/v1/scrims/:id/add_game |
| 119 | + def add_game |
| 120 | + victory = params[:victory] |
| 121 | + duration = params[:duration] |
| 122 | + notes = params[:notes] |
| 123 | + |
| 124 | + if @scrim.add_game_result(victory: victory, duration: duration, notes: notes) |
| 125 | + # Update opponent team stats if present |
| 126 | + if @scrim.opponent_team.present? |
| 127 | + @scrim.opponent_team.update_scrim_stats!(victory: victory) |
| 128 | + end |
| 129 | + |
| 130 | + render json: { data: ScrimSerializer.new(@scrim.reload).as_json } |
| 131 | + else |
| 132 | + render json: { errors: @scrim.errors.full_messages }, status: :unprocessable_entity |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + private |
| 137 | + |
| 138 | + def set_scrim |
| 139 | + @scrim = current_organization.scrims.find(params[:id]) |
| 140 | + rescue ActiveRecord::RecordNotFound |
| 141 | + render json: { error: 'Scrim not found' }, status: :not_found |
| 142 | + end |
| 143 | + |
| 144 | + def scrim_params |
| 145 | + params.require(:scrim).permit( |
| 146 | + :opponent_team_id, |
| 147 | + :match_id, |
| 148 | + :scheduled_at, |
| 149 | + :scrim_type, |
| 150 | + :focus_area, |
| 151 | + :pre_game_notes, |
| 152 | + :post_game_notes, |
| 153 | + :is_confidential, |
| 154 | + :visibility, |
| 155 | + :games_planned, |
| 156 | + :games_completed, |
| 157 | + game_results: [], |
| 158 | + objectives: {}, |
| 159 | + outcomes: {} |
| 160 | + ) |
| 161 | + end |
| 162 | + |
| 163 | + def pagination_meta(collection) |
| 164 | + { |
| 165 | + current_page: collection.current_page, |
| 166 | + total_pages: collection.total_pages, |
| 167 | + total_count: collection.total_count, |
| 168 | + per_page: collection.limit_value |
| 169 | + } |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | +end |
0 commit comments