Commit cc535c3b authored by Candy Tsai's avatar Candy Tsai

add search results with pagination

parent 2900cab1
Pipeline #49304 failed with stage
in 9 minutes and 55 seconds
......@@ -31,6 +31,7 @@ Build-Depends: debhelper (>= 9.20160709),
ruby-rspec (>= 2.14) <!pkg.debci.runtime>,
ruby-sinatra,
ruby-sinatra-contrib,
ruby-kaminari,
ruby-sqlite3 <!pkg.debci.runtime>,
ruby-thor,
shellcheck <!pkg.debci.runtime>,
......
......@@ -6,7 +6,8 @@ require 'securerandom'
require 'sinatra'
require "sinatra/namespace"
require 'time'
require 'kaminari/core'
require 'kaminari/activerecord'
require 'debci'
require 'debci/job'
require 'debci/key'
......@@ -64,6 +65,7 @@ module Debci
class API < SelfDocAPI
register Sinatra::Namespace
set :views, File.dirname(__FILE__) + '/api'
attr_reader :suite, :arch, :user
......@@ -379,16 +381,13 @@ module Debci
201
end
get '/package' do
package = params[:package]
exists = Debci::Job.exists?(package: package)
if exists
prefix = package[0]
redirect "/packages/#{prefix}/#{package}"
end
# TODO: add template for this
halt(404, 'package not found')
@query = params[:query]
@current_page = params[:page]
results = Debci::Job.where("package LIKE :prefix", prefix: "#{@query}%").select("package").group("package")
@results = results.page(@current_page).per(10)
@total = @results.total_pages
erb :search_results
end
end
......
<h1>Search Results: <%= @query %></h1>
<ul class='list-group' style='max-width: 400px'>
<% @results.each do |result| %>
<li class='list-group-item'>
<a href='/packages/<%= result.package[0] %>/<%= result.package %>'>
<%= result.package %>
</a></li>
<% end %>
</ul>
<nav aria-label='Page navigation'>
<ul class='pagination'>
<li>
<a href='/api/v1/package?query=<%= @query %>&page=<%= @current_page.to_i - 1 %>' aria-label='Previous'>
<span aria-hidden='true'>&laquo;</span>
</a>
</li>
<% for n in 1..@total %>
<li class='<%= 'active' if @current_page.to_i == n %>'>
<a href='/api/v1/package?query=<%= @query %>&page=<%= n %>'><%= n %></a>
</li>
<% end %>
<li>
<a href='/api/v1/package?query=<%= @query %>&page=<%= @current_page.to_i + 1 %>' aria-label='Next'>
<span aria-hidden='true'>&raquo;</span>
</a>
</li>
</ul>
</nav>
\ No newline at end of file
......@@ -34,9 +34,11 @@
</div> <!-- class='col-md-4' -->
<div class="col-md-4">
<h3>Search Package</h3>
<form action='/api/v1/package' style="display: inline;">
<input autofocus="" id="package" name="package" size="20" type="text" value="">
<input type="submit" value="Go">
<form action='/api/v1/package' class="form-inline">
<div class="form-group">
<input type="text" class="form-control" id="query" name="query" placeholder="Package to search...">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
</div>
</div> <!-- class='col-md-4' -->
......
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