Added new ItemComment model

This commit is contained in:
Gregory Ballantine 2022-12-08 01:32:41 -05:00
parent 11d33e394b
commit 12045c684e
6 changed files with 61 additions and 1 deletions

View File

@ -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

View File

@ -1,5 +1,5 @@
class Item < Sequel::Model class Item < Sequel::Model
one_to_many :item_comments
end end

View File

@ -0,0 +1,5 @@
class ItemComment < Sequel::Model
many_to_one :items
end

View File

@ -44,3 +44,12 @@ get '/item/:item_id' do
:item => item :item => item
} }
end 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

View File

@ -22,6 +22,7 @@ Sequel::Model.plugin :timestamps
DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database')) DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database'))
# Load models # Load models
require_relative 'lib/models/item.rb' require_relative 'lib/models/item.rb'
require_relative 'lib/models/item_comment.rb'
# Load helper functions # Load helper functions
require_relative 'lib/helpers.rb' require_relative 'lib/helpers.rb'

View File

@ -34,3 +34,28 @@
</table> </table>
</div> </div>
</div> </div>
<div class="row">
<div class="twelve columns">
<% if item.item_comments.length > 0 %>
<ul class="u-full-width">
<% item.item_comments.each do |comment| %>
<li><%= comment.body %></li>
<% end %>
</ul>
<% else %>
<p>There are no comments to display at this time.</p>
<% end %>
</div>
</div>
<div class="row">
<div class="twelve columns">
<form action="/item/<%= item.id %>/comment" method="POST" class="u-full-width">
<label for="comment_body">Add a comment:</label>
<textarea name="comment_body" id="comment_body" class="u-full-width" cols="30" rows="10"></textarea>
<input class="button button-primary" type="submit" value="Submit">
</form>
</div>
</div>