From 12045c684edc3b4605ab05feb9cd54bf33e28dc4 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Thu, 8 Dec 2022 01:32:41 -0500 Subject: [PATCH] Added new ItemComment model --- db/migrations/0002_add_item_comments_table.rb | 20 +++++++++++++++ lib/models/item.rb | 2 +- lib/models/item_comment.rb | 5 ++++ lib/routes.rb | 9 +++++++ raven.rb | 1 + views/item/view.erb | 25 +++++++++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 db/migrations/0002_add_item_comments_table.rb create mode 100644 lib/models/item_comment.rb diff --git a/db/migrations/0002_add_item_comments_table.rb b/db/migrations/0002_add_item_comments_table.rb new file mode 100644 index 0000000..5620a75 --- /dev/null +++ b/db/migrations/0002_add_item_comments_table.rb @@ -0,0 +1,20 @@ +Sequel.migration do + + up do + create_table(:item_comments) do + primary_key :id + String :body, null: false + DateTime :created_at + DateTime :updated_at + end + + alter_table(:item_comments) do + add_foreign_key :item_id, :items + end + end + + down do + drop_table(:item_comments) + end + +end diff --git a/lib/models/item.rb b/lib/models/item.rb index ab0351d..0b7b427 100644 --- a/lib/models/item.rb +++ b/lib/models/item.rb @@ -1,5 +1,5 @@ class Item < Sequel::Model - + one_to_many :item_comments end diff --git a/lib/models/item_comment.rb b/lib/models/item_comment.rb new file mode 100644 index 0000000..a4c8b1b --- /dev/null +++ b/lib/models/item_comment.rb @@ -0,0 +1,5 @@ +class ItemComment < Sequel::Model + + many_to_one :items + +end diff --git a/lib/routes.rb b/lib/routes.rb index df123b0..55b8201 100644 --- a/lib/routes.rb +++ b/lib/routes.rb @@ -44,3 +44,12 @@ get '/item/:item_id' do :item => item } end + +post '/item/:item_id/comment' do + item = Item.first(id: params[:item_id]) + + comment = ItemComment.create(body: params[:comment_body]) + item.add_item_comment(comment) + + redirect "/item/#{item.id}" +end diff --git a/raven.rb b/raven.rb index b9ac9d3..dbca99a 100644 --- a/raven.rb +++ b/raven.rb @@ -22,6 +22,7 @@ Sequel::Model.plugin :timestamps DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database')) # Load models require_relative 'lib/models/item.rb' +require_relative 'lib/models/item_comment.rb' # Load helper functions require_relative 'lib/helpers.rb' diff --git a/views/item/view.erb b/views/item/view.erb index 71566fe..cb384c3 100644 --- a/views/item/view.erb +++ b/views/item/view.erb @@ -34,3 +34,28 @@ + +
+
+ <% if item.item_comments.length > 0 %> +
    + <% item.item_comments.each do |comment| %> +
  • <%= comment.body %>
  • + <% end %> +
+ <% else %> +

There are no comments to display at this time.

+ <% end %> +
+
+ +
+
+
+ + + + +
+
+