Skip to content

Instantly share code, notes, and snippets.

@epintozzi
Created February 18, 2022 16:07
Show Gist options
  • Save epintozzi/5bba373025100045758c29093fb88669 to your computer and use it in GitHub Desktop.
Save epintozzi/5bba373025100045758c29093fb88669 to your computer and use it in GitHub Desktop.

1. Still a little unsure about how the JSON should be rendered for total unshipped revenue

I’m pretty sure we could pass the results of this AR query to an InvoiceSerializer:

class Api::V1::RevenueController < ApplicationController
  def show
    render json: InvoiceSerializer.new(Invoice.unshipped_revenue(quantity = 10))
  end
end

class InvoiceSerializer
  include JSONAPI::Serializer
  set_type :invoice
  set_id :id
  attributes :unshipped_revenue
end

2. Very interested to see the logic you’d recommend for handling empty query/quantity strings in params.

I would probably do something like this for quantity:

def most_items
  if params[:quantity].nil? #postman is looking for a 400 status here, but this is a better user experience. Either is fine as long as it is handled in some way.
    <happy path code with default limit of 5>
  elsif params[:quantity].is_a?(Integer) && params[:quantity].positive?
    <happy path code with limit of params[:quantity]>
  else
    render json: { data: [], error: 'error' }, status: 400
  end
end

More generally, I’ve found this breakdown of nil?/empty?/blank?/present? really helpful in checking param values.

3. I used the custom attribute feature in my serializer to return the count. Is that the proper way to do it?

Yes!

4. I'm having trouble testing the query we went over in class. It would be nice to see an example for the request spec for it maybe.

Does anyone have a spec they wrote for the query we went over in class or any of the other business logic they'd like to share? Please add it in the comments! I will add one later as well.

5. Also got really stuck on: Total revenue generated in the whole system over a start/end date range. An example of that one talked through could also be very helpful

Video walkthrough

Final Note: It came up a few times that there was a lot of confusion resulting from slightly different relationship with Little Esty shop, and that will be acknowledged and addressed for future mods to reduce the amount of time debugging setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment