Before we can view a page, we have to be able to serve it. We'll be creating a couple of album-related pages in hipstr. So let's create a new hipstr.routes.albums
namespace, and also create a route to serve up a page for the /albums/recently-added
URL:
hipstr.routes.albums
namespace in the hipstr/routes
directory. As with our other route namespaces, we'll be making use of Compojure for creating the route, as well as album-model
to retrieve the recently added albums from the database:(ns hipstr.routes.albums (:require [compojure.core :refer :all] [hipstr.layout :as layout] [hipstr.models.album-model :as album]))
album-routes
defroute that will encapsulate all our albums-related routes for hipstr:(defroutes album-routes (GET "/albums/recently-added" [] (album/get-recently-added)))
album-routes
to the hipstr app-handler
:hipstr/handler.clj
file and add a reference to the hipstr.routes.albums
namespace we just created:(:require [compojure.core :refer [defroutes]] [hipstr.routes.albums :refer [album-routes]] …)
album-routes
to the list of routes in app-handler
:(def app (app-handler ;; add your application routes here [home-routes album-routes base-routes test-routes] …))
At this point, we have enough plumbing to render the new route. Open your browser and navigate to http://localhost:3000/albums/recently-added
, You should see the following as a result (or else, something similar, depending on how your browser renders straight JSON):
Hooray! Fortune is smiling upon us! Our new route is responding to requests, and is also making use of the album-model
we created, which in turn uses YeSQL to fetch the 10 most recently added albums from the albums table (ordered by create_date desc
). All the plumbing is there. The only thing we have left to do is represent this data in something a bit more palatable than straight up JSON.
3.16.69.199