-
Notifications
You must be signed in to change notification settings - Fork 56
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
236 simple api rebased #290
base: main
Are you sure you want to change the base?
Conversation
…on code for API using basic auth
…he back-end, Added admin check, Render json for adopted drains
…s. I created this initially to test out how the API would handle load
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jafowler49 I rebased #241 as the Github diff was showing other changes that happened on the production
branch. I left a couple of comments, but this is looking better. Thank you!
@@ -0,0 +1,46 @@ | |||
class AdoptedController < ApplicationController | |||
before_action :authenticate | |||
before_action :get_adopted_things, :make_cur_page, :make_other_pages, only: [:index] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given these filters are only used for the index
action, what do you think of just moving the logic in there? You could still leave the helper functions and call them.
private | ||
|
||
def get_adopted_things | ||
@adopted_things = Thing.where.not(user_id: nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already an adopted
scope on Thing
that you could use.
app/views/adopted/index.csv.erb
Outdated
@@ -0,0 +1,5 @@ | |||
<%- headers = ['Lat', 'Lng', 'City ID'] -%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used anywhere? It didn't appear to be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's not. I removed CSV rendering, and the API allows for JSON only.
Hey @jszwedko Made the changes you requested and one other change - adding seed data to an API Test Case. And also made changes to make Rubocop and Rails tests pass. |
Hey @jszwedko, this pull request can be reviewed again to see if its good for merge. Thanks |
Awesome, thanks for revisiting this @jafowler49 . Apologies that I haven't had a chance to review yet, but aim to do so soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jafowler49 thanks so much for continuing to push this forward! I left a few more comments, but I think this is almost there.
user = User.find_by(email: username) | ||
if user && user.valid_password?(password) | ||
return true if user.admin? | ||
render html: '<div> You must be an admin to access this page </div>' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like we should return a 401 here in addition to the rendered HTML.
@@ -3,9 +3,11 @@ ruby '2.2.3' | |||
|
|||
gem 'airbrake', '~> 7.1' | |||
gem 'devise', '~> 3.0' | |||
gem 'faker', '~> 1.7.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed for all environments? It feels like it is only needed for test
and maybe development
.
# Determine if the user supplied a valid page number, if not they get first page | ||
def make_cur_page | ||
page = params[:page].blank? || params[:page].to_i.zero? ? 1 : params[:page] | ||
@cur_page = @adopted_things.page(page) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This coupling feels a bit weird as it requires the developer to have called adopted_things
first.
I think this can be simplified a bit if def index
was like:
adopted_things = Things.adopted.order(:created_at).page(params[:page])
results = {
next_page: adopted_things.next_page, # I think it's fine if this is null to the caller
prev_page: adopted_things.prev_page,
total_pages: adopted_things.total_pages,
things: format_fields(things)
}
render json: @results
and then the make_cur_pages
, make_other_pages
and the shared state could be removed.
Rebased #241 to get a cleaner diff