diff --git a/Gemfile b/Gemfile index 824b6ca9f..8fe31bee1 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,9 @@ gem 'coffee-rails' gem 'haml' gem 'sass-rails' +# API data +gem 'jsonapi-resources' + # CSS framework gem 'bootstrap-sass' gem 'font-awesome-sass' diff --git a/Gemfile.lock b/Gemfile.lock index 5f8bcf612..c1f9e6b6b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -277,6 +277,10 @@ GEM railties (>= 3.2) sprockets-rails json (2.1.0) + jsonapi-resources (0.9.0) + activerecord (>= 4.1) + concurrent-ruby + railties (>= 4.1) jwt (1.5.6) kaminari (1.1.1) activesupport (>= 4.1.0) @@ -590,6 +594,7 @@ DEPENDENCIES jquery-rails jquery-ui-rails (~> 5.0.2) js-routes + jsonapi-resources kaminari leaflet-markercluster-rails leaflet-rails (~> 0.7.7) @@ -625,7 +630,6 @@ DEPENDENCIES will_paginate xmlrpc - RUBY VERSION ruby 2.4.1p111 diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb new file mode 100644 index 000000000..6f6efdaad --- /dev/null +++ b/app/controllers/api/v1/base_controller.rb @@ -0,0 +1,6 @@ +module Api + module V1 + class BaseController < JSONAPI::ResourceController + end + end +end diff --git a/app/controllers/api/v1/crops_controller.rb b/app/controllers/api/v1/crops_controller.rb new file mode 100644 index 000000000..9d4802df6 --- /dev/null +++ b/app/controllers/api/v1/crops_controller.rb @@ -0,0 +1,6 @@ +module Api + module V1 + class CropsController < Api::V1::BaseController + end + end +end diff --git a/app/controllers/api/v1/gardens_controller.rb b/app/controllers/api/v1/gardens_controller.rb new file mode 100644 index 000000000..f1af5fe2c --- /dev/null +++ b/app/controllers/api/v1/gardens_controller.rb @@ -0,0 +1,6 @@ +module Api + module V1 + class GardensController < Api::V1::BaseController + end + end +end diff --git a/app/controllers/api/v1/members_controller.rb b/app/controllers/api/v1/members_controller.rb new file mode 100644 index 000000000..288ed3318 --- /dev/null +++ b/app/controllers/api/v1/members_controller.rb @@ -0,0 +1,6 @@ +module Api + module V1 + class MembersController < Api::V1::BaseController + end + end +end diff --git a/app/controllers/api/v1/photos_controller.rb b/app/controllers/api/v1/photos_controller.rb new file mode 100644 index 000000000..89da2f708 --- /dev/null +++ b/app/controllers/api/v1/photos_controller.rb @@ -0,0 +1,6 @@ +module Api + module V1 + class PhotosController < Api::V1::BaseController + end + end +end diff --git a/app/controllers/api/v1/plantings_controller.rb b/app/controllers/api/v1/plantings_controller.rb new file mode 100644 index 000000000..30e1e54cf --- /dev/null +++ b/app/controllers/api/v1/plantings_controller.rb @@ -0,0 +1,6 @@ +module Api + module V1 + class PlantingsController < Api::V1::BaseController + end + end +end diff --git a/app/resources/api/v1/crop_resource.rb b/app/resources/api/v1/crop_resource.rb new file mode 100644 index 000000000..525cb1458 --- /dev/null +++ b/app/resources/api/v1/crop_resource.rb @@ -0,0 +1,13 @@ +module Api + module V1 + class CropResource < JSONAPI::Resource + immutable + model_name 'Crop' + attribute :name + attribute :en_wikipedia_url + has_many :plantings + has_many :photos + has_one :parent + end + end +end diff --git a/app/resources/api/v1/garden_resource.rb b/app/resources/api/v1/garden_resource.rb new file mode 100644 index 000000000..95521a873 --- /dev/null +++ b/app/resources/api/v1/garden_resource.rb @@ -0,0 +1,11 @@ +module Api + module V1 + class GardenResource < JSONAPI::Resource + immutable + model_name 'Garden' + attribute :name + has_one :owner, class_name: 'Member' + has_many :plantings + end + end +end diff --git a/app/resources/api/v1/member_resource.rb b/app/resources/api/v1/member_resource.rb new file mode 100644 index 000000000..cd0e3be01 --- /dev/null +++ b/app/resources/api/v1/member_resource.rb @@ -0,0 +1,10 @@ +module Api + module V1 + class MemberResource < JSONAPI::Resource + immutable + model_name 'Member' + attribute :login_name + has_many :gardens + end + end +end diff --git a/app/resources/api/v1/photo_resource.rb b/app/resources/api/v1/photo_resource.rb new file mode 100644 index 000000000..270773b07 --- /dev/null +++ b/app/resources/api/v1/photo_resource.rb @@ -0,0 +1,8 @@ +module Api + module V1 + class PhotoResource < JSONAPI::Resource + immutable + model_name 'Photo' + end + end +end diff --git a/app/resources/api/v1/planting_resource.rb b/app/resources/api/v1/planting_resource.rb new file mode 100644 index 000000000..5a9399b18 --- /dev/null +++ b/app/resources/api/v1/planting_resource.rb @@ -0,0 +1,20 @@ +module Api + module V1 + class PlantingResource < JSONAPI::Resource + immutable + model_name 'Planting' + has_one :garden + has_one :crop + has_one :owner + has_many :photos + + attribute :planted_at + attribute :finished_at + attribute :quantity + attribute :description + attribute :sunniness + attribute :planted_from + attribute :days_before_maturity + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 72a3d5e19..d8111b518 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -96,8 +96,17 @@ Growstuff::Application.routes.draw do get '/admin/newsletter' => 'admin#newsletter', :as => :admin_newsletter get '/admin/:action' => 'admin#:action' - get '/.well-known/acme-challenge/:id' => 'pages#letsencrypt' + namespace :api do + namespace :v1 do + jsonapi_resources :photos + jsonapi_resources :crops + jsonapi_resources :plantings + jsonapi_resources :gardens + jsonapi_resources :members + end + end + get '/.well-known/acme-challenge/:id' => 'pages#letsencrypt' # CMS stuff -- must remain LAST comfy_route :cms, path: '/', sitemap: false end