Added channel and video models, views, and routes

This commit is contained in:
Gregory Ballantine 2023-03-03 14:19:20 -05:00
parent 646d2d19f9
commit 8a538cb018
16 changed files with 245 additions and 8 deletions

View File

@ -5,7 +5,6 @@ Sequel.migration do
primary_key :id
String :name, null: false
String :description
DateTime :purchased_at
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
end

View File

@ -6,7 +6,6 @@ Sequel.migration do
String :name, null: false
String :description
Integer :serial, null: false, unique: true
DateTime :purchased_at
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
end

View File

@ -18,4 +18,8 @@ helpers do
return dt.strftime('%Y-%m-%dT%H:%M:%S')
end
def serialize(num)
return num
end
end

View File

@ -2,4 +2,8 @@ class Channel < Sequel::Model
one_to_many :videos
def openProjects()
return 0
end
end

View File

@ -1,5 +1,5 @@
class Video < Sequel::Model
many_to_one :channels
many_to_one :channel
end

View File

@ -1 +1,5 @@
require_relative 'routes/index.rb'
require_relative 'routes/channel.rb'
require_relative 'routes/video.rb'

37
lib/routes/channel.rb Normal file
View File

@ -0,0 +1,37 @@
namespace '/channel' do
get '' do
redirect '/channel/list'
end
get '/list' do
channels = Channel.reverse(:updated_at).all()
erb :'channel/list', :locals => {
:title => 'List of channels',
:channels => channels
}
end
get '/create' do
erb :'channel/create', :locals => {
:title => 'Create new channel'
}
end
post '/create' do
channel = Channel.create(
name: params[:channel_name],
description: params[:channel_description]
)
redirect "/channel/#{channel.id}"
end
get '/:channel_id' do
channel = Channel.where(id: params[:channel_id]).first()
puts "#{channel.name}"
erb :'channel/view', :locals => {
:title => channel.name,
:channel => channel
}
end
end

41
lib/routes/video.rb Normal file
View File

@ -0,0 +1,41 @@
namespace '/video' do
get '' do
redirect '/video/list'
end
get '/list' do
videos = Video.reverse(:updated_at).all()
erb :'video/list', :locals => {
:title => 'List of videos',
:videos => videos
}
end
get '/create' do
channels = Channel.all()
erb :'video/create', :locals => {
:title => 'Create new video',
:channels => channels
}
end
post '/create' do
video = Video.create(
serial: params[:video_serial],
name: params[:video_name],
channel_id: params[:video_channel],
description: params[:video_description]
)
redirect "/video/#{video.id}"
end
get '/:video_id' do
video = Video.where(id: params[:video_id]).first()
puts "#{video.name}"
erb :'video/view', :locals => {
:title => video.name,
:video => video
}
end
end

27
views/channel/create.erb Normal file
View File

@ -0,0 +1,27 @@
<div class="row">
<div class="twelve columns">
<h1>Create new channel</h1>
</div>
</div>
<div class="row">
<div class="twelve columns">
<form action="/channel/create" method="POST" class="u-full-width">
<div class="row">
<div class="columns twelve">
<label for="channel_name">Channel name:</label>
<input class="u-full-width" type="text" placeholder="My new channel" id="channel_name" name="channel_name" required>
</div>
</div>
<div class="row">
<div class="twelve columns">
<label for="channel_description">Channel description:</label>
<textarea class="u-full-width" type="text" placeholder="Description of the channel" id="channel_description" name="channel_description"></textarea>
</div>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
</div>

21
views/channel/list.erb Normal file
View File

@ -0,0 +1,21 @@
<div class="row">
<div class="twelve columns">
<h1>Hardware Inventory List</h1>
</div>
</div>
<div class="row">
<div class="twelve columns">
<p><a href="/channel/create">Create new channel</a></p>
<% if channels.length > 0 %>
<ul>
<% channels.each do |channel| %>
<li><a href="/channel/<%= channel.id %>"><%= channel.name %></a></li>
<% end %>
</ul>
<% else %>
<p>There are no channels recorded in the database yet.</p>
<% end %>
</div>
</div>

18
views/channel/view.erb Normal file
View File

@ -0,0 +1,18 @@
<div id="channel-header" class="row">
<div class="twelve columns">
<h1 class="channel-name"><%= channel.name %></h1>
<h4 class="channel-created">Item added at: <%= date_format(channel.created_at) %></h4>
<% if channel.updated_at %>
<h4 class="channel-updated">Last updated at: <%= date_format(channel.updated_at) %></h4>
<% end %>
</div>
</div>
<div class="row">
<div class="twelve columns">
<p class="inventory-actions">
<a href="/channel/<%= channel.id %>/edit"><i class="fa-solid fa-pen-to-square"></i></a>
<a href="/channel/<%= channel.id %>/delete"><i class="fa-solid fa-trash"></i></a>
</p>
</div>
</div>

View File

@ -14,16 +14,16 @@
<th>Channel name</th>
<th>Open projects</th>
<th>Total projects</th>
<th>Last updated!</th>
<th>Last updated</th>
</tr>
</thead>
<tbody>
<% channels.each do |c| %>
<tr>
<td><%= c.name %></td>
<td><a href="/channel/<%= c.id %>"><%= c.name %></a></td>
<td><%= nullable(c.openProjects) %></td>
<td><%= nullable(c.videos.length) %></td>
<td><%= date_format(c.last_updated) %></td>
<td><%= date_format(c.updated_at) %></td>
</tr>
<% end %>
</tbody>

View File

@ -2,7 +2,7 @@
<h3>Stage Manager</h3>
<ul>
<li><a href="/">Dashboard</a></li>
<li><a href="/channels">Channels</a></li>
<li><a href="/videos">Videos</a></li>
<li><a href="/channel">Channels</a></li>
<li><a href="/video">Videos</a></li>
</ul>
</nav>

43
views/video/create.erb Normal file
View File

@ -0,0 +1,43 @@
<div class="row">
<div class="twelve columns">
<h1>Create new video</h1>
</div>
</div>
<div class="row">
<div class="twelve columns">
<form action="/video/create" method="POST" class="u-full-width">
<div class="row">
<div class="columns twelve">
<label for="video_name">Video name:</label>
<input class="u-full-width" type="text" placeholder="My new video" id="video_name" name="video_name" required>
</div>
</div>
<div class="row">
<div class="columns three">
<label for="video_serial">Video serial:</label>
<input class="u-full-width" type="number" placeholder="0001" id="video_serial" name="video_serial" required>
</div>
<div class="columns nine">
<label for="video_channel">Associated channel:</label>
<select class="u-full-width" id="video_channel" name="video_channel" required>
<% channels.each do |c| %>
<option value="<%= c.id %>"><%= c.name %></option>
<% end %>
</select>
</div>
</div>
<div class="row">
<div class="twelve columns">
<label for="video_description">Video description:</label>
<textarea class="u-full-width" type="text" placeholder="Description of the video" id="video_description" name="video_description"></textarea>
</div>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
</div>

21
views/video/list.erb Normal file
View File

@ -0,0 +1,21 @@
<div class="row">
<div class="twelve columns">
<h1>Hardware Inventory List</h1>
</div>
</div>
<div class="row">
<div class="twelve columns">
<p><a href="/video/create">Create new video</a></p>
<% if videos.length > 0 %>
<ul>
<% videos.each do |video| %>
<li><a href="/video/<%= video.id %>"><%= video.name %></a></li>
<% end %>
</ul>
<% else %>
<p>There are no videos recorded in the database yet.</p>
<% end %>
</div>
</div>

19
views/video/view.erb Normal file
View File

@ -0,0 +1,19 @@
<div id="video-header" class="row">
<div class="twelve columns">
<h1 class="video-name"><%= video.name %></h1>
<h3 class="video-serial"><%= video.channel.name %> - <%= serialize(video.serial) %></h3>
<h4 class="video-created">Item added at: <%= date_format(video.created_at) %></h4>
<% if video.updated_at %>
<h4 class="video-updated">Last updated at: <%= date_format(video.updated_at) %></h4>
<% end %>
</div>
</div>
<div class="row">
<div class="twelve columns">
<p class="inventory-actions">
<a href="/video/<%= video.id %>/edit"><i class="fa-solid fa-pen-to-square"></i></a>
<a href="/video/<%= video.id %>/delete"><i class="fa-solid fa-trash"></i></a>
</p>
</div>
</div>