Added project model; added views for viewing and adding projects

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

View File

@ -0,0 +1,35 @@
Sequel.migration do
up do
# create projects table
create_table(:projects) do
primary_key :id
String :name, null: false
String :description
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
end
# create tables for project many-to-many relationships
create_table(:projects_hardware) do
foreign_key(:project_id, :projects, null: false)
foreign_key(:hardware_id, :hardware, null: false)
end
create_table(:projects_benchmarks) do
foreign_key(:project_id, :projects, null: false)
foreign_key(:benchmark_id, :benchmarks, null: false)
end
create_table(:projects_tests) do
foreign_key(:project_id, :projects, null: false)
foreign_key(:test_id, :tests, null: false)
end
end
down do
drop_table(:projects_tests)
drop_table(:projects_benchmarks)
drop_table(:projects_hardware)
drop_table(:projects)
end
end

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

View File

@ -2,11 +2,10 @@
<div class="top-bar-left">
<ul class="menu">
<li><a href="/">Dashboard</a></li>
<li><a href="/project">Projects</a></li>
<li><a href="/hardware">Hardware</a></li>
<li><a href="/benchmark">Benchmarks</a></li>
<li><a href="/test">Tests</a></li>
<li><a href="/result">Results</a></li>
<li><a href="/comparison">Comparisons</a></li>
</ul>
</div>
</div>

53
views/project/add.erb Normal file
View File

@ -0,0 +1,53 @@
<div class="grid-x grid-margin-x">
<div class="cell small-12">
<h1>Add new project</h1>
</div>
</div>
<div class="grid-x grid-margin-x">
<form class="cell small-12" action="/project/add" method="post">
<div class="grid-x grid-padding-x">
<div class="cell small-12">
<label for="project_name">
Project name
<input id="project_name" type="text" name="project_name" placeholder="Example hardware">
</label>
</div>
<div class="cell small-12">
<label for="project_description">
Project description:
<textarea id="project_description" cols="30" rows="10" name="project_description"></textarea>
</label>
</div>
</div>
<div class="grid-x grid-padding-x">
<div class="cell small-12 medium-6">
<label for="project_hardware">
Project hardware:
<select id="project_hardware" name="project_hardware[]" multiple>
<% hardware.each do |h| %>
<option value="<%= h.id %>"><%= h.name %></option>
<% end %>
</select>
</label>
</div>
<div class="cell small-12 medium-6">
<label for="project_benchmarks">
Project benchmarks:
<select id="project_benchmarks" name="project_benchmarks[]" multiple>
<% benchmarks.each do |b| %>
<option value="<%= b.id %>"><%= b.name %></option>
<% end %>
</select>
</label>
</div>
</div>
<input type="submit" class="button" value="Submit">
</form>
</div>

40
views/project/index.erb Normal file
View File

@ -0,0 +1,40 @@
<div class="grid-x grid-margin-x">
<div class="cell small-12">
<h1>List of projects</h1>
</div>
<div class="cell small-12">
<p>
<a href="/project/add">Add new project</a>
</p>
</div>
</div>
<div class="grid-x grid-margin-x">
<% if projects.length > 0 %>
<div class="cell small-12">
<table>
<thead>
<tr>
<th>Project name</th>
<th>Date added</th>
<th>Date modified</th>
</tr>
</thead>
<tbody>
<% projects.each do |p| %>
<tr>
<td><a href="/project/<%= p.id %>"><%= p.name %></a></td>
<td><%= date_format(p.created_at) %></td>
<td><%= date_format(p.updated_at) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="cell small-12">
<p>I'm sorry, there doesn't appear to be any projects added yet. Check again later!</p>
</div>
<% end %>
</div>

39
views/project/view.erb Normal file
View File

@ -0,0 +1,39 @@
<div class="grid-x grid-margin-x">
<div class="cell small-12">
<h1><%= project.name %></h1>
</div>
<div class="cell small-12">
<p>
<a href="/project/<%= project.id %>/edit/">Edit project</a>
</p>
</div>
</div>
<div class="grid-x grid-margin-x">
<div class="cell small-6">
<h3>Hardware:</h3>
<ul>
<% project.hardware.each do |h| %>
<li><%= h.name %></li>
<% end %>
</ul>
</div>
<div class="cell small-6">
<h3>Benchmarks:</h3>
<ul>
<% project.benchmarks.each do |b| %>
<li><%= b.name %></li>
<% end %>
</ul>
</div>
</div>
<div class="grid-x grid-margin-x">
<h3>Project description:</h3>
<p><%= project.description %></p>
</div>