Class: Api::V1::User::GroupOrderArticlesController

Inherits:
BaseController
  • Object
show all
Includes:
Concerns::CollectionScope
Defined in:
app/controllers/api/v1/user/group_order_articles_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/api/v1/user/group_order_articles_controller.rb', line 20

def create
  goa = nil
  GroupOrderArticle.transaction do
    oa = order_articles_scope_for_create.find(create_params.require(:order_article_id))
    go = current_ordergroup.group_orders.find_or_create_by!(order_id: oa.order_id)
    goa = go.group_order_articles.create!(order_article: oa)
    goa.update_quantities((create_params[:quantity] || 0).to_i, (create_params[:tolerance] || 0).to_i)
    oa.update_results!
    go.update_price!
    go.update!(updated_by: current_user)
  end
  render_goa_with_oa(goa)
end

#destroyObject



47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/api/v1/user/group_order_articles_controller.rb', line 47

def destroy
  goa = nil
  GroupOrderArticle.transaction do
    goa = scope_for_update.find(params.require(:id))
    goa.destroy!
    goa.order_article.update_results!
    goa.group_order.update_price!
    goa.group_order.update!(updated_by: current_user)
  end
  render_goa_with_oa(nil, goa.order_article)
end

#indexObject

TODO:

allow decreasing amounts when minimum balance isn’t met



11
12
13
# File 'app/controllers/api/v1/user/group_order_articles_controller.rb', line 11

def index
  render_collection search_scope
end

#showObject



15
16
17
18
# File 'app/controllers/api/v1/user/group_order_articles_controller.rb', line 15

def show
  goa = scope.find(params.require(:id))
  render_goa_with_oa(goa)
end

#updateObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/api/v1/user/group_order_articles_controller.rb', line 34

def update
  goa = nil
  GroupOrderArticle.transaction do
    goa = scope_for_update.includes(:group_order_article_quantities).find(params.require(:id))
    goa.update_quantities((update_params[:quantity] || goa.quantity).to_i,
                          (update_params[:tolerance] || goa.tolerance).to_i)
    goa.order_article.update_results!
    goa.group_order.update_price!
    goa.group_order.update!(updated_by: current_user)
  end
  render_goa_with_oa(goa)
end