Skip to content

Commit 31da19c

Browse files
committed
Added user purchases page and listing index to only show unpurchased items
1 parent 3b36a57 commit 31da19c

20 files changed

+19163
-21
lines changed

app/controllers/listings_controller.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ class ListingsController < ApplicationController
44
before_action :set_listing, only: [:show, :favourite]
55

66
def index()
7-
@listings = Listing.all.order("listings.created_at DESC")
7+
@all_listings = Listing.all.order("id DESC")
8+
@listings = []
9+
10+
# Don't show listings that have been purchased.
11+
@all_listings.each do |listing|
12+
if listing.purchase == nil
13+
@listings.push(listing)
14+
end
15+
end
816
end
917

1018
def new()
@@ -85,7 +93,7 @@ def destroy()
8593
redirect_to listings_path
8694
end
8795

88-
def favourite
96+
def favourite()
8997
type = params[:type]
9098
if type == "favourite"
9199
current_user.favourites << @listing

app/controllers/payments_controller.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ def webhook()
1010
listing_id = payment.metadata.listing_id
1111
user_id = payment.metadata.user_id
1212

13-
p "listing id " + listing_id
14-
p "user id " + user_id
15-
16-
status 200
13+
Purchase.create(
14+
user_id: user_id,
15+
listing_id: listing_id,
16+
stripe_receipt: payment_id,
17+
)
1718
end
1819
end

app/controllers/users_controller.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
class UsersController < ApplicationController
22
before_action :authenticate_user!
3-
before_action :set_user, only: [:show, :favourites]
3+
before_action :set_user, only: [:show, :favourites, :purchases]
44

55
def show()
66
@user = User.find(params[:id])
77
end
88

99
def favourites()
10-
@user = User.find(params[:id])
10+
@favourites = current_user.favourites
11+
end
12+
13+
def purchases()
14+
@purchases = current_user.purchases.order("id DESC")
1115
end
1216

1317
private

app/models/listing.rb

+4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ class Listing < ApplicationRecord
44
belongs_to :category
55
belongs_to :brand
66
has_one_attached :picture
7+
78
has_many :favourite_listings, dependent: :destroy
89
has_many :favourited_by, through: :favourite_listings, source: :user
10+
11+
has_one :purchase
12+
913
validates :title, :price, :condition_id, :brand_id, :category_id, presence: :true
1014
end

app/models/purchase.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Purchase < ApplicationRecord
2+
belongs_to :user
3+
belongs_to :listing
4+
5+
validates :listing, uniqueness: true
6+
end

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ class User < ApplicationRecord
1010
has_many :favourite_listings
1111
has_many :favourites, through: :favourite_listings, source: :listing
1212

13+
has_many :purchases
14+
1315
validates :full_name, presence: true
1416
end

app/views/devise/registrations/edit.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="align-self-center">
33
<% if current_user.avatar.attached? %>
44
<%= image_tag current_user.avatar, class: "rounded" %>
5-
<% else %>
5+
<% else %>
66
<%= image_tag "placeholder-avatar.png", class: "rounded" %>
77
<% end %>
88
</div>

app/views/listings/index.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<%= link_to "Create new", new_listing_path, class: "btn btn-primary" %>
33
</div>
44

5-
<div class="d-flex flex-wrap justify-content-around mt-2">
5+
<div class="d-flex flex-wrap mt-2">
66
<% @listings.each do |listing| %>
7-
<div class= "card m-3", style= "width 15rem">
7+
<div class= "card m-3", style= "width: 300px">
88
<% if listing.picture.attached? %>
99
<%= image_tag listing.picture, class: "card-img-top", style: "width: 250px; height: 250px;" %>
1010
<% else %>

app/views/listings/show.html.erb

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div class="d-flex justify-content-end">
22
<% if current_user.id == @listing.user_id %>
3+
<% # Check current user owns the listing then show Edit/Remove functions %>
34
<div>
45
<%= link_to "Edit", edit_listing_path(@listing), class: "btn btn-primary mr-2" %>
56
<%= link_to "Remove", listing_path(@listing.id), data: { toggle: "modal", target: "#RemoveListingModal" }, class: "btn btn-danger" %>
@@ -29,12 +30,12 @@
2930
</div>
3031
</div>
3132
<% else %>
32-
<% # Check to see if the user has already favourited the listing %>
33-
<% if current_user.favourites.exists?(@listing.id) %>
34-
<%= link_to image_tag("favourited.svg"), favourite_listing_path(@listing, type: "unfavourite", id: @listing.id), data: { toggle: "tooltip", placement: "bottom", title: "Favourited" }, method: :put %>
35-
<% else %>
36-
<%= link_to image_tag("favourite.svg"), favourite_listing_path(@listing, type: "favourite", id: @listing.id), data: { toggle: "tooltip", placement: "bottom", title: "Add to Favourites" }, method: :put %>
37-
<% end %>
33+
<% # Check if the user has already favourited the listing %>
34+
<% if current_user.favourites.exists?(@listing.id) %>
35+
<%= link_to image_tag("favourited.svg"), favourite_listing_path(@listing, type: "unfavourite", id: @listing.id), data: { toggle: "tooltip", placement: "bottom", title: "Favourited" }, method: :put %>
36+
<% else %>
37+
<%= link_to image_tag("favourite.svg"), favourite_listing_path(@listing, type: "favourite", id: @listing.id), data: { toggle: "tooltip", placement: "bottom", title: "Add to Favourites" }, method: :put %>
38+
<% end %>
3839
<% end %>
3940
</div>
4041
@@ -64,11 +65,17 @@
6465
<h5>Description:</h5>
6566
<%= @listing.description %>
6667
</div>
67-
<% if current_user.id != @listing.user_id %>
68+
<% if current_user.id != @listing.user_id && @listing.purchase == nil %>
6869
<div class="mb-2">
6970
<button data-stripe="payment" class="btn btn-primary">Purchase</button>
7071
</div>
7172
<% end %>
73+
<% if @listing.purchase != nil %>
74+
<div class="mb-2">
75+
<button class="btn btn-danger disabled">SOLD</button><br>
76+
<small class="text-muted"><%= @listing.purchase.created_at.strftime("%Y/%m/%d %H:%M") %></small>
77+
</div>
78+
<% end %>
7279
</div>
7380
</div>
7481

app/views/shared/_navigation_bar.html.erb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</a>
1818
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
1919
<%= link_to "Favourites", user_favourites_path(current_user.id), class: "dropdown-item" %>
20+
<%= link_to "Purchases", user_purchases_path(current_user.id), class: "dropdown-item" %>
2021
<%= link_to "Edit Account", edit_user_registration_path, class: "dropdown-item" %>
2122
<div class="dropdown-divider"></div>
2223
<%= link_to "Log off", destroy_user_session_path, method: :delete, class: "dropdown-item" %>

app/views/users/favourites.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
<% @user.favourites.each do |favourite| %>
1+
<h2>Favourites</h2>
2+
<% @favourites.each do |favourite| %>
23
<li><%= link_to favourite.title, listing_path(favourite.id) %></li>
34
<% end %>

app/views/users/listings.html.erb

Whitespace-only changes.

app/views/users/purchases.html.erb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<div class="container">
2+
<h2>Purchases</h2>
3+
</div>
4+
<% if @purchases == nil %>
5+
<div class="container">
6+
<div class="row">
7+
<div class="col font-weight-bold">Date</div>
8+
<div class="col font-weight-bold">Item</div>
9+
<div class="col font-weight-bold">Receipt</div>
10+
</div>
11+
<% @purchases.each do |purchase| %>
12+
<div class="row">
13+
<div class="col">
14+
<%= purchase.created_at.strftime("%Y/%m/%d %H:%M") %>
15+
</div>
16+
<div class="col">
17+
<%= link_to purchase.listing.title, listing_path(purchase.listing_id) %>
18+
</div>
19+
<div class="col">
20+
<%= purchase.stripe_receipt %>
21+
</div>
22+
</div>
23+
<% end %>
24+
<% else %>
25+
<div class="container">
26+
<%= "You haven't made any purchases yet." %>
27+
</div>
28+
<% end %>

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
get "/users/:id", to: "users#show", as: "user_profile"
1919
get "/users/:id/favourites", to: "users#favourites", as: "user_favourites"
20+
get "/users/:id/purchases", to: "users#purchases", as: "user_purchases"
2021

2122
get "/payments/success", to: "payments#success"
2223
post "/payments/webhook", to: "payments#webhook"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreatePurchases < ActiveRecord::Migration[5.2]
2+
def change
3+
create_table :purchases do |t|
4+
t.references :user, foreign_key: true
5+
t.references :listing, foreign_key: true
6+
t.string :stripe_receipt
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_05_14_010507) do
13+
ActiveRecord::Schema.define(version: 2020_05_15_070610) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -80,6 +80,16 @@
8080
t.index ["user_id"], name: "index_listings_on_user_id"
8181
end
8282

83+
create_table "purchases", force: :cascade do |t|
84+
t.bigint "user_id"
85+
t.bigint "listing_id"
86+
t.string "stripe_receipt"
87+
t.datetime "created_at", null: false
88+
t.datetime "updated_at", null: false
89+
t.index ["listing_id"], name: "index_purchases_on_listing_id"
90+
t.index ["user_id"], name: "index_purchases_on_user_id"
91+
end
92+
8393
create_table "users", force: :cascade do |t|
8494
t.string "email", default: "", null: false
8595
t.string "encrypted_password", default: "", null: false
@@ -102,4 +112,6 @@
102112
add_foreign_key "listings", "categories"
103113
add_foreign_key "listings", "conditions"
104114
add_foreign_key "listings", "users"
115+
add_foreign_key "purchases", "listings"
116+
add_foreign_key "purchases", "users"
105117
end

0 commit comments

Comments
 (0)