Skip to content

Commit

Permalink
iss #69: [wip] display search results
Browse files Browse the repository at this point in the history
  • Loading branch information
maizy committed Apr 25, 2021
1 parent ea9ef9c commit fa15399
Show file tree
Hide file tree
Showing 10 changed files with 1,500 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/ponylib/cmd/web_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var webUiCmd = &cobra.Command{
engine := gin.Default()
web.SetupMiddlewares(engine)
web.SetupTemplates(engine, devMode)
web.AppendRouters(engine)
web.AppendRouters(engine, conn)

addr := fmt.Sprintf("%s:%d", bindHost, bindPort)
fmt.Printf("launching web app at: http://%s\n", addr)
Expand Down
18 changes: 13 additions & 5 deletions fb2_parser/fb2_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ func (b *Book) String() string {
return b.Title
}

func (b *Book) WrittenAt() string {
if b.Date != nil {
if b.Date.Month() == time.January && b.Date.Day() == 1 {
return b.Date.Format("2006")
}
return b.Date.Format("2006-01-02")
} else if b.FormattedDate != nil {
return *b.FormattedDate
}
return ""
}

func (b *Book) AdditionalInfoString() *string {
if b.Date != nil || b.FormattedDate != nil || b.Lang != nil {
var sb strings.Builder
Expand All @@ -34,11 +46,7 @@ func (b *Book) AdditionalInfoString() *string {
sb.WriteString(", ")
}
sb.WriteString("Written at: ")
if b.Date != nil {
sb.WriteString(b.Date.Format("2006-01-02"))
} else {
sb.WriteString(*b.FormattedDate)
}
sb.WriteString(b.WrittenAt())
}
return u.StrPtr(sb.String())
}
Expand Down
58 changes: 58 additions & 0 deletions ponylib_app/web/books.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package web

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"

"dev.maizy.ru/ponylib/ponylib_app/search"
)

func BuildBooksHandler(conn *pgxpool.Pool) func(c *gin.Context) {

return func(c *gin.Context) {
query := c.Query("query")

var results *search.BookSearchResult
var totalFound int

if query != "" {
offset := 0
limit := 40

queryStruct := search.BookSearchQuery{TextMatch: query}

if found, err := search.CountBooks(conn, &queryStruct); err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("unable to count matched books: %w", err))
return
} else {
totalFound = found
}

if totalFound > 0 {
if res, err := search.SearchBooks(conn, &queryStruct, offset, limit); err != nil {
c.AbortWithError(
http.StatusInternalServerError,
fmt.Errorf("unable to count matched books: %w", err),
)
return
} else {
results = res
}
}
}

titleQuery := query
if len(titleQuery) >= 32 {
titleQuery = query[:30] + "…"
}
c.HTML(http.StatusOK, "search.tmpl", WithCommonVars(c, gin.H{
"subtitle": "Books · " + query,
"query": query,
"totalFound": totalFound,
"results": results,
}))
}
}
64 changes: 19 additions & 45 deletions ponylib_app/web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,15 @@ import (
"strings"

"github.com/gin-gonic/gin"

"dev.maizy.ru/ponylib/ponylib_app"
"github.com/jackc/pgx/v4/pgxpool"
)

func AppendWebUiRouters(engine *gin.Engine) {
engine.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"lang": "en",
"version": ponylib_app.GetVersion(),
"show_examples": true,
})
})

engine.GET("/auth", func(c *gin.Context) {
c.HTML(http.StatusOK, "auth.tmpl", gin.H{
"lang": "en",
"version": ponylib_app.GetVersion(),
})
})

engine.POST("/unlock", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/")
})

engine.GET("/books", func(c *gin.Context) {
query := c.Query("query")
titleQuery := query
if len(titleQuery) >= 32 {
titleQuery = query[:30] + "…"
}
c.HTML(http.StatusOK, "search.tmpl", gin.H{
"lang": "en",
"subtitle": "Books · " + query,
"query": query,
"version": ponylib_app.GetVersion(),
})
})
}

func AppendApiRouters(engine *gin.Engine) {
apiGroup := engine.Group("/api/v1")
apiGroup.GET("/version", BuildVersionHandler())
}

const staticPrefix = "/static/"

//go:embed static/*
var staticFS embed.FS

func AppendRouters(engine *gin.Engine) {
func AppendRouters(engine *gin.Engine, conn *pgxpool.Pool) {

engine.GET(staticPrefix+"*filepath", func(c *gin.Context) {
path := c.Request.URL.Path
Expand All @@ -66,6 +25,21 @@ func AppendRouters(engine *gin.Engine) {
}
})

AppendApiRouters(engine)
AppendWebUiRouters(engine)
engine.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", WithCommonVars(c, gin.H{
"show_examples": true,
}))
})

engine.GET("/auth", func(c *gin.Context) {
c.HTML(http.StatusOK, "auth.tmpl", WithCommonVars(c, gin.H{}))
})

engine.POST("/unlock", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/")
})

engine.GET("/version", BuildVersionHandler())

engine.GET("/books", BuildBooksHandler(conn))
}
Loading

0 comments on commit fa15399

Please sign in to comment.