Implement business logic for reading Guidebooks

This commit is contained in:
Justin Edmund 2023-04-17 23:46:56 -07:00
parent 26ce703df9
commit 7de8594bf1
5 changed files with 94 additions and 0 deletions

View file

@ -0,0 +1,23 @@
# frozen_string_literal: true
module Api
module V1
class GuidebookBlueprint < ApiBlueprint
field :name do |book|
{
en: book.name_en,
ja: book.name_jp
}
end
field :description do |book|
{
en: book.name_en,
ja: book.name_jp
}
end
fields :granblue_id
end
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Api
module V1
class GuidebooksController < Api::V1::ApiController
def all
render json: GuidebookBlueprint.render(Guidebook.all)
end
end
end
end

View file

@ -196,6 +196,26 @@ module Api
})
end
def guidebooks
# Perform the query
books = if search_params[:query].present? && search_params[:query].length >= 2
Guidebook.method("#{locale}_search").call(search_params[:query])
else
Guidebook.all
end
count = books.length
paginated = books.paginate(page: search_params[:page], per_page: SEARCH_PER_PAGE)
render json: GuidebookBlueprint.render(paginated,
root: :results,
meta: {
count: count,
total_pages: total_pages(count),
per_page: SEARCH_PER_PAGE
})
end
private
def total_pages(count)

37
app/models/guidebook.rb Normal file
View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
class Guidebook < ApplicationRecord
alias eql? ==
include PgSearch::Model
pg_search_scope :en_search,
against: :name_en,
using: {
tsearch: {
prefix: true,
dictionary: 'simple'
}
}
pg_search_scope :jp_search,
against: :name_jp,
using: {
tsearch: {
prefix: true,
dictionary: 'simple'
}
}
def blueprint
GuidebookBlueprint
end
def display_resource(book)
book.name_en
end
def ==(other)
self.class == other.class && granblue_id === other.granblue_id
end
end

View file

@ -34,6 +34,7 @@ Rails.application.routes.draw do
post 'search/weapons', to: 'search#weapons'
post 'search/summons', to: 'search#summons'
post 'search/job_skills', to: 'search#job_skills'
post 'search/guidebooks', to: 'search#guidebooks'
get 'jobs', to: 'jobs#all'
@ -41,6 +42,8 @@ Rails.application.routes.draw do
get 'jobs/:id/skills', to: 'job_skills#job'
get 'jobs/:id/accessories', to: 'job_accessories#job'
get 'guidebooks', to: 'guidebooks#all'
get 'raids', to: 'raids#all'
get 'weapon_keys', to: 'weapon_keys#all'