Conceptual architectural change for CLI user app

This commit is contained in:
2015-09-07 19:10:54 +02:00
parent 4ef7ecba41
commit 1caa488524
15 changed files with 163 additions and 140 deletions
@@ -34,5 +34,14 @@ module Users
obj[:ok] = true unless obj.has_key?(:ok)
obj
end
def new_order_product_orders
case params[:product_orders]
when String then JSON.parse(params[:product_orders]) rescue []
when Hash then params[:product_orders].values
else
[]
end
end
end
end
+47
View File
@@ -29,5 +29,52 @@ module Users
render json: {}, status: :not_found and return unless @list.present? && Array.wrap(@list.user_ids).include?(current_user.id)
render json: JSONAPI::Serializer.serialize(@list, serializer: Users::ListSerializer, include: %w[supplier users])
end
# POST /user/list_needs_payment.json
def needs_payment
@list = active_list
render json: json_alert('messages.no_active_list', list_active: false) and return unless @list.try(:id).to_s == params[:id]
@list.needs_payment!
render json: JSONAPI::Serializer.serialize(@list, serializer: Users::ListSerializer)
end
# POST /user/lists/:id/move_table.json?table_id=....
# used to move the table
# TODO wrap logic of actions
# - table_info
# - move_table
# into separate class and implement security in a non stupid way as it is now
def move_to_table
render json: json_alert('messages.no_active_list', list_active: false) and return unless active_list.present?
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
@table = Table.find(params[:table_id])
if @table.occupied?
render json: {occupied: true}
elsif @table.reserved?
render json: {reserved: true}
elsif active_list.supplier_id != @table.supplier_id
res[:other_supplier] = true if active_list.supplier_id != @table.supplier_id
res[:current_table_id] = active_list.table_id
else
active_list.move_to_table! @table
render json: {occupied: false, reserved: false}
end
end
# Used by the user Ember app
# please also look and mirror this action in the tables controller
# POST /user/lists/:id/order_products
def order_products
res = {}
res[:supplier_closed] = active_list.supplier.closed?
unless res[:supplier_closed]
# Create new list
active_list.place_order product_orders: new_order_product_orders, user: current_user, first_order: false
# do not add payload, since relevant data is added through message bus (Faye)
#res[:payload] = JSONAPI::Serializer.serialize(order, serializer: Users::OrderSerializer, include: %w[product_orders])
end
render json: res
end
end
end
@@ -11,31 +11,5 @@ module Users
render json: JSONAPI::Serializer.serialize(orders, serializer: Users::OrderSerializer, include: %w[list product_orders product_orders.order], is_collection: true)
end
# Used by the user Ember app
# POST /user/orders
def create
# render json: {}, status: :unprocessable_entity and return unless params[:order].present? && params[:order][:product_orders].present?
if list = current_user.active_list
render json: {}, status: :not_acceptable and return unless list.supplier.open?
else
#TODO: More logic about creating a new list!!!!!, usercontroller table_info should become irrelevant
#NOTE: security bug here!!!!!!
# - supplier.open?
# - etc....
render json: {}, status: :unprocessable_entity and return unless params[:table_id].present?
table = Table.find(params[:table_id])
render json: {}, status: :not_acceptable and return unless table.supplier.open?
if table.occupied?
#render json: json_alert('messages.table_is_occupied', location: :join_occupied_table, location_params: {table_id: @table.id})
render json: {}, status: :not_acceptable and return
end
list = List.from_table( table, current_user )
end
order = list.place_order product_orders: params[:product_orders], user: current_user
render json: order, serializer: Users::OrderSerializer
#render nothing: true
end
end
end
@@ -1,5 +1,6 @@
module Users
class TablesController < Users::ApplicationController
respond_to :json
def show
@table = Table.find(params[:id])
render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
@@ -11,5 +12,65 @@ module Users
supplier.product_categories.include_relations(:products)
render json: JSONAPI::Serializer.serialize(supplier, serializer: Users::SupplierSerializer, include: %w[product_categories product_categories.products product_categories.supplier])
end
# POST /tables/:id/needs_help.json
def needs_help
#@table = Table.find(params[:id])
render json: json_alert('messages.no_active_list', list_active: false) and return unless active_list.present?
active_list.needs_help!
#render json: JSONAPI::Serializer.serialize(@table, serializer: Users::TableSerializer)
render json: {}
end
# POST /user/tables/:id/join
def join
render json: json_alert('messages.table_not_found') and return unless params[:id].present?
@table = Table.find(params[:id])
if @list = @table.active_list
@list.send_table_join_request_for_user! current_user
end
head :ok
end
# GET /user/table_info.json
# used for moving table request
# TODO wrap logic of actions
# - table_info
# - move_table
# into separate class and implement security in a non stupid way as it is now
def status_info
render json: json_alert('messages.table_not_found') and return unless params[:table_id].present?
table = Table.find(params[:table_id])
res = {}
res[:occupied] = table.occupied?
res[:reserved] = table.reserved?
res[:supplier_closed] = table.supplier.closed?
res[:table] = table.attributes
if active_list.present?
res[:other_supplier] = true if active_list.supplier_id != table.supplier_id
res[:current_table_id] = active_list.table_id
res[:current_list_id] = active_list.id
end
render json: res
end
# Used by the user Ember app
# POST /user/tables/:id/order_products
def order_products
table = Table.find(params[:id])
res = {}
res[:occupied] = table.occupied?
res[:reserved] = table.reserved?
res[:supplier_closed] = table.supplier.closed?
res[:no_product_orders] = true unless product_orders = new_order_product_orders.presence
unless res[:occupied] or res[:supplier_closed] or res[:no_product_orders]
# Create new list
list = List.from_table( table, current_user )
res[:active_list_id] = list.id # used to set the active list in the app
order = list.place_order product_orders: product_orders, user: current_user, first_order: true
res[:payload] = JSONAPI::Serializer.serialize(order, serializer: Users::OrderSerializer, include: %w[list product_orders])
end
render json: res
end
end
end