Class: FReCon::TeamsController

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

Class Method Summary collapse

Methods inherited from Controller

could_not_find, index, model, model_name, process_request, show_attribute, team_number_to_team_id

Class Method Details

.competitions(params) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/frecon/controllers/teams_controller.rb', line 111

def self.competitions(params)
	@team = Team.find_by number: params[:number]

	if @team
		@team.competitions.to_json
	else
		[404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
	end
end

.create(request, params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/frecon/controllers/teams_controller.rb', line 16

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

	@team = Team.new
	@team.attributes = post_data

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

.delete(params) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/frecon/controllers/teams_controller.rb', line 50

def self.delete(params)
	@team = Team.find_by number: params[:number]

	if @team
		if @team.destroy
			204
		else
			[422, ErrorFormatter.format(@team.errors.full_messages)]
		end
	else
		[404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
	end
end

.matches(params) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/frecon/controllers/teams_controller.rb', line 94

def self.matches(params)
	@team = Team.find_by number: params[:number]

	if @team
		# Ensure that the competition ID is valid.
		if params[:competition_id]
			@competition = Competition.find params[:competition_id]

			return [404, ErrorFormatter.format(could_not_find(params[:competition_id], "id", "competition"))] if @competition.nil?
		end

		@team.matches(params[:competition_id]).to_json
	else
		[404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
	end
end

.records(params) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/frecon/controllers/teams_controller.rb', line 74

def self.records(params)
	@team = Team.find_by number: params[:number]

	if @team
		if params[:competition_id]
			@competition = Competition.find params[:competition_id]

			if @competition
				@team.records.in(match_id: @competition.matches.map { |match| match.id }).to_json
			else
				[404, ErrorFormatter.format(could_not_find(params[:competition_id], "id", "competition"))]
			end
		else
			@team.records.to_json
		end
	else
		[404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
	end
end

.show(params) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/frecon/controllers/teams_controller.rb', line 64

def self.show(params)
	@team = Team.find_by number: params[:number]

	if @team
		@team.to_json
	else
		[404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
	end
end

.update(request, params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/frecon/controllers/teams_controller.rb', line 31

def self.update(request, params)
	return [400, "Must supply a team number!"] unless params[:number]

	post_data = process_request request
	return post_data if post_data.is_an?(Array)

	@team = Team.find_by number: params[:number]

	if @team.nil?
		return [404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
	end

	if @team.update_attributes(post_data)
		@team.to_json
	else
		[422, ErrorFormatter.format(@team.errors.full_messages)]
	end
end