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
- Post editing
- Post editing & deleting
- User reviews
- Yardoc 50%
----------------
- Remove bids
- Remove posts
- Film
## Refactor
- Funds transfer logic for auctions?

@ -49,7 +49,7 @@ not_found do
serve :"404"
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[:ret] = ret
flash[:error] = msg

@ -33,16 +33,17 @@ LOGIN_ERRORS = {
}
# Auction stuff
AH_BUYOUT_FACTOR = 1.8 # min buyout factor
AH_BIDS_FACTOR = 1.01 # min 1%
AH_MIN_IMAGES = 1 # minimum images
AUCTION_ERRORS = {
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}!",
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!",
imagecount: "You need to submit at least #{AH_MIN_IMAGES} image(s)!",
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[:initprice] unless init_price >= MIN_INIT_PRICE
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, ""
end
@ -371,6 +372,17 @@ class Auction < EntityModel
ah && ah.expired?
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
User.find_by_id @user_id
end
@ -379,9 +391,14 @@ class Auction < EntityModel
Image.get_relation @id
end
def categories
def category_ids
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
def expired?
@ -462,9 +479,12 @@ class Bid < EntityModel
def self.get_delta_amount(ahid, uid, amount)
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)}
max_bid = data.max_by {|bid| bid.amount}
p "sgiodfhgiodfhioghoi"
p data
p "sgiodfhgiodfhioghoi"
return amount - max_bid.amount
else
return amount
@ -475,6 +495,7 @@ class Bid < EntityModel
ah = Auction.find_by_id ahid
return false, "Invalid auction" unless ah.is_a? Auction
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[:bidamount] unless amount >= ah.min_new_bid
return true, ""

@ -58,10 +58,65 @@ end
get "/auctions/:id" do
id = params[:id].to_i
auction_obj = Auction.find_by_id id
auction = Auction.find_by_id id
if !auction_obj.nil? then
serve :"auction/view", {auction: auction_obj}
if !auction.nil? then
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
raise Sinatra::NotFound
end
@ -69,13 +124,13 @@ end
post "/auctions/:id/bids" do
id = params[:id].to_i
auction_obj = Auction.find_by_id id
auction = Auction.find_by_id id
amount = params[:amount].to_f
message = params[:message]
if !auction_obj.nil? then
success, resp = auction_obj.place_bid(session[:userid], amount, message)
if !auction.nil? then
success, resp = auction.place_bid(session[:userid], amount, message)
if success then
flash[:success] = "Placed bid."
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
.form-container
form action="/auctions" method="get"
label Keywords
input type="search" name="title" placeholder="ex: computer, teapot"
label Title
input type="search" name="title" placeholder="ex: 'teapot'"
label Price range
.range-container
@ -22,7 +22,7 @@
label Categories
select multiple="" name="categories[]"
- 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"
a.button href="/auctions" = "Clear Filters"
@ -54,4 +54,4 @@
p
| #{auction.description}
- else
h3.tcenter No results :(
h3.tcenter No results found...

@ -20,6 +20,9 @@
h2
span
| #{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()"
| [Go back]
h3
@ -43,9 +46,6 @@
h3
| Minimum bid factor:
span.gray = "#{((AH_BIDS_FACTOR-1)*100).round(2)}%"
h3
| Buyout factor:
span.gray = "#{((AH_BUYOUT_FACTOR-1)*100).round(2)}%"
h3
| Expires in:
span.red = "#{auction.time_left_s}"
@ -54,7 +54,7 @@
form action="/auctions/#{auction.id}/bids" method="post"
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!"
input type="submit" value="Bid"
input type="submit" value="Bid" disabled=("disabled" if session_user.id == auction.user_id)
- else
h1.red
| Auction Expired

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

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

Loading…
Cancel
Save