Skip to content

Commit

Permalink
adding pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-assar committed Mar 13, 2024
1 parent de9dbc0 commit 8e6ed47
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
32 changes: 22 additions & 10 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ func SetupRoutes(app_settings common.AppSettings, database database.Database) *g
addCachableHandler(r, "GET", "/contact", contactHandler, &cache, app_settings, database)
addCachableHandler(r, "GET", "/post/:id", postHandler, &cache, app_settings, database)

// Add the pagination route as a cacheable endpoint
addCachableHandler(r, "GET", "/page/:num", homeHandler, &cache, app_settings, database)

// DO not cache as it needs to handlenew form values
r.POST("/contact-send", makeContactFormHandler())

// this will setup the route for pagination
r.GET("/page/:num", func(c *gin.Context) {
homeHandler(c, app_settings, database)
})

r.Static("/static", "./static")
return r
}
Expand Down Expand Up @@ -81,12 +79,19 @@ func addCachableHandler(e *gin.Engine, method string, endpoint string, generator
// / This function will act as the handler for
// / the home page
func homeHandler(c *gin.Context, settings common.AppSettings, db database.Database) ([]byte, error) {
pageNumQuery := c.Param("num")
pageNum, err := strconv.Atoi(pageNumQuery)
if err != nil {
return nil, err
pageNum := 0 // Default to page 0
if pageNumQuery := c.Param("num"); pageNumQuery != "" {
num, err := strconv.Atoi(pageNumQuery)
if err == nil && num > 0 {
pageNum = num
} else {
log.Error().Msgf("Invalid page number: %s", pageNumQuery)
}
}

// Ensure pageNum is at least 1
pageNum = max(pageNum, 1)

limit := 10 // or whatever limit you want
offset := (pageNum - 1) * limit

Expand All @@ -101,9 +106,16 @@ func homeHandler(c *gin.Context, settings common.AppSettings, db database.Databa

err = index_view.Render(c, html_buffer)
if err != nil {
log.Error().Msgf("could not render index: %v", err)
log.Error().Msgf("Could not render index: %v", err)
return []byte{}, err
}

return html_buffer.Bytes(), nil
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
4 changes: 2 additions & 2 deletions cmd/urchin/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type DatabaseMock struct{}

func (db DatabaseMock) GetPosts() ([]common.Post, error) {
func (db DatabaseMock) GetPosts(int, int) ([]common.Post, error) {
return []common.Post{
{
Title: "TestPost",
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestIndexPing(t *testing.T) {

r := app.SetupRoutes(app_settings, database_mock)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
req, _ := http.NewRequest("GET", "/page/0", nil) //...
r.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
Expand Down
4 changes: 2 additions & 2 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type SqlDatabase struct {
// / This function gets all the posts from the current
// / database connection.
func (db SqlDatabase) GetPosts(limit int, offset int) (all_posts []common.Post, err error) {
query := fmt.Sprintf("SELECT title, excerpt, id FROM posts LIMIT %d OFFSET %d;", limit, offset)
rows, err := db.Connection.Query(query)
query := "SELECT title, excerpt, id FROM posts LIMIT ? OFFSET ?;"
rows, err := db.Connection.Query(query, limit, offset)
if err != nil {
return make([]common.Post, 0), err
}
Expand Down

0 comments on commit 8e6ed47

Please sign in to comment.