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

512 - Implementing a phones API #580

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions app/controllers/phones_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
# frozen_string_literal: true

class PhonesController < ApplicationController
def index
category_id = params.require :category_id

relation = get_all_resources(category_id)
render json: PhonesPresenter.present(relation)
end

def show
resource = Phone.find([params[:id]])
render json: PhonesPresenter.present(resource)
end

def get_all_resources(category_id)
relation = if category_id == "all"
phones.where(status: Phone.statuses[:approved])
else
# TODO: This can be simplified once we remove categories from resources
phones
.joins(:addresses)
.where(categories_join_string, category_id, category_id)
.where(status: Phone.statuses[:approved])
.order(sort_order)
end
relation
end

def destroy
phone = Phone.find params[:id]
phone.delete
end

def phones
# Note: We *must* use #preload instead of #includes to force Rails to make a
# separate query per table. Otherwise, it creates one large query with many
# joins, which amplifies the amount of data being sent between Rails and the
# DB by several orders of magnitude due to duplication of tuples.
Phone.preload(:phones [:categories])
end

def categories_join_string
<<~'SQL'
phones.id IN (
(
SELECT resources.id
FROM resources
INNER JOIN categories_resources ON resources.id = categories_resources.resource_id
WHERE categories_resources.category_id = ?
) UNION (
SELECT resources.id
FROM resources
INNER JOIN services ON resources.id = services.resource_id
INNER JOIN categories_services ON services.id = categories_services.service_id
WHERE categories_services.category_id = ?
)
)
SQL
end
end
34 changes: 34 additions & 0 deletions swagger/v1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,40 @@
"application/json"
]
}
},
"/phones/{id}": {
"get": {
"responses": {
"200": {
"examples": {
"application/json": {
"id": "string",
"number": "string",
"extension": "string",
"service_type": "string",
"country_code": "string"
}
},
"description": "Phone Response"
}
},
"parameters": [
{
"name": "id",
"in": "path",
"type": "integer",
"description": "Phone ID",
"required": true
}
],
"tags": [
"phones"
],
"summary": "Retrieves phone numbers by id",
"produces" : [
"application/json"
]
}
}
}
}