Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recent beverages endpoint #205

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ To provide a consistent experience on every system, docker and docker-compose is
1. Install [asdf](http://asdf-vm.com/guide/getting-started.html#getting-started)
2. Install dependencies: `asdf install`
3. Install gems: `bundle install`
4. Migrate the db using `bundle exec rails db:migrate`
5. Seed the db using `bundle exec rails db:seed`
6. Start Tap by running `bundle exec rails s`
4. Run: `yarn`
5. Migrate the db using `bundle exec rails db:migrate`
6. Seed the db using `bundle exec rails db:seed`
7. Start Tap by running `bundle exec rails s`

## Production

Expand Down
26 changes: 26 additions & 0 deletions app/controllers/public_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class PublicController < ApplicationController
include ApplicationHelper
include OrderSessionHelper

skip_before_action :authenticate_user_from_token!, only: :recent
skip_before_action :authenticate_user!, only: :recent

def recent
orders = Order.recent
mapped_orders = orders.joins(:order_items => :product).map do |order|
order.order_items.map do |order_item|
product = order_item.product
{
order_id: order.id,
order_created_at: order.created_at,
product_name: product.name,
product_category: product.category
}
end
end.flatten

render json: { orders: mapped_orders }
end
end
2 changes: 2 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Order < ApplicationRecord
scope :pending, -> { where(created_at: Rails.application.config.call_api_after.ago..) }
scope :final, -> { where(created_at: ..Rails.application.config.call_api_after.ago) }

scope :recent, -> { where(created_at: 2.weeks.ago..Time.zone.now) }

def to_sentence
order_items.map do |oi|
pluralize(oi.count, oi.product.name)
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
# Koelkast overview page
get "overview" => "orders#overview", as: "orders"

get "recent" => "public#recent", as: "recent"

# Guest endpoints
namespace :guest do
resources :orders, only: [:new, :create]
Expand Down
Loading