From 640e8d34ab83f9a630eb1f9877642e3e7eedbdc4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 3 Sep 2025 00:12:44 -0500 Subject: [PATCH] Added CRUD but edit and delete giving errors. --- Gemfile.lock | 6 ++--- app.rb | 30 ++++++++++++++++++++++++- db/migrations/0001_create_assistants.rb | 1 + models/assistant.rb | 10 +++++++++ views/index.erb | 12 +++++++++- 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 8c0e19f..1b51a58 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -65,7 +65,7 @@ GEM puma (6.6.1) nio4r (~> 2.0) racc (1.8.1) - rack (3.2.0) + rack (3.2.1) rack-test (2.2.0) rack (>= 1.3) rainbow (3.1.1) @@ -78,7 +78,7 @@ GEM listen (~> 3.0) roda (3.95.0) rack - rubocop (1.80.0) + rubocop (1.80.1) json (~> 2.3) language_server-protocol (~> 3.17.0.2) lint_roller (~> 1.1.0) @@ -93,7 +93,7 @@ GEM parser (>= 3.3.7.2) prism (~> 1.4) ruby-progressbar (1.13.0) - sequel (5.95.1) + sequel (5.96.0) bigdecimal sqlite3 (2.7.3-aarch64-linux-gnu) sqlite3 (2.7.3-aarch64-linux-musl) diff --git a/app.rb b/app.rb index 096b0b4..d9fbb2b 100644 --- a/app.rb +++ b/app.rb @@ -11,6 +11,7 @@ require_relative 'models/assistant' class App < Roda plugin :render, escape: true plugin :sessions, secret: ENV.delete('APP_SESSION_SECRET') + plugin :all_verbs route do |r| r.root do @@ -27,14 +28,41 @@ class App < Roda @assistant = Assistant.new view :edit end + + r.get ':id/edit' do |id| + @page_title = 'Edit Assistant' + @assistant = Assistant[id] + if @assistant.nil? + r.halt(404) + else + view :edit + end + end + r.post do @assistant = Assistant.new(r.params) if @assistant.save - r.redirect '/' # change to just r.redirect when get a host.org/assistants page up + r.redirect '/' + else + @page_title = 'Create New Assistant' + view :edit + end + end + + r.put ':id' do |id| + @assistant = Assistant[id] + if @assistant && @assistant.update(r.params) + r.redirect '/' else r.halt(404) end end + + r.delete ':id' do |id| + @assistant = Assistant[id] + @assistant.destroy if @assistant + r.redirect '/' + end end end end diff --git a/db/migrations/0001_create_assistants.rb b/db/migrations/0001_create_assistants.rb index c9b05e3..2a1053f 100644 --- a/db/migrations/0001_create_assistants.rb +++ b/db/migrations/0001_create_assistants.rb @@ -14,6 +14,7 @@ Sequel.migration do String :capabilities String :test_results DateTime :created_at, null: false, default: Sequel::CURRENT_TIMESTAMP + DateTime :updated_at end end end diff --git a/models/assistant.rb b/models/assistant.rb index 1101aae..7c2652f 100644 --- a/models/assistant.rb +++ b/models/assistant.rb @@ -11,6 +11,7 @@ class Assistant < Sequel::Model def validate super errors.add(:name, 'must be present') if name.nil? || name.empty? + errors.add(:run_command, 'must be present') if run_command.nil? || run_command.empty? end # Optional: Add custom methods for common operations @@ -18,7 +19,16 @@ class Assistant < Sequel::Model where(name: name).first end + def self.find_by_run_command(run_command) + where(run_command: run_command).first + end + def self.active where(active: true) end + + # Helper to check if this is a new record + def new? + id.nil? + end end diff --git a/views/index.erb b/views/index.erb index 8bd9f7c..8109b4c 100644 --- a/views/index.erb +++ b/views/index.erb @@ -12,6 +12,7 @@ Evaluation Rate Capabilities Created At + Actions @@ -25,10 +26,19 @@ <%= assistant.eval_rate || 'N/A' %> <%= assistant.capabilities || 'N/A' %> <%= assistant.created_at.strftime('%Y-%m-%d %H:%M') if assistant.created_at %> + + Edit +
+ + +
+ <% end %> <% else %>

No assistants found in the database.

-<% end %> \ No newline at end of file +<% end %> + +

Add New Assistant