Commit ea1f0df4 authored by Candy Tsai's avatar Candy Tsai

get search package from server

parent f6f27e44
Pipeline #50376 passed with stage
in 9 minutes and 48 seconds
......@@ -10,3 +10,8 @@ Style/FrozenStringLiteralComment:
Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
- 'lib/debci/api.rb' # documentation
Metrics/ClassLength:
Exclude:
- 'lib/debci/api.rb' # documentation
......@@ -321,11 +321,18 @@ Metrics/AbcSize:
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 260
Exclude:
- 'lib/debci/api.rb'
# Offense count: 4
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 299
Exclude:
- 'lib/debci/api.rb'
# Offense count: 2
Metrics/CyclomaticComplexity:
......
......@@ -31,6 +31,8 @@ Build-Depends: debhelper (>= 9.20160709),
ruby-rspec (>= 2.14) <!pkg.debci.runtime>,
ruby-sinatra,
ruby-sinatra-contrib,
ruby-kaminari-activerecord,
ruby-kaminari-actionview,
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,7 +381,14 @@ module Debci
201
end
get '/package' do
@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
protected
......
<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.match(/^((lib)?.)/)[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,20 +34,13 @@
</div> <!-- class='col-md-4' -->
<div class="col-md-4">
<h3>Search Package</h3>
<form action='javascript:redirect();' name="searchform" id="searchform" method="post" 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>
<script>
function redirect() {
var package = document.getElementById("package").value
var m = package.match(/((lib)?.)/)
prefix = m[0]
location.href = '/packages/' + prefix + '/' + package
}
</script>
</div> <!-- class='col-md-4' -->
</div> <!-- class='row' -->
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