aboutsummaryrefslogtreecommitdiff
path: root/brep/package-search.cxx
blob: 3317d43d390ae8adcf68c357d3c0330e69cd2fc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// file      : brep/package-search.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <brep/package-search>

#include <string>
#include <memory> // make_shared()

#include <xml/serializer>

#include <odb/database.hxx>
#include <odb/transaction.hxx>

#include <web/xhtml>
#include <web/module>
#include <web/mime-url-encoding>

#include <brep/package>
#include <brep/package-odb>
#include <brep/shared-database>

using namespace std;
using namespace cli;
using namespace odb::core;

namespace brep
{
  void package_search::
  init (scanner& s)
  {
    MODULE_DIAG;

    options_ = make_shared<options::package_search> (
      s, unknown_mode::fail, unknown_mode::fail);

    db_ = shared_database (options_->db_host (), options_->db_port ());
  }

  void package_search::
  handle (request& rq, response& rs)
  {
    using namespace xml;
    using namespace web;
    using namespace web::xhtml;

    MODULE_DIAG;

    params::package_search pr;

    try
    {
      param_scanner s (rq.parameters ());
      pr = params::package_search (s, unknown_mode::fail, unknown_mode::fail);
    }
    catch (const unknown_argument& e)
    {
      throw invalid_request (400, e.what ());
    }

    // @@ Would be nice to have a manipulator identing string properly
    //    according to the most nested element identation.
    //
    const char* ident ("\n      ");
    const char* title ("Package Search");
    serializer s (rs.content (), title);

    s << HTML
      <<   HEAD
      <<     TITLE << title << ~TITLE
      <<     STYLE(TYPE="text/css") << ident
      <<     ".package {margin: 0 0 0.5em;}" << ident
      <<     ".name a {text-decoration: none;}" << ident
      <<     ".summary {font-size: small;}"
      <<     ~STYLE
      <<   ~HEAD
      <<   BODY;

    string q (
      pr.query ().empty () ? "" : "q=" + mime_url_encode (pr.query ()));

    transaction t (db_->begin ());

    // @@ Use appropriate view when clarify which package info to be displayed
    //    and search index structure get implemented.
    //
    using query = query<package>;
    auto r (
      db_->query<package> (
        "ORDER BY" + query::name +
        "OFFSET" + to_string (pr.page () * options_->results_on_page ()) +
        "LIMIT" + to_string (options_->results_on_page ())));

    for (const auto& p: r)
    {
      s <<   DIV(CLASS="package")
        <<     DIV(CLASS="name")
        <<       A
        <<         HREF
        <<           "/go/" << mime_url_encode (p.name);

      // Propagate search criteria to the package version search url.
      //
      if (!q.empty ())
        s <<         "?" << q;

      s <<         ~HREF
        <<         p.name
        <<       ~A
        <<     ~DIV
        <<     DIV(CLASS="summary")
        <<       p.summary
        <<     ~DIV
        <<   ~DIV;
    }

    t.commit ();

    if (pr.page () || r.size () == options_->results_on_page ())
    {
      s <<   DIV;

      if (pr.page ())
      {
        s <<   A
          <<     HREF << "/?p=" << pr.page () - 1
          <<             (q.empty () ? "" : "&" + q)
          <<     ~HREF
          <<     "Previous"
          <<   ~A
          <<   " ";
      }

      // @@ Not ideal as can produce link to an empty page, but easy to fix
      //    and most likelly will be replaced with something more meaningful
      //    based on knowing the total number of matched packages.
      //
      if (r.size () == options_->results_on_page ())
      {
        s <<   A
          <<     HREF << "/?p=" << pr.page () + 1
          <<             (q.empty () ? "" : "&" + q)
          <<     ~HREF
          <<     "Next"
          <<   ~A;
      }

      s <<   ~DIV;
    }

    s <<   ~BODY
      << ~HTML;
  }
}