Class: FReCon::RecordsController

Inherits:
Controller show all
Defined in:
lib/frecon/controllers/records_controller.rb

Class Method Summary collapse

Methods inherited from Controller

could_not_find, delete, index, model, model_name, process_request, show, show_attribute, team_number_to_team_id, update

Class Method Details

.competition(params) ⇒ Object



77
78
79
# File 'lib/frecon/controllers/records_controller.rb', line 77

def self.competition(params)
  show_attribute params, :competition
end

.create(request, params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/frecon/controllers/records_controller.rb', line 16

def self.create(request, params)
  post_data = process_request request
  return post_data if post_data.is_an?(Array)

  # Change special post_data attributes.
  # Convert team number to team id.
  post_data = team_number_to_team_id(post_data)
  return post_data if post_data.is_an?(Array)

  # Convert match number and competition name to match id.
  if post_data["match_number"] && !post_data["match_id"]
    if post_data["competition_name"] && (competition = Competition.find_by name: post_data["competition_name"])
      # Try to set the match to the already existing match.
      begin
        match = competition.matches.find_by number: post_data["match_number"]
      rescue ArgumentError, TypeError => e
        return [422, ErrorFormatter.format(e.message)]
      end

      # Create the match if necessary.
      begin
        match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
      rescue ArgumentError, TypeError => e
        return [422, ErrorFormatter.format(e.message)]
      end

      post_data["match_id"] = match.id

      post_data.delete("match_number")
      post_data.delete("competition_name")
    elsif post_data["competition"] && post_data["competition"]["_id"] && post_data["competition"]["_id"]["$oid"] && (competition = Competition.find_by(id: post_data["competition"]["_id"]["$oid"]))
      # Try to set the match to the already existing match.
      match = competition.matches.find_by number: post_data["match_number"]

      # Create the match if necessary.
      begin
        match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
      rescue ArgumentError, TypeError => e
        return [422, ErrorFormatter.format(e.message)]
      end

      post_data["match_id"] = match.id

      post_data.delete("match_number")
      post_data.delete("competition")
    else
      return [422, ErrorFormatter.format("A current competition is not set.  Please set it.")]
    end
  end

  @record = Record.new
  @record.attributes = post_data

  if @record.save
    # Use to_json for now; we can filter it later.
    [201, @record.to_json]
  else
    [422, ErrorFormatter.format(@record.errors.full_messages)]
  end
end