Posts edit & deleting + refactor and bug fixes

master
E. Almqvist 3 years ago
parent 3751b72c7d
commit 3035a3ea3d
  1. 6
      src/TODO.md
  2. 2
      src/app.rb
  3. 5
      src/config.rb
  4. 27
      src/lib/db_models.rb
  5. 67
      src/routes/auction.rb
  6. 13
      src/views/auction/edit.slim
  7. 8
      src/views/auction/index.slim
  8. 8
      src/views/auction/view.slim
  9. 4
      src/views/index.slim
  10. 21
      src/views/stylesheets/style.sass

@ -1,10 +1,8 @@
# TODO # TODO
- Post editing - Post editing & deleting
- User reviews - User reviews
- Yardoc 50% - Yardoc 50%
---------------- - Film
- Remove bids
- Remove posts
## Refactor ## Refactor
- Funds transfer logic for auctions? - Funds transfer logic for auctions?

@ -49,7 +49,7 @@ not_found do
serve :"404" serve :"404"
end end
def auth_denied(msg=AUTH_ERRORS[:denied], status=403, ret="/") def auth_denied(msg=AUTH_ERRORS[:denied], status=403, ret=back)
session[:status] = status session[:status] = status
session[:ret] = ret session[:ret] = ret
flash[:error] = msg flash[:error] = msg

@ -33,16 +33,17 @@ LOGIN_ERRORS = {
} }
# Auction stuff # Auction stuff
AH_BUYOUT_FACTOR = 1.8 # min buyout factor
AH_BIDS_FACTOR = 1.01 # min 1% AH_BIDS_FACTOR = 1.01 # min 1%
AH_MIN_IMAGES = 1 # minimum images AH_MIN_IMAGES = 1 # minimum images
AUCTION_ERRORS = { AUCTION_ERRORS = {
titlelen: "Title length must be between #{MIN_TITLE_LEN} and #{MAX_TITLE_LEN} characters!", titlelen: "Title length must be between #{MIN_TITLE_LEN} and #{MAX_TITLE_LEN} characters!",
desclen: "Description length must be between #{MIN_DESC_LEN} and #{MAX_DESC_LEN} characters!",
initprice: "The initial price must be at least #{MIN_INIT_PRICE}!", initprice: "The initial price must be at least #{MIN_INIT_PRICE}!",
deltatime: "Time span is too short! Must be at least one day!", deltatime: "Time span is too short! Must be at least one day!",
bidamount: "Bid amount must be at least #{((AH_BIDS_FACTOR-1)*100).round(2)}% greater than the highest bid!", bidamount: "Bid amount must be at least #{((AH_BIDS_FACTOR-1)*100).round(2)}% greater than the highest bid!",
imagecount: "You need to submit at least #{AH_MIN_IMAGES} image(s)!", imagecount: "You need to submit at least #{AH_MIN_IMAGES} image(s)!",
expired: "Auction has expired!", expired: "Auction has expired!",
cantafford: "You can not afford to bid that much!" cantafford: "You can not afford to bid that much!",
ownerbid: "You can not bid on your own auction!"
} }

@ -299,6 +299,7 @@ class Auction < EntityModel
return false, AUCTION_ERRORS[:titlelen] unless title.length.between?(MIN_TITLE_LEN, MAX_TITLE_LEN) return false, AUCTION_ERRORS[:titlelen] unless title.length.between?(MIN_TITLE_LEN, MAX_TITLE_LEN)
return false, AUCTION_ERRORS[:initprice] unless init_price >= MIN_INIT_PRICE return false, AUCTION_ERRORS[:initprice] unless init_price >= MIN_INIT_PRICE
return false, AUCTION_ERRORS[:deltatime] unless delta_time >= MIN_DELTA_TIME return false, AUCTION_ERRORS[:deltatime] unless delta_time >= MIN_DELTA_TIME
return false, AUCTION_ERRORS[:desclen] unless description.length.between?(MIN_DESC_LEN, MAX_DESC_LEN)
return true, "" return true, ""
end end
@ -371,6 +372,17 @@ class Auction < EntityModel
ah && ah.expired? ah && ah.expired?
end end
def edit(title, description)
return false, AUCTION_ERRORS[:titlelen] unless title.length.between?(MIN_TITLE_LEN, MAX_TITLE_LEN)
return false, AUCTION_ERRORS[:desclen] unless description.length.between?(MIN_DESC_LEN, MAX_DESC_LEN)
data = {
title: title,
description: description
}
Auction.update data, "id = ?", @id
end
def poster def poster
User.find_by_id @user_id User.find_by_id @user_id
end end
@ -379,9 +391,14 @@ class Auction < EntityModel
Image.get_relation @id Image.get_relation @id
end end
def categories def category_ids
data = Auction_Category_relation.get "category_id", "auction_id = ?", @id data = Auction_Category_relation.get "category_id", "auction_id = ?", @id
data && data.map! { |category| Category.find_by_id category["category_id"]} data && data.map! {|category| category["category_id"].to_i}
end
def categories
data = self.category_ids
data && data.map! { |catid| Category.find_by_id catid}
end end
def expired? def expired?
@ -462,9 +479,12 @@ class Bid < EntityModel
def self.get_delta_amount(ahid, uid, amount) def self.get_delta_amount(ahid, uid, amount)
data = self.get "*", "auction_id = ? AND user_id = ?", ahid, uid data = self.get "*", "auction_id = ? AND user_id = ?", ahid, uid
if data then if data and data.length > 0 then
data.map! {|dat| self.new(dat)} data.map! {|dat| self.new(dat)}
max_bid = data.max_by {|bid| bid.amount} max_bid = data.max_by {|bid| bid.amount}
p "sgiodfhgiodfhioghoi"
p data
p "sgiodfhgiodfhioghoi"
return amount - max_bid.amount return amount - max_bid.amount
else else
return amount return amount
@ -475,6 +495,7 @@ class Bid < EntityModel
ah = Auction.find_by_id ahid ah = Auction.find_by_id ahid
return false, "Invalid auction" unless ah.is_a? Auction return false, "Invalid auction" unless ah.is_a? Auction
return false, AUCTION_ERRORS[:expired] unless not ah.expired? return false, AUCTION_ERRORS[:expired] unless not ah.expired?
return false, AUCTION_ERRORS[:ownerbid] unless uid != ah.user_id
return false, AUCTION_ERRORS[:cantafford] unless User.find_by_id(uid).balance - amount >= 0 return false, AUCTION_ERRORS[:cantafford] unless User.find_by_id(uid).balance - amount >= 0
return false, AUCTION_ERRORS[:bidamount] unless amount >= ah.min_new_bid return false, AUCTION_ERRORS[:bidamount] unless amount >= ah.min_new_bid
return true, "" return true, ""

@ -58,10 +58,65 @@ end
get "/auctions/:id" do get "/auctions/:id" do
id = params[:id].to_i id = params[:id].to_i
auction_obj = Auction.find_by_id id auction = Auction.find_by_id id
if !auction_obj.nil? then if !auction.nil? then
serve :"auction/view", {auction: auction_obj} serve :"auction/view", {auction: auction}
else
raise Sinatra::NotFound
end
end
get "/auctions/:id/edit" do
id = params[:id].to_i
auction = Auction.find_by_id id
if !auction.nil? then
auth_denied "You can not edit expired auctions!" if auction.expired?
auth_denied unless auction.user_id == session[:userid] or get_current_user.admin?
flash[:success] = "Updated post."
serve :"auction/edit", {auction: auction}
else
raise Sinatra::NotFound
end
end
get "/auctions/:id/delete" do
id = params[:id].to_i
auction = Auction.find_by_id id
if !auction.nil? then
auth_denied "You can not delete expired auctions!" if auction.expired?
auth_denied unless auction.user_id == session[:userid] or get_current_user.admin?
# Delete everything related in the db
Auction.delete "id = ?", id
Auction_Category_relation.delete "auction_id = ?", id
Bid.delete "auction_id = ?", id
flash[:success] = "Removed post."
redirect "/auctions"
else
raise Sinatra::NotFound
end
end
post "/auctions/:id/update" do
id = params[:id].to_i
auction = Auction.find_by_id id
if !auction.nil? then
auth_denied "You can not edit expired auctions!" if auction.expired?
auth_denied unless auction.user_id == session[:userid] or get_current_user.admin?
new_title = params[:title].strip
new_desc = params[:description].strip
auction.edit new_title, new_desc
redirect "/auctions/#{id}"
else else
raise Sinatra::NotFound raise Sinatra::NotFound
end end
@ -69,13 +124,13 @@ end
post "/auctions/:id/bids" do post "/auctions/:id/bids" do
id = params[:id].to_i id = params[:id].to_i
auction_obj = Auction.find_by_id id auction = Auction.find_by_id id
amount = params[:amount].to_f amount = params[:amount].to_f
message = params[:message] message = params[:message]
if !auction_obj.nil? then if !auction.nil? then
success, resp = auction_obj.place_bid(session[:userid], amount, message) success, resp = auction.place_bid(session[:userid], amount, message)
if success then if success then
flash[:success] = "Placed bid." flash[:success] = "Placed bid."
else else

@ -0,0 +1,13 @@
.content-container
h1 Edit Auction "#{auction.title}"
.form-container
form#auction_new action="/auctions/#{auction.id}/update" method="post"
label Title
input type="text" name="title" placeholder="Title" pattern="#{TITLE_REGEX_STR}" value="#{auction.title}"
label Description
textarea name="description" cols="20" rows="5" title="Content length must be between #{MIN_DESC_LEN} and #{MAX_DESC_LEN} characters" pattern="#{DESC_REGEX_STR}" maxlength="#{MAX_DESC_LEN}" placeholder="Tell us about what you're selling!" = "#{auction.description}"
input type="submit" value="Update"
a.inlbutton.red href="/auctions/#{auction.id}/delete" [DELETE]

@ -4,8 +4,8 @@
h2 Search Filters h2 Search Filters
.form-container .form-container
form action="/auctions" method="get" form action="/auctions" method="get"
label Keywords label Title
input type="search" name="title" placeholder="ex: computer, teapot" input type="search" name="title" placeholder="ex: 'teapot'"
label Price range label Price range
.range-container .range-container
@ -22,7 +22,7 @@
label Categories label Categories
select multiple="" name="categories[]" select multiple="" name="categories[]"
- Category.get_all.each do |category| - Category.get_all.each do |category|
option value="#{category.id}" selected=("selected" if params[:categories] and params[:categories].include?(category.id.to_s)) style="color: #{category.color};" #{category.name} option value="#{category.id}" selected=("selected" if params[:categories] and params[:categories].include?(category.id.to_s)) style="color: #{category.color};" = "#{category.name}"
input type="submit" value="Search" input type="submit" value="Search"
a.button href="/auctions" = "Clear Filters" a.button href="/auctions" = "Clear Filters"
@ -54,4 +54,4 @@
p p
| #{auction.description} | #{auction.description}
- else - else
h3.tcenter No results :( h3.tcenter No results found...

@ -20,6 +20,9 @@
h2 h2
span span
| #{auction.title} | #{auction.title}
- if auction.user_id == session_user.id or session_user.admin?
a.inlbutton href="/auctions/#{auction.id}/edit"
| [Edit]
a.inlbutton href="javascript:history.back()" a.inlbutton href="javascript:history.back()"
| [Go back] | [Go back]
h3 h3
@ -43,9 +46,6 @@
h3 h3
| Minimum bid factor: | Minimum bid factor:
span.gray = "#{((AH_BIDS_FACTOR-1)*100).round(2)}%" span.gray = "#{((AH_BIDS_FACTOR-1)*100).round(2)}%"
h3
| Buyout factor:
span.gray = "#{((AH_BUYOUT_FACTOR-1)*100).round(2)}%"
h3 h3
| Expires in: | Expires in:
span.red = "#{auction.time_left_s}" span.red = "#{auction.time_left_s}"
@ -54,7 +54,7 @@
form action="/auctions/#{auction.id}/bids" method="post" form action="/auctions/#{auction.id}/bids" method="post"
input type="number" name="amount" placeholder="#{auction.min_new_bid}" pattern="[0-9]+" input type="number" name="amount" placeholder="#{auction.min_new_bid}" pattern="[0-9]+"
textarea name="message" cols="20" rows="2" title="Content length must be between #{MIN_MSG_LEN} and #{MAX_MSG_LEN} characters" pattern="#{MSG_REGEX_STR}" maxlength="#{MAX_MSG_LEN}" placeholder="Write a message for your bid!" textarea name="message" cols="20" rows="2" title="Content length must be between #{MIN_MSG_LEN} and #{MAX_MSG_LEN} characters" pattern="#{MSG_REGEX_STR}" maxlength="#{MAX_MSG_LEN}" placeholder="Write a message for your bid!"
input type="submit" value="Bid" input type="submit" value="Bid" disabled=("disabled" if session_user.id == auction.user_id)
- else - else
h1.red h1.red
| Auction Expired | Auction Expired

@ -9,14 +9,14 @@
li li
a.button href="/auctions/new" = "Post Auction" a.button href="/auctions/new" = "Post Auction"
- if Auction.get_all.length > 0 - if Auction.search(nil, nil, nil, nil, false).length > 0
article article
h1.tcenter.title h1.tcenter.title
| Recent posts | Recent posts
article.post-container.card#posts article.post-container.card#posts
ul.list-container ul.list-container
- Auction.get_all.reverse[0..1].each do |auction| - Auction.search(nil, nil, nil, nil, false).reverse[0..1].each do |auction|
li li
a href="/auctions/#{auction.id}" a href="/auctions/#{auction.id}"
div style="background-image: url(#{auction.images[0].url})" div style="background-image: url(#{auction.images[0].url})"

@ -157,7 +157,7 @@ a.button:hover
.flash .flash
position: fixed position: fixed
text-align: center text-align: center
top: 4rem top: 5rem
left: 50% left: 50%
transform: translateX(-50%) transform: translateX(-50%)
font-size: 1rem font-size: 1rem
@ -185,7 +185,7 @@ a.button:hover
.flash:hover .flash:hover
cursor: pointer cursor: pointer
opacity: .5 opacity: .76
img.avatar, img.avatar_big img.avatar, img.avatar_big
background: $bg_clr background: $bg_clr
@ -310,6 +310,13 @@ ul.list-container
font-family: $font_stack font-family: $font_stack
font-size: 1.2rem font-size: 1.2rem
padding: .2rem 1rem padding: .2rem 1rem
input:disabled
color: $shadow_clr
background: $bg_alt_clr
input:disabled:hover
cursor: not-allowed !important
input[type=file] input[type=file]
padding: 0 padding: 0
@ -319,7 +326,7 @@ ul.list-container
background: $bg_clr background: $bg_clr
transition: color .2s transition: color .2s
input[type=submit]:hover input[type=submit]:hover:not(:disabled)
opacity: .8 opacity: .8
color: $special_clr color: $special_clr
cursor: pointer cursor: pointer
@ -543,7 +550,7 @@ article.post-container
max-width: 70rem max-width: 70rem
grid-template-columns: 1fr 1fr grid-template-columns: 1fr 1fr
grid-template-rows: 1fr grid-template-rows: 1fr
grid-template-areas: "im in" "bi bi" grid-template-areas: "im in" "bi in"
#auctionbid-container #auctionbid-container
grid-area: bi grid-area: bi
@ -563,7 +570,13 @@ article.post-container
#auctioninfo #auctioninfo
grid-area: in grid-area: in
padding-left: 1rem
#auctionbid
align-items: unset
form
padding: 0
div div
margin-top: 1.2rem margin-top: 1.2rem

Loading…
Cancel
Save