107 lines
3.7 KiB
Ruby
107 lines
3.7 KiB
Ruby
require_relative "./pex.rb"
|
|
|
|
class LifePex::Systems::Pex2System < LifePex::Systems::PexSystem
|
|
# include JSON::API # included by PexSystem
|
|
|
|
# extend DocMyRoutes::Annotatable # included by PexSystem
|
|
# register Sinatra::Namespace # included by PexSystem
|
|
namespace '/api/pex/v2' do
|
|
|
|
namespace '/meta' do
|
|
|
|
summary 'Get a list of all pex categories'
|
|
produces 'application/json'
|
|
status_codes [200]
|
|
get "/categories", auth: [], provides: 'json' do
|
|
LifePex::Pex.where(user_id: current_user_id).select(:category).uniq.map(&:category).to_json
|
|
end
|
|
|
|
end
|
|
|
|
namespace '/pexs' do
|
|
|
|
summary 'Get a list of all pex'
|
|
produces 'application/json'
|
|
status_codes [200]
|
|
parameter :pluck, required: false, type: 'string', in: 'query', description: 'improve performance by only fetching one field'
|
|
get "", auth: [], provides: 'json' do
|
|
pexs = current_user.pexs
|
|
pexs = pexs.select(json_params["pluck"]) if json_params["pluck"]
|
|
pexs.to_json
|
|
end
|
|
|
|
summary 'Create a new pex'
|
|
produces 'application/json'
|
|
status_codes [200]
|
|
parameter :name, required: true, type: 'string', in: 'body'
|
|
parameter :category, required: true, type: 'string', in: 'body'
|
|
parameter :amount, required: true, type: 'number', in: 'body'
|
|
parameter :auth, required: true, type: 'string', in: 'cookies'
|
|
post "/", auth: [], provides: 'json' do
|
|
raise RunetimeError.new "\"name\" is required" unless json_params["name"]
|
|
raise RunetimeError.new "\"category\" is required" unless json_params["category"]
|
|
raise RunetimeError.new "\"amount\" is required" unless json_params["amount"]
|
|
pex_id = LifePex::Pex.insert(
|
|
name: json_params['name'],
|
|
category: json_params['category'],
|
|
amount: json_params['amount'],
|
|
user_id: current_user.id,
|
|
)
|
|
{
|
|
message: "entity created",
|
|
pex: LifePex::Pex.find(id: pex_id),
|
|
}.to_json
|
|
end
|
|
|
|
summary 'Show the state of a pex'
|
|
produces 'application/json'
|
|
status_codes [200]
|
|
parameter :id, required: true, type: 'string', in: 'path'
|
|
get "/:id", auth: [], provides: 'json' do
|
|
{
|
|
pex: LifePex::Pex.find(
|
|
id: params["id"],
|
|
user_id: current_user.id,
|
|
),
|
|
}.to_json
|
|
end
|
|
|
|
summary 'Show the state of a pex and load advanced infos about it'
|
|
produces 'application/json'
|
|
status_codes [200]
|
|
parameter :id, required: true, type: 'string', in: 'path'
|
|
get "/:id/more", auth: [], provides: 'json' do
|
|
pex = LifePex::Pex.find(
|
|
id: params["id"],
|
|
user_id: current_user_id,
|
|
)
|
|
{
|
|
pex: pex,
|
|
user_pexs: LifePex::UserPex.where(user_id: current_user_id, pex_id: pex.id).select(:created_at).all.group_by(&:created_at).transform_values { |group| group.size }
|
|
}.to_json
|
|
end
|
|
|
|
summary 'Get the amount of a pex each day since first occurence since 1 january'
|
|
notes ""
|
|
produces 'application/json'
|
|
status_codes [200]
|
|
parameter :id, required: true, type: 'integer', in: 'path'
|
|
parameter :auth, required: true, type: 'string', in: 'cookies'
|
|
post "/:id/recap", auth: [], provides: 'json' do
|
|
user_id = user_id_decoded
|
|
pex_id = params["id"]
|
|
year = Date.today.year
|
|
first_jan = Date.new(year, 1, 1)
|
|
pex = LifePex::Pex.find(id: pex_id)
|
|
user_pexs = LifePex::UserPex.where {
|
|
self.user_id == user_id && self.pex_id == pex_id && created_at > first_jan
|
|
}.first
|
|
{ pex: pex, user_pexs: user_pexs }.to_json
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
include LifePex::Systems::ApiList
|
|
end
|