From 646d2d19f9bd0ba3d586e01914c89e771df53efe Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 3 Mar 2023 13:05:01 -0500 Subject: [PATCH] Added Sequel migrations and models for videos and channels; Fixed database name in Rakefile and defaults config file --- .gitignore | 2 +- data/defaults.yaml | 2 +- db/migrations/0001_add_channels_table.rb | 18 ++++++++++++++++++ db/migrations/0002_add_videos_table.rb | 23 +++++++++++++++++++++++ lib/models/channel.rb | 5 +++++ lib/models/video.rb | 5 +++++ lib/routes/index.rb | 9 +-------- server.rb | 3 ++- views/index.erb | 8 ++++---- 9 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 db/migrations/0001_add_channels_table.rb create mode 100644 db/migrations/0002_add_videos_table.rb create mode 100644 lib/models/channel.rb create mode 100644 lib/models/video.rb diff --git a/.gitignore b/.gitignore index 91ce27b..a5fcad4 100644 --- a/.gitignore +++ b/.gitignore @@ -57,7 +57,7 @@ build-iPhoneSimulator/ # .rubocop-https?--* # Local database storage -data/raven.db +data/stgm.db # Node modules for Grunt.js node_modules/ diff --git a/data/defaults.yaml b/data/defaults.yaml index 6d7b685..3a1bb3f 100644 --- a/data/defaults.yaml +++ b/data/defaults.yaml @@ -1,3 +1,3 @@ database: adapter: 'sqlite' - database: 'data/raven.db' + database: 'data/stgm.db' diff --git a/db/migrations/0001_add_channels_table.rb b/db/migrations/0001_add_channels_table.rb new file mode 100644 index 0000000..d2e059c --- /dev/null +++ b/db/migrations/0001_add_channels_table.rb @@ -0,0 +1,18 @@ +Sequel.migration do + + up do + create_table(:channels) 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 + end + + down do + drop_table(:channels) + end + +end diff --git a/db/migrations/0002_add_videos_table.rb b/db/migrations/0002_add_videos_table.rb new file mode 100644 index 0000000..1c764c0 --- /dev/null +++ b/db/migrations/0002_add_videos_table.rb @@ -0,0 +1,23 @@ +Sequel.migration do + + up do + create_table(:videos) do + primary_key :id + 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 + + alter_table(:videos) do + add_foreign_key :channel_id, :channels + end + end + + down do + drop_table(:videos) + end + +end diff --git a/lib/models/channel.rb b/lib/models/channel.rb new file mode 100644 index 0000000..87bf1d8 --- /dev/null +++ b/lib/models/channel.rb @@ -0,0 +1,5 @@ +class Channel < Sequel::Model + + one_to_many :videos + +end diff --git a/lib/models/video.rb b/lib/models/video.rb new file mode 100644 index 0000000..d2522af --- /dev/null +++ b/lib/models/video.rb @@ -0,0 +1,5 @@ +class Video < Sequel::Model + + many_to_one :channels + +end diff --git a/lib/routes/index.rb b/lib/routes/index.rb index 506dfae..67869c9 100644 --- a/lib/routes/index.rb +++ b/lib/routes/index.rb @@ -1,14 +1,7 @@ namespace '/' do get '' do - channels = [ - { - :name => 'Bit Goblin', - :openProjects => 'N/a', - :totalProjects => 'N/a', - :last_updated => 'N/a' - } - ] + channels = Channel.reverse(:updated_at).limit(10).all() erb :index, :locals => { :title => 'Dashboard', :channels => channels diff --git a/server.rb b/server.rb index d493e8c..89c4e32 100644 --- a/server.rb +++ b/server.rb @@ -22,7 +22,8 @@ Sequel::Model.plugin :timestamps # Initialize Sequel gem for database actions DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database')) # Load models -# TODO - when models get made +require_relative 'lib/models/channel.rb' +require_relative 'lib/models/video.rb' # Load helper functions require_relative 'lib/helpers.rb' diff --git a/views/index.erb b/views/index.erb index 7bf71b7..1364b3d 100644 --- a/views/index.erb +++ b/views/index.erb @@ -20,10 +20,10 @@ <% channels.each do |c| %> - <%= c[:name] %> - <%= c[:openProjects] %> - <%= c[:totalProjects] %> - <%= c[:last_updated] %> + <%= c.name %> + <%= nullable(c.openProjects) %> + <%= nullable(c.videos.length) %> + <%= date_format(c.last_updated) %> <% end %>