-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
190 lines (174 loc) · 5.27 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
class SQApp < Sinatra::Base
register Sinatra::ConfigFile
config_file './config/config.yml'
use Rack::Auth::Basic,"Protected Area" do |username, password|
@@auth_user = User.find_by(username: username).authenticate(password)
end
before do
content_type :xml
end
#ROUTES
#Load greeting
get '/' do
'<?xml version="1.0" encoding="UTF-8"?>
<xml>
<message>Welcome to user market API!</message>
</xml>'
end
#Get user info
get '/users/:username' do
user = User.select("id, username, level").find_by(username: params[:username])
if user.present?
user.to_xml
else
status 404
end
end
#Get user market
get '/users/:username/items' do
user = User.find_by(username: params[:username])
if !user.present?
status 404
else
lot = Lot.select("id, user_id, item_id, amount, price").where(user_id: user.id)
if !lot.present?
'<?xml version="1.0" encoding="UTF-8"?>
<xml>
<message>User has no items on the market.</message>
</xml>'
else
lot.to_xml
end
end
end
#Get public market
get '/market' do
lot = Lot.filter(params).where(public: true)
if lot.present?
lot.to_xml
else
status 404
end
end
#Buy item
put '/users/:username/items/:itemname' do
user = User.find_by(username: params[:username])
item = Item.find_by(name: params[:itemname])
if user.present? && item.present?
lot = Lot.find_by(user_id: user.id, item_id: item.id)
if lot.present? || @@auth_user.id != lot.user_id
if params.has_key?("amount") || params[:amount].to_i <= 0 || params[:amount].to_i > lot.amount
total_price = params[:amount].to_i * lot.price
if total_price < @@auth_user.balance
@@auth_user.update_column("balance", @@auth_user.balance - total_price)
inventory = Inventory.find_by(user_id: @@auth_user.id, item_id: lot.item_id)
if inventory.present?
inventory.update(amount: inventory.amount + params[:amount].to_i)
else
Inventory.create(user_id: @@auth_user.id, item_id: lot.item_id, amount: params[:amount].to_i)
end
user.update_column("balance", user.balance + total_price)
if params[:amount].to_i == lot.amount
lot.destroy
else
lot.update(amount: lot.amount - params[:amount].to_i)
end
status 204
else
status 402
end
else
status 403
end
else
status 404
end
else
status 404
end
end
#Get your profile info
get '/dashboard' do
@@auth_user.to_xml
end
#Get your items
get '/dashboard/inventory' do
inventory = Inventory.where(user_id: @@auth_user.id)
if inventory.present?
inventory.to_xml
else
'<?xml version="1.0" encoding="UTF-8"?>
<xml>
<message>You have no items on the market.</message>
</xml>'
end
end
#Add lot
post '/dashboard/inventory/:id' do
inventory = Inventory.find_by(id: params[:id].to_i)
if !inventory.present?
status 404
elsif params[:amount].to_i > inventory.amount || params[:amount].to_i < 1 || params[:amount].to_i > 10 || !params.has_key?("price") || !params.has_key?("amount")
status 403
else
lot = Lot.new(user_id: inventory.user_id, item_id: inventory.item_id, amount: params[:amount].to_i, price: params[:price].to_i, public: false)
if lot.invalid?
status 403
else
lot.save
if inventory.amount -= lot.amount != 0
inventory.update_column("amount", inventory.amount - lot.amount)
else
inventory.destroy
staus 201
end
end
end
end
#Get your lots
get '/dashboard/lots' do
lot = Lot.where(user_id: @@auth_user.id)
if lot.present?
lot.to_xml
else
status 404
end
end
#Make lot public
patch '/dashboard/lots/:id' do
if Lot.where(public: true).count < settings.max_market_rec
lot = Lot.find_by(id: params[:id].to_i)
if !lot.present?
status 404
elsif lot.user_id != @@auth_user.id && lot.public = true
status 403
elsif @@auth_user.balance < 100
status 402
else
@@auth_user.update_column("balance", @@auth_user.balance -= 100)
lot.update(public: true)
status 204
end
else
status 403
end
end
#Remove lot from the market
delete '/dashboard/lots/:id' do
lot = Lot.find_by(id: params[:id])
if !lot.present?
status 404
elsif lot.user_id != @@auth_user.id
status 403
else
inventory = Inventory.find_by(user_id: lot.user_id, item_id: lot.item_id)
if !inventory.present?
Inventory.create(user_id: lot.user_id, item_id: lot.item_id, amount: lot.amount)
else
inventory.update_column("amount", inventory.amount + lot.amount)
end
lot.destroy
status 204
end
end
end