Commit 5cb86361 authored by charlie-ablett's avatar charlie-ablett
Browse files

Favourites index action

parent d7f73605
module Api
module V1
class RecipesController < ApplicationController
before_action :authenticate_account!, only: [:create, :update, :destroy, :favourite, :unfavourite]
before_action :authenticate_account!, only: [:create, :update, :destroy, :favourite, :unfavourite, :favourites]
before_action :authenticate!, only: [:index, :show, :execute]
respond_to :json
......@@ -51,6 +51,10 @@ module Api
render json: recipes, scope: current_entity, scope_name: :current_entity
end
def favourites
render json: favourite_recipes, scope: current_entity, scope_name: :current_entity
end
def show
render json: recipe, scope: current_entity, scope_name: :current_entity
rescue => e
......@@ -112,6 +116,10 @@ module Api
def recipes
@recipes ||= Recipe.includes(:recipe_steps, process_chains: :process_steps).available_to_account(current_entity.account.id, current_entity.admin?)
end
def favourite_recipes
@recipes ||= Recipe.includes(:recipe_steps, process_chains: :process_steps).available_to_account(current_entity.account.id, current_entity.admin?).favourites(current_entity.account.id, current_entity.admin?)
end
end
end
end
......
......@@ -29,10 +29,14 @@ class Recipe < ApplicationRecord
if admin
all
else
active.where("PUBLIC = ? OR ACCOUNT_ID = ?", true, account_id)
active.where("RECIPES.PUBLIC = ? OR RECIPES.ACCOUNT_ID = ?", true, account_id)
end
}
scope :favourites, -> (account_id, admin) {
includes(:recipe_favourites).available_to_account(account_id, admin).where(recipe_favourites: { account_id: account_id })
}
def available_process_chains(current_entity)
if current_entity.admin?
ProcessChain.order(created_at: :desc)
......
......@@ -36,6 +36,10 @@ Rails.application.routes.draw do
get 'available_step_gems' => 'step_class#index_by_gems'
resources :recipes, only: [:index, :show, :create, :update, :destroy] do
collection do
get 'favourites'
end
member do
post 'execute'
get 'favourite'
......
......@@ -507,6 +507,63 @@ RSpec.describe Api::V1::RecipesController do
end
end
describe "GET favourites" do
context 'if a valid token is supplied' do
context 'there are no favourited recipes' do
it "finds no recipes" do
request_with_auth(account.new_jwt) do
perform_favourites_request
end
expect(response.status).to eq 200
expect(assigns[:recipes]).to eq []
end
end
context 'there are recipes' do
let!(:other_account) { create(:account) }
let!(:recipe1) { create(:recipe, name: "recipe1", account: account, public: true) }
let!(:recipe2) { create(:recipe, name: "recipe2", account: account, public: true, active: false) }
let!(:recipe3) { create(:recipe, name: "recipe3", account: other_account, public: false) }
let!(:recipe4) { create(:recipe, name: "recipe4", account: account, public: false) }
let!(:recipe5) { create(:recipe, name: "recipe5", account: account, public: true) }
before do
create(:recipe_favourite, recipe: recipe1, account: account )
create(:recipe_favourite, recipe: recipe2, account: account )
create(:recipe_favourite, recipe: recipe3, account: other_account )
create(:recipe_favourite, recipe: recipe4, account: account )
create(:recipe_favourite, recipe: recipe5, account: other_account )
end
it "finds the account's recipes" do
request_with_auth(account.new_jwt) do
perform_favourites_request
end
expect(response.status).to eq 200
expect(assigns[:recipes].to_a).to match_array [recipe1, recipe4]
end
end
end
context 'if no valid token is supplied' do
it "does not assign anything" do
request_with_auth do
perform_index_request({})
end
expect(response.status).to eq 401
expect(assigns[:recipe]).to be_nil
end
end
end
describe "GET show" do
context 'if a valid token is supplied' do
......@@ -951,6 +1008,10 @@ RSpec.describe Api::V1::RecipesController do
get_index_request(version, data)
end
def perform_favourites_request(data = {})
get_favourites_request(version, data)
end
def perform_show_request(data = {})
get_show_request(version, data)
end
......
......@@ -36,6 +36,10 @@ def execute_recipe(version, data = {}.to_json)
post :execute, params: data, headers: {'Content-Type' => "application/json", 'Accept' => "application/vnd.ink.#{version}" }
end
def get_favourites_request(version, data={}.to_json)
get :favourites, params: data, headers: {'Content-Type' => "application/json", 'Accept' => "application/vnd.ink.#{version}" }
end
def retry_execution(version, data = {}.to_json)
get :retry, params: data, headers: {'Content-Type' => "application/json", 'Accept' => "application/vnd.ink.#{version}" }
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment