Skip to content

Commit

Permalink
wip:form not working
Browse files Browse the repository at this point in the history
  • Loading branch information
krzykamil committed Apr 19, 2024
1 parent 1dcd895 commit 141a5b3
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 27 deletions.
1 change: 1 addition & 0 deletions config/app.rb
Expand Up @@ -20,5 +20,6 @@ class App < Hanami::App
end
config.shared_app_component_keys += ["redis", "db", "persistence.rom", "shrine"]
config.middleware.use Hanami::Middleware::BodyParser, :form
config.middleware.use Rack::MethodOverride
end
end
1 change: 1 addition & 0 deletions db/migrate/20231229091551_create_books.rb
Expand Up @@ -9,6 +9,7 @@
column :published_date, Date, null: false
column :description, :text, null: true
column :image_url, String, null: true
column :image_data, :jsonb, null: true
column :category, String, null: true
column :language, String, null: true
column :isbn_10, String, null: true
Expand Down
11 changes: 0 additions & 11 deletions db/migrate/20240116122351_create_storages.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/libus/persistence/entities/book.rb
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Libus
module Persistence
module Entities
class Book < ROM::Struct
include ImageUploader::Attachment(:image)
end
end
end
end

2 changes: 2 additions & 0 deletions lib/libus/persistence/relations/books.rb
Expand Up @@ -4,11 +4,13 @@ module Libus
module Persistence
module Relations
class Books < ROM::Relation[:sql]
include ImageUploader::Attachment(:image)
schema(:books, infer: true) do
associations do
belongs_to :author
end
end
struct_namespace Entities
auto_struct(true)
end
end
Expand Down
13 changes: 0 additions & 13 deletions lib/libus/persistence/relations/storages.rb

This file was deleted.

13 changes: 13 additions & 0 deletions slices/main/actions/books/edit.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Main
module Actions
module Books
class Edit < Main::Action
def handle(request, response)
response.render view, id: request.params[:id]
end
end
end
end
end
13 changes: 13 additions & 0 deletions slices/main/actions/books/update.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Main
module Actions
module Books
class Update < Main::Action
def handle(request, response)
puts "na razie nic"
end
end
end
end
end
2 changes: 2 additions & 0 deletions slices/main/config/routes.rb
Expand Up @@ -15,6 +15,8 @@ class Routes < Hanami::Routes
delete "/logout", to: "sessions.destroy"

get '/books', to: 'books.index'
get '/books/:id/edit', to: 'books.edit'
patch '/books/:id', to: 'books.update'

get '/storage', to: 'storages.index'
scope 'storages' do
Expand Down
4 changes: 4 additions & 0 deletions slices/main/repositories/books.rb
Expand Up @@ -16,6 +16,10 @@ def by_isbn(type:, identifier:)
books.where(isbn_13: identifier).first
end
end

def by_id(id)
books.by_pk(id).one!
end
end
end
end
2 changes: 1 addition & 1 deletion slices/main/templates/books/_avatar.html.erb
@@ -1,5 +1,5 @@
<div class="avatar">
<div class="<%= size %> rounded">
<img src="<%=book.value.image_url.gsub('http', 'https') %>" />
<!-- <img src="<%#=book.value.image_url.gsub('http', 'https') %>" />-->
</div>
</div>
5 changes: 5 additions & 0 deletions slices/main/templates/books/edit.html.erb
@@ -0,0 +1,5 @@
<%= form_for book, "/books/#{id}", method: :patch do %>
<input name="book[image]" type="hidden" value="<%= book.image_data %>" />
<input name="book[image] "type="file" />
<button class="btn btn-primary" type="submit">Submit</button>
<% end %>
8 changes: 6 additions & 2 deletions slices/main/templates/books/index.html.erb
Expand Up @@ -24,7 +24,7 @@
</label>
</th>
<td>
<div class="flex items-center gap-3">
<div id="picture" class="flex">
<div class="avatar">
<div class="mask mask-squircle w-32 h-32">
<%= book.avatar(:sm)%>
Expand All @@ -41,6 +41,9 @@
<th>
<div class="text-sm opacity-50"><%= book.author.name %></div>
</th>
<th>
<button class="btn btn-sm btn-ghost" hx-get="/books/<%= book.id %>/edit" hx-target="#picture" hx-swap="innerHTML" hx-trigger="click">Edit</button>
</th>
</tr>
<% end %>

Expand All @@ -52,7 +55,8 @@
<th>Title</th>
<th>Genre</th>
<th>Author</th>
<th></th>
<th>
</th>
</tr>
</tfoot>

Expand Down
15 changes: 15 additions & 0 deletions slices/main/views/books/edit.rb
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Main
module Views
module Books
class Edit < Main::View
include Deps['repositories.books']
expose :id
expose :book do |id:|
books.by_id(id)
end
end
end
end
end

0 comments on commit 141a5b3

Please sign in to comment.