Skip to content

Commit

Permalink
[AFA-5] Create API with Restful structure (#6)
Browse files Browse the repository at this point in the history
* Create API with Restful structure

* Fix lint issues

* Update pull request template

* Upgrade core version
  • Loading branch information
ricardopacheco committed Apr 30, 2024
1 parent 82a66a9 commit 678b3ed
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 44 deletions.
32 changes: 32 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
> [!CAUTION]
> Topics with :red_circle: are required. The rest are optional and can be removed.
Issue number: resolves #

## Summary :red_circle:

## Proposed / Possible solution :red_circle:

## how to test :policeman:

Describe in details (preferred step by step).

## Risks / Impacts :red_circle:

Inform the risks/impacts that this PR is generating

### Examples

- Unavailability in API x/y/z
- Downtime of x seconds in y environment
- None anticipated

## Requirements for deployment :red_circle:

Provide the necessary requirements that are needed pre or post deployment.

### Examples

- Execute the deployment of changes in project X as a preliminary step before starting this deployment.
- Execute the migrate command before starting the deployment process.
- None anticipated
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ jobs:
with:
ruby-version: "3.3.0"
bundler-cache: true
- name: Create database
run: bundle exec rake auction_fun_core:db:create_database[postgres]
- name: Create database and running migrations
run: |
bundle exec rake auction_fun_core:db:create_database[postgres]
bundle exec rake auction_fun_core:db:migrate
- name: Run lint
run: bundle exec standardrb
- name: Run suite
run: bundle exec rspec --color --format progress
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gem "rack-cors", "2.0.2"
gem "dotenv-rails", "3.1.0", require: "dotenv/load", groups: %i[development test]

# AuctionFunCore
gem "auction_fun_core", "0.8.5"
gem "auction_fun_core", "0.8.10"
# gem "auction_fun_core", path: "../auction_fun_core", require: "auction_fun_core"

group :development, :test do
Expand Down
34 changes: 8 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
PATH
remote: ../auction_fun_core
specs:
auction_fun_core (0.8.5)
activesupport (= 7.1.3.2)
bcrypt (= 3.1.20)
dotenv (= 3.1.0)
dry-events (= 1.0.1)
dry-matcher (= 1.0.0)
dry-monads (= 1.6.0)
dry-system (= 1.0.1)
dry-validation (= 1.10.0)
idlemailer (= 2.2.0)
money (= 6.19.0)
pg (= 1.5.6)
phonelib (= 0.8.8)
rake (= 13.2.0)
rom (= 5.3.0)
rom-sql (= 3.6.2)
sidekiq (= 7.2.2)
yard (= 0.9.36)
zeitwerk (= 2.6.13)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -99,7 +76,7 @@ GEM
mutex_m
tzinfo (~> 2.0)
ast (2.4.2)
auction_fun_core (0.8.5)
auction_fun_core (0.8.10)
activesupport (= 7.1.3.2)
bcrypt (= 3.1.20)
dotenv (= 3.1.0)
Expand All @@ -116,6 +93,7 @@ GEM
rom (= 5.3.0)
rom-sql (= 3.6.2)
sidekiq (= 7.2.2)
sidekiq-unique-jobs (= 8.0.10)
yard (= 0.9.36)
zeitwerk (= 2.6.13)
base64 (0.2.0)
Expand Down Expand Up @@ -308,7 +286,7 @@ GEM
rake (13.2.0)
rdoc (6.6.2)
psych (>= 4.0.0)
redis-client (0.21.1)
redis-client (0.22.1)
connection_pool
regexp_parser (2.9.0)
reline (0.4.3)
Expand Down Expand Up @@ -387,6 +365,10 @@ GEM
connection_pool (>= 2.3.0)
rack (>= 2.2.4)
redis-client (>= 0.19.0)
sidekiq-unique-jobs (8.0.10)
concurrent-ruby (~> 1.0, >= 1.0.5)
sidekiq (>= 7.0.0, < 8.0.0)
thor (>= 1.0, < 3.0)
standard (1.35.1)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
Expand Down Expand Up @@ -422,7 +404,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
auction_fun_core (= 0.8.5)
auction_fun_core (= 0.8.10)
bootsnap (= 1.18.3)
database_cleaner-sequel (= 2.0.2)
dotenv-rails (= 3.1.0)
Expand Down
3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
web: RUBYOPT="-W0" bundle exec rails s
database: postgres
redis: redis-server
mailserver: maildev --hide-extensions STARTTLS
background: bundle exec sidekiq
background: RUBYOPT="-W0" bundle exec sidekiq
18 changes: 18 additions & 0 deletions app/controllers/v1/auctions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module V1
# Main class (Add doc)
class AuctionsController < ApplicationController
def index
auctions = auction_relation.all(params[:page] || 1, params[:per_page] || 25).to_a

render json: {auctions: auctions}, status: :ok
end

private

def auction_relation
AuctionFunCore::Application[:container].relations[:auctions]
end
end
end
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class Application < Rails::Application
# Skip views, helpers and assets when generating a new resource.
config.api_only = true

# Configure default timezone
config.time_zone = "America/Sao_Paulo"

# Custom global configurations
config.secret_key_base = ENV.fetch("SECRET_KEY_BASE")
config.x.database_url = ENV.fetch("DATABASE_URL")
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
config.active_support.disallowed_deprecation_warnings = []

# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
config.active_record.migration_error = false

# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/auction_fun_core.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
AuctionFunCore::Application[:settings].database_url = ENV.fetch("DATABASE_URL")
AuctionFunCore::Application[:settings].redis_url = ENV.fetch("REDIS_URL")

# Import locales from gem
I18n.load_path += Dir[Gem.loaded_specs["auction_fun_core"].full_gem_path + "/i18n/**/*.{rb,yml}"]

AuctionFunCore::Application.finalize!
13 changes: 10 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
if Rails.env.development?
require "sidekiq/web"

Sidekiq::Web.use ActionDispatch::Cookies
Sidekiq::Web.use ActionDispatch::Session::CookieStore, key: "_auctionfuncore_session"
mount Sidekiq::Web => "/sidekiq"
end

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", :as => :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
namespace :v1, defaults: {format: :json} do
resources :auctions, only: %i[index]
end
end
31 changes: 31 additions & 0 deletions spec/controllers/v1/auctions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "rails_helper"

describe V1::AuctionsController do
describe "Routes", type: :routing do
it { expect(get("/v1/auctions")).to route_to("v1/auctions#index", format: :json) }
end

describe "GET #index", type: :controller do
context "when auctions are available" do
let!(:auction) { Factory[:auction, :default_running_standard] }

it "expect return http status ok with auctions" do
get :index

expect(response).to have_http_status(:ok)
expect(response.body).to include(auction.title)
end
end

context "when there are no auctions available" do
it "expect return http status ok with empty results" do
get :index

expect(response).to have_http_status(:ok)
expect(json_response["auctions"]).to be_blank
end
end
end
end
14 changes: 4 additions & 10 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
require "rspec/rails"

# Start auction fun core application, configure rom and load factories directly from auction core gem.
require "sidekiq/testing"
require "rom-factory"
require "database_cleaner/sequel"
AuctionCore::Application.start(:core)
AuctionFunCore::Application.start(:core)

Factory = ROM::Factory.configure do |config|
config.rom = AuctionCore::Application[:container]
config.rom = AuctionFunCore::Application[:container]
end

Dir[AuctionFunCore::Application.root.join("spec", "support", "**", "*.rb")].each { |f| require f }
Expand All @@ -33,15 +34,8 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f }
Rails.root.glob("spec/support/**/*.rb").sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
abort e.to_s.strip
end
RSpec.configure do |config|
config.add_setting :rom
config.rom = Factory.rom
Expand Down
3 changes: 3 additions & 0 deletions spec/support/database_cleaner_sequel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

DatabaseCleaner.strategy = :truncation
13 changes: 13 additions & 0 deletions spec/support/requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module ControllerHelpers
def json_response
return if response.body.blank?

JSON.parse(response.body, symbolize_keys: true)
end
end

RSpec.configure do |c|
c.include ControllerHelpers, type: :controller
end

0 comments on commit 678b3ed

Please sign in to comment.