diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb
index 4eb947d..29b4e35 100644
--- a/app/controllers/books_controller.rb
+++ b/app/controllers/books_controller.rb
@@ -1,5 +1,5 @@
class BooksController < ApplicationController
- before_action :set_book, only: [:show, :edit, :update]
+ before_action :set_book, only: [:show, :edit, :update, :add_to_cart]
before_action :ensure_admin, only: [:edit, :update]
def index
@@ -23,6 +23,17 @@ class BooksController < ApplicationController
end
end
+ def add_to_cart
+ @book = Book.find(params[:id])
+ current_user.books << @book
+ @book.decrement!(:quantity)
+ redirect_to '/books', notice: 'Book added to your cart'
+ end
+
+ def shopping_cart
+ @books = current_user.books.map { |book| BooksPresenter.new(book) }
+ end
+
private
def set_book
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
index d894dc0..2b160a5 100644
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -15,4 +15,9 @@ class ApplicationRecord < ActiveRecord::Base
AuditRecord.create(model: self.class, action: 'create', params: result.to_json)
result
end
+ def decrement!(*args)
+ result = super(*args)
+ AuditRecord.create(model: self.class, action: 'decrement!', params: self.to_json)
+ result
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index a4f75f3..fef0b53 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,4 +1,6 @@
class User < ApplicationRecord
+ has_and_belongs_to_many :books
+
has_secure_password
has_secure_password :recovery_password, validations: false
enum role: [:customer, :admin], _default: :customer
diff --git a/app/views/books/index.html.erb b/app/views/books/index.html.erb
index 2359275..d13fc32 100644
--- a/app/views/books/index.html.erb
+++ b/app/views/books/index.html.erb
@@ -24,8 +24,8 @@
<%= book.quantity %>
- <% if logged_in? && book.quantity.positive? %>
- <%= link_to 'Add', edit_book_path(book), class: "btn" %>
+ <% if logged_in? && book.quantity.positive? && !current_user.books.exists?(book.id) %>
+ <%= link_to 'Add', "book/#{book.id}/add_to_cart", method: :post, class: "btn" %>
<% end %>
diff --git a/app/views/books/shopping_cart.erb b/app/views/books/shopping_cart.erb
new file mode 100644
index 0000000..8b6ee48
--- /dev/null
+++ b/app/views/books/shopping_cart.erb
@@ -0,0 +1,22 @@
+
+
+
Title
+
Authors
+
Price
+
+ <% @books.each do |book| %>
+
+
+ <%= link_to book.title, book %>
+
+
+
+ <%= book.authors %>
+
+
+
+ <%= book.price_with_currency %>
+
+
+ <% end %>
+
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index af8d743..904da48 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -32,7 +32,7 @@
<% end %>
<% if logged_in? %>
- <%= link_to 'Shopping cart', '/authors', method: :get%>
+ <%= link_to 'Shopping cart', '/shopping_cart', method: :get%>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index e9d2b49..4c8765d 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -9,6 +9,9 @@ Rails.application.routes.draw do
get 'recover_password/:id/:recovery_password', to: 'users#recover_password_form'
post 'recover_password', to: 'users#recover_password'
post 'user/:id/block', to: 'users#block'
+ post 'book/:id/add_to_cart', to: 'books#add_to_cart'
+ get 'shopping_cart', to: 'books#shopping_cart'
+
resources :books
resources :authors
end
diff --git a/db/migrate/20210322002803_create_books_users.rb b/db/migrate/20210322002803_create_books_users.rb
new file mode 100644
index 0000000..d852ec0
--- /dev/null
+++ b/db/migrate/20210322002803_create_books_users.rb
@@ -0,0 +1,10 @@
+class CreateBooksUsers < ActiveRecord::Migration[6.1]
+ def change
+ create_table :books_users do |t|
+ t.belongs_to :book
+ t.belongs_to :user
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 27ba16b..5580947 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 2021_03_21_235625) do
+ActiveRecord::Schema.define(version: 2021_03_22_002803) do
create_table "audit_records", force: :cascade do |t|
t.string "model"
@@ -47,6 +47,15 @@ ActiveRecord::Schema.define(version: 2021_03_21_235625) do
t.index ["published"], name: "index_books_on_published"
end
+ create_table "books_users", force: :cascade do |t|
+ t.integer "book_id"
+ t.integer "user_id"
+ t.datetime "created_at", precision: 6, null: false
+ t.datetime "updated_at", precision: 6, null: false
+ t.index ["book_id"], name: "index_books_users_on_book_id"
+ t.index ["user_id"], name: "index_books_users_on_user_id"
+ end
+
create_table "users", force: :cascade do |t|
t.string "email"
t.string "password_digest"
diff --git a/db/seeds.rb b/db/seeds.rb
index f2c9c04..1cdfbec 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -15,7 +15,13 @@ books = Book.create([
title: 'Imaginary trip',
price: 3600,
published: true,
- quantity: 0
+ quantity: 1
+ },
+ {
+ title: 'Winnie the Pooh',
+ price: 3700,
+ published: true,
+ quantity: 5
},
])
@@ -32,12 +38,17 @@ authors = Author.create([
first_name: 'Rick',
last_name: 'Pickle'
},
+ {
+ first_name: 'Alan',
+ last_name: 'Milne'
+ },
])
books.first.authors << authors.first
books.second.authors << authors.first
books.third.authors << authors.second
books.third.authors << authors.third
+books.fourth.authors << authors.fourth
User.create([
{
diff --git a/spec/models/audit_record_spec.rb b/spec/models/audit_record_spec.rb
deleted file mode 100644
index 24cf532..0000000
--- a/spec/models/audit_record_spec.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require 'rails_helper'
-
-RSpec.describe AuditRecord, type: :model do
- pending "add some examples to (or delete) #{__FILE__}"
-end