Книга: Practical Common Lisp

The Browse Page

The Browse Page

The first URL function will generate a page for browsing the MP3 database. Its query parameters will tell it what kind of thing the user is browsing and provide the criteria of what elements of the database they're interested in. It'll give them a way to select database entries that match a specific genre, artist, or album. In the interest of serendipity, you can also provide a way to select a random subset of matching items. When the user is browsing at the level of individual songs, the title of the song will be a link that causes that song to be added to the playlist. Otherwise, each item will be presented with links that let the user browse the listed item by some other category. For example, if the user is browsing genres, the entry "Blues" will contain links to browse all albums, artists, and songs in the genre Blues. Additionally, the browse page will feature an "Add all" button that adds every song matching the page's criteria to the user's playlist. The function looks like this:

(define-url-function browse
(request (what keyword :genre) genre artist album (random integer))
(let* ((values (values-for-page what genre artist album random))
(title (browse-page-title what random genre artist album))
(single-column (if (eql what :song) :file what))
(values-string (values->base-64 single-column values)))
(html
(:mp3-browser-page
(:title title)
((:form :method "POST" :action "playlist")
(:input :name "values" :type "hidden" :value values-string)
(:input :name "what" :type "hidden" :value single-column)
(:input :name "action" :type "hidden" :value :add-songs)
(:input :name "submit" :type "submit" :value "Add all"))
(:ul (do-rows (row values) (list-item-for-page what row)))))))

This function starts by using the function values-for-page to get a table containing the values it needs to present. When the user is browsing by song—when the what parameter is :song—you want to select complete rows from the database. But when they're browsing by genre, artist, or album, you want to select only the distinct values for the given category. The database function select does most of the heavy lifting, with values-for-page mostly responsible for passing the right arguments depending on the value of what. This is also where you select a random subset of the matching rows if necessary.

(defun values-for-page (what genre artist album random)
(let ((values
(select
:from *mp3s*
:columns (if (eql what :song) t what)
:where (matching *mp3s* :genre genre :artist artist :album album)
:distinct (not (eql what :song))
:order-by (if (eql what :song) '(:album :track) what))))
(if random (random-selection values random) values)))

To generate the title for the browse page, you pass the browsing criteria to the following function, browse-page-title:

(defun browse-page-title (what random genre artist album)
(with-output-to-string (s)
(when random (format s "~:(~r~) Random " random))
(format s "~:(~a~p~)" what random)
(when (or genre artist album)
(when (not (eql what :song)) (princ " with songs" s))
(when genre (format s " in genre ~a" genre))
(when artist (format s " by artist ~a " artist))
(when album (format s " on album ~a" album)))))

Once you have the values you want to present, you need to do two things with them. The main task, of course, is to present them, which happens in the do-rows loop, leaving the rendering of each row to the function list-item-for-page. That function renders :song rows one way and all other kinds another way.

(defun list-item-for-page (what row)
(if (eql what :song)
(with-column-values (song file album artist genre) row
(html
(:li
(:a :href (link "playlist" :file file :action "add-songs") (:b song)) " from "
(:a :href (link "browse" :what :song :album album) album) " by "
(:a :href (link "browse" :what :song :artist artist) artist) " in genre "
(:a :href (link "browse" :what :song :genre genre) genre))))
(let ((value (column-value row what)))
(html
(:li value " - "
(browse-link :genre what value)
(browse-link :artist what value)
(browse-link :album what value)
(browse-link :song what value))))))
(defun browse-link (new-what what value)
(unless (eql new-what what)
(html
"["
(:a :href (link "browse" :what new-what what value) (:format "~(~as~)" new-what))
"] ")))

The other thing on the browse page is a form with several hidden INPUT fields and an "Add all" submit button. You need to use an HTML form instead of a regular link to keep the application stateless—to make sure all the information needed to respond to a request comes in the request itself. Because the browse page results can be partially random, you need to submit a fair bit of data for the server to be able to reconstitute the list of songs to add to the playlist. If you didn't allow the browse page to return randomly generated results, you wouldn't need much data—you could just submit a request to add songs with whatever search criteria the browse page used. But if you added songs that way, with criteria that included a random argument, then you'd end up adding a different set of random songs than the user was looking at on the page when they hit the "Add all" button.

The solution you'll use is to send back a form that has enough information stashed away in a hidden INPUT element to allow the server to reconstitute the list of songs matching the browse page criteria. That information is the list of values returned by values-for-page and the value of the what parameter. This is where you use the base64-list parameter type; the function values->base64 extracts the values of a specified column from the table returned by values-for-page into a list and then makes a base 64-encoded string out of that list to embed in the form.

(defun values->base-64 (column values-table)
(flet ((value (r) (column-value r column)))
(obj->base64 (map-rows #'value values-table))))

When that parameter comes back as the value of the values query parameter to a URL function that declares values to be of type base-64-list, it'll be automatically converted back to a list. As you'll see in a moment, that list can then be used to construct a query that'll return the correct list of songs.[310] When you're browsing by :song, you use the values from the :file column since they uniquely identify the actual songs while the song names may not.

Оглавление книги


Генерация: 0.974. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз