Added project model; added views for viewing and adding projects

This commit is contained in:
2023-11-29 16:48:11 -05:00
parent c940a248d7
commit c5169f1d15
12 changed files with 259 additions and 4 deletions

View File

@ -2,4 +2,10 @@ class Benchmark < Sequel::Model
one_to_many :tests
# many-to-many with Project
many_to_many :projects,
left_id: :project_id,
right_id: :benchmark_id,
join_table: :projects_benchmarks
end

View File

@ -2,4 +2,10 @@ class Hardware < Sequel::Model(:hardware)
one_to_many :tests
# many-to-many with Project
many_to_many :projects,
left_id: :project_id,
right_id: :hardware_id,
join_table: :projects_hardware
end

View File

@ -1,4 +1,5 @@
require_relative 'hardware'
require_relative 'benchmark'
require_relative 'hardware'
require_relative 'project'
require_relative 'result'
require_relative 'test'

21
src/models/project.rb Normal file
View File

@ -0,0 +1,21 @@
class Project < Sequel::Model(:projects)
# many-to-many with Hardware
many_to_many :hardware,
left_id: :project_id,
right_id: :hardware_id,
join_table: :projects_hardware
# many-to-many with Benchmark
many_to_many :benchmarks,
left_id: :project_id,
right_id: :benchmark_id,
join_table: :projects_benchmarks
# many-to-many with Test
many_to_many :tests,
left_id: :project_id,
right_id: :test_id,
join_table: :projects_tests
end

View File

@ -4,6 +4,12 @@ class Test < Sequel::Model
many_to_one :benchmark # link Test model back to its benchmark
many_to_one :hardware # link Test model back to hardware used in test
# many-to-many with Project
many_to_many :projects,
left_id: :project_id,
right_id: :test_id,
join_table: :projects_tests
# formats the name of the test for display in the web UI
def formatted_name()
return "#{self.date_tag} - #{self.hardware.name} / #{self.benchmark.name}"

View File

@ -1,5 +1,6 @@
require_relative 'index'
require_relative 'hardware'
require_relative 'benchmark'
require_relative 'hardware'
require_relative 'project'
require_relative 'result'
require_relative 'test'

48
src/routes/project.rb Normal file
View File

@ -0,0 +1,48 @@
class GameData < Sinatra::Base
get '/project' do
projects = Project.reverse(:updated_at).limit(10).all()
erb :'project/index', locals: {
title: 'List of Projects',
projects: projects
}
end
get '/project/add' do
hardware = Hardware.all()
benchmarks = Benchmark.all()
erb :'project/add', locals: {
title: 'Add Project',
hardware: hardware,
benchmarks: benchmarks,
}
end
post '/project/add' do
project = Project.create(
name: params[:project_name],
description: params[:project_description]
)
params[:project_hardware].each do |h|
hardware = Hardware.where(:id => h).first()
project.add_hardware(hardware)
end
params[:project_benchmarks].each do |b|
benchmark = Benchmark.where(:id => b).first()
project.add_benchmark(benchmark)
end
redirect '/project'
end
get '/project/:project_id' do
project = Project.where(:id => params[:project_id]).first()
erb :'project/view', locals: {
title: project.name,
project: project,
}
end
end