Creating the recently added route

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:

  1. Create a new 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]))
  2. Next, we'll define a new album-routes defroute that will encapsulate all our albums-related routes for hipstr:
    (defroutes album-routes
      (GET "/albums/recently-added" [] (album/get-recently-added)))
  3. Finally, we have to add the new album-routes to the hipstr app-handler:
    1. Open the 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]]
      …)
    2. Add 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):

Creating the recently added route

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.69.199