require_relative "./pex.rb" class LifePex::Systems::Pex2System < LifePex::Systems::PexSystem # include JSON::API # included by PexSystem include LifePex::Systems::ApiResponse # 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 halt 400, "\"name\" is required" unless json_params["name"] halt 400, "\"category\" is required" unless json_params["category"] halt 400, "\"amount\" is required" unless json_params["amount"] halt 409, "conflict with existing pex with the same characteristics" if LifePex::Pex.find( name: json_params['name'], category: json_params['category'], user_id: current_user.id, ) 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 'Update an existing pex' produces 'application/json' status_codes [200] parameter :id, require: true, type: 'integer', in: 'path' parameter :name, required: false, type: 'string', in: 'body' parameter :category, required: false, type: 'string', in: 'body' parameter :amount, required: false, type: 'number', in: 'body' parameter :auth, required: true, ype: 'string', in: 'cookies' put '/:id', auth: [], provides: 'json' do pex = LifePex::Pex.find( id: params["id"], user_id: current_user.id, ) halt 404, { message: "No pex #{params["id"]}" }.to_json if pex.nil? pex.name = json_params['name'] unless json_params['name'].nil? pex.category = json_params['category'] unless json_params['category'].nil? pex.amount = json_params['amount'] unless json_params['amount'].nil? pex.save { message: "entity updated", pex: pex, }.to_json end summary 'Delete an existing pex' produces 'application/json' status_codes [200] parameter :id, require: true, type: 'integer', in: 'path' parameter :auth, required: true, type: 'string', in: 'cookies' delete '/:id', auth: [], provides: 'json' do pex = LifePex::Pex.find( id: params["id"], user_id: current_user.id, ) halt 404, { message: "No pex #{params["id"]}" }.to_json if pex.nil? pex.destroy { message: "entity deleted", pex: pex, }.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, ) halt 404, { message: "No pex #{params["id"]}" }.to_json if pex.nil? { pex: pex, }.to_json end summary 'Show the state of a pex and load advanced infos about it' produces 'application/json,text/html' 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, ) halt 404, { message: "No pex #{params["id"]}" }.to_json if pex.nil? user_pexs = LifePex::UserPex.where( user_id: current_user_id, pex_id: pex.id ).select(:created_at).all date = date_input_convertor(json_params["date"]) user_pexs_by_date = user_pexs.group_by(&:created_at).transform_values { |group| group.size } user_pexs_at_date = user_pexs_by_date[date] pex_with_more = { pex: pex, user_pexs: { by_date: user_pexs_by_date, at_date: user_pexs_at_date, total: user_pexs.size, }, } given_content_type do json pex_with_more.to_json html slim_partial("pex_row", locals: { pex_with_amount: pex_with_more }) default do api_error(400, message: "No valid accepted content found.") end end end summary 'Get the amount of a pex each day since first occurence since a given date' notes '' produces 'application/json' status_codes [200] parameter :id, required: true, type: 'integer', in: 'path' parameter :auth, required: true, type: 'string', in: 'cookies' parameter :days_ago, required: false, type: 'integer', in: 'body', description: "default: 30, limit the amount of results to the n last days included" parameter :since_first_jan, required: false, type: 'boolean', in: 'body', description: "default: false, if no days_ago defined, limit to all dates after (included) 1st jan of the current year" post '/:id/recap', auth: [], provides: 'json' do pex_id = params["id"] pex = LifePex::Pex.find(id: pex_id, user_id: current_user_id) halt 404, { message: "No pex #{params["id"]}" }.to_json if pex.nil? start_date = 30.days.ago if params["days_ago"] start_date = params["days_ago"].to_i.days.ago elsif params["since_first_jan"] year = Date.today.year first_jan = Date.new(year, 1, 1) start_date = first_jan end user_pexs = LifePex::UserPex.where { # TODO query that self.user_id == current_user_id && self.pex_id == pex_id && created_at >= start_date } { pex: pex, user_pexs: user_pexs }.to_json end end namespace '/infos' do summary 'Get for all existing pex the last date a user_pex has been added' produces 'application/json' status_codes [200] parameter :id, required: true, type: 'string', in: 'path' get '/last-inserts', auth: [], provides: 'json' do pexs = LifePex::UserPex.last_inserted_at(current_user.id) pexs.map { |pex| { pex_id: pex[:pex_id], name: pex[:name], last_inserted_at: pex[:last_inserted_at], } }.to_json end end end include LifePex::Systems::ApiList end