Skip to content

Commit

Permalink
refactor: abstract favicon logic
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijit-hota committed May 16, 2022
1 parent 7f2a22e commit af126e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
18 changes: 18 additions & 0 deletions api/db/bookmark.model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package db

import (
"api/utils"
"net/url"
"strings"
)

type Meta struct {
Title string `json:"title"`
Description string `json:"description"`
Expand All @@ -14,6 +20,18 @@ type Bookmark struct {
LastUpdated int64 `json:"last_updated,omitempty"`
}

func (bm *Bookmark) NormalizeFavicon() {
if bm.Meta.Favicon == "" {
bm.Meta.Favicon = "favicon.ico"
}
favicon, err := url.Parse(bm.Meta.Favicon)
utils.Must(err)
if !favicon.IsAbs() {
rootURL, _ := url.Parse(bm.URL)
bm.Meta.Favicon = rootURL.Scheme + "://" + strings.TrimRight(rootURL.Hostname(), "/") + "/" + strings.TrimLeft(favicon.String(), "/")
}
}

type Tag struct {
ID int64 `json:"id"`
Name string `json:"name" form:"name" binding:"required"`
Expand Down
11 changes: 6 additions & 5 deletions api/handlers/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ func AddBookmark(ctx *gin.Context) {
utils.Must(err)
defer statement.Close()

var res BookmarkRes
res.Bookmark = body.Bookmark
var bm BookmarkRes
bm.Bookmark = body.Bookmark
bm.NormalizeFavicon()

for tagIDs.Next() {
var tag DB.Tag
Expand All @@ -124,13 +125,13 @@ func AddBookmark(ctx *gin.Context) {
utils.Must(err)

fmt.Printf("%+v\n", tag)
res.Tags = append(res.Tags, tag)
bm.Tags = append(bm.Tags, tag)
}
err = tagIDs.Err()
utils.Must(err)

tx.Commit()
ctx.JSON(http.StatusOK, res)
ctx.JSON(http.StatusOK, bm)
}

const (
Expand Down Expand Up @@ -231,7 +232,7 @@ func GetBookmarks(ctx *gin.Context) {
bm.Tags = append(bm.Tags, tag)
}
}

bm.NormalizeFavicon()
bookmarks = append(bookmarks, bm)
}
err = rows.Err()
Expand Down

0 comments on commit af126e7

Please sign in to comment.