Skip to content

Commit

Permalink
Get a very basic survey API working
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbrethorst committed Jul 1, 2024
1 parent fb22c86 commit 33c7307
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ruby "3.3.0"
gem "bcrypt", "~> 3.1.7"
gem "bootsnap", require: false
gem "importmap-rails"
gem "jbuilder"
gem "jbuilder", '~>2.12'
gem "pg", "~> 1.1"
gem 'positioning', '~> 0.2.2'
gem "puma", ">= 5.0"
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ DEPENDENCIES
feedjira (~> 3.2, >= 3.2.3)
haml (~> 6.3)
importmap-rails
jbuilder
jbuilder (~> 2.12)
jsonb_accessor (~> 1.4)
mailgun-ruby (~> 1.2.14)
pg (~> 1.1)
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/api/v1/surveys_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Api::V1::SurveysController < ApplicationController
before_action :load_region

def index
@surveys = Survey.includes(:study).where(available: true, studies: { region: @region })
end

private

def load_region
@region = Region.find(params[:region_id])
end
end
14 changes: 14 additions & 0 deletions app/views/api/v1/surveys/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
json.array! @surveys do |survey|
json.extract! survey, :id, :name, :created_at

json.study do
json.extract! survey.study, :id, :name, :description
end

json.questions do
json.array! survey.questions do |question|
json.extract! question, :id, :position, :content
end
end
end

3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

# Alerts
resources :alerts, only: [:index], defaults: {format: 'pb'}

# Surveys
resources :surveys, only: [:index]
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/requests/api/v1/surveys_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe "Api::V1::Surveys", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end

0 comments on commit 33c7307

Please sign in to comment.