Skip to content

Commit

Permalink
Mirko Test API
Browse files Browse the repository at this point in the history
  • Loading branch information
mjavurek authored and paroga committed Sep 20, 2022
1 parent 8a62637 commit 46d2aae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
28 changes: 28 additions & 0 deletions app/controllers/api/v1/mirko_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Api::V1::MirkoController < Api::V1::BaseController
before_action -> { doorkeeper_authorize! 'orders:read' }

def index
time_now = Time.now
gos = GroupOrder
.includes(group_order_articles: { order_article: :article },
order: :supplier)
.references(:orders)
.where(ordergroup_id: current_user.ordergroup.id)
.limit(200)
.where(orders: {
ends: (time_now.weeks_ago(1)..time_now.next_year), state: "open", pickup: nil
})
.or(
GroupOrder
.includes(group_order_articles: { order_article: :article },
order: :supplier)
.references(:orders)
.where(ordergroup_id: current_user.ordergroup.id)
.limit(200)
.where(orders: {
pickup: ((time_now.weeks_ago(5)..(time_now.weeks_ago(-1)))), state: %w[finished received closed]
})
)
render json: gos, each_serializer: MirkoSerializer
end
end
36 changes: 36 additions & 0 deletions app/serializers/mirko_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class MirkoSerializer < ActiveModel::Serializer
attributes :id
# has_many :group_order_articles, serializer: MirkoSubSerializer
attributes :enddate
attributes :pickupdate
attributes :order_state
attributes :suppliername
attributes :article_details
attributes :transport

def article_details
object.group_order_articles.map do |goa|
{ id: goa.id,
name: goa.order_article.article.name,
unit: goa.order_article.article.unit,
price: goa.order_article.article.price,
ordered: goa.quantity, received: goa.result }
end
end

def suppliername
object.order.name
end

def enddate
object.order.ends
end

def pickupdate
object.order.pickup
end

def order_state
object.order.state
end
end
2 changes: 1 addition & 1 deletion app/serializers/user_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email, :locale
attributes :id, :name, :email, :locale, :ordergroup_name
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
resources :order_articles, only: [:index, :show]
resources :group_order_articles
resources :article_categories, only: [:index, :show]
resources :mirko
end
end

Expand Down

0 comments on commit 46d2aae

Please sign in to comment.