namespace '/item' do get '' do redirect '/item/list' end get '/list' do items = Item.reverse(:updated_at).all() erb :'item/list', :locals => { :title => 'List of Items', :items => items } end get '/create' do erb :'item/create', :locals => { :title => 'Create New Item' } end post '/create' do item = Item.create( name: params[:item_name], serial_number: params[:item_serial], sku_number: params[:item_sku], purchased_from: params[:item_purchase_from], purchased_at: params[:item_purchase_date], manufacturer: params[:item_manufacturer], type: params[:item_type] ) redirect "/item/#{item.id}" end get '/:item_id' do item = Item.where(id: params[:item_id]).first() puts "#{item.name}" erb :'item/view', :locals => { :title => item.name, :item => item } end get '/:item_id/edit' do item = Item.where(id: params[:item_id]).first() puts "#{item.name}" erb :'item/edit', :locals => { :title => "Editing: #{item.name}", :item => item } end post '/:item_id/edit' do item = Item.where(id: params[:item_id]).first() item.name = params[:item_name] item.serial_number = params[:item_serial] item.sku_number = params[:item_sku] item.purchased_from = params[:item_purchase_from] item.purchased_at = params[:item_purchase_date] item.manufacturer = params[:item_manufacturer] item.type = params[:item_type] item.save() redirect "/item/#{item.id}" end get '/:item_id/delete' do item = Item.where(id: params[:item_id]).first() item.delete() redirect '/item/list' end post '/: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 end