Skip to content

Commit

Permalink
update: remodel tags
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijit-hota committed May 10, 2022
1 parent 1d29f98 commit ae30154
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 26 deletions.
15 changes: 5 additions & 10 deletions api/db/bookmark.model.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package db

type Tag struct {
Path string `json:"path" binding:"required"`
IsRoot bool `json:"isRoot"`
}

type Meta struct {
Title string `json:"title"`
Description string `json:"description"`
Favicon string `json:"favicon"`
}

type Bookmark struct {
Meta Meta `json:"meta"`
URL string `json:"url" binding:"required"`
Created int64 `json:"created"`
LastUpdated int64 `json:"last_updated"`
Tags []Tag `json:"tags"`
Meta Meta `json:"meta"`
URL string `json:"url" binding:"required"`
Created int64 `json:"created"`
LastUpdated int64 `json:"last_updated"`
Tags []string `json:"tags"`
}
28 changes: 12 additions & 16 deletions api/handlers/save_bookmark.go → api/handlers/add_bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bingo/api/common"
DB "bingo/api/db"
"bingo/api/utils"
"database/sql"
"fmt"
"log"
"net/http"
Expand All @@ -14,8 +13,6 @@ import (
"github.com/gin-gonic/gin"
)

var db *sql.DB = DB.GetDB()

var config utils.Config

func init() {
Expand All @@ -24,16 +21,16 @@ func init() {

func AddBookmark(ctx *gin.Context) {
var json DB.Bookmark
db := DB.GetDB()

if err := ctx.ShouldBindJSON(&json); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if config.AutofillURLData {
if err := common.GetMetadata(json.URL, &json.Meta); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// hasAutotagRule = utils.Contains[]() config.AutoTagRules
if err := common.GetMetadata(json.URL, &json.Meta); err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

tx, err := db.Begin()
Expand All @@ -52,7 +49,7 @@ func AddBookmark(ctx *gin.Context) {
return
}

statement, err = tx.Prepare("INSERT INTO bookmarks (url, meta_id, created, last_updated) VALUES (?, ?, ?, ?)")
statement, err = tx.Prepare("INSERT INTO links (url, meta_id, created, last_updated) VALUES (?, ?, ?, ?)")
utils.Must(err)
defer statement.Close()

Expand All @@ -70,7 +67,7 @@ func AddBookmark(ctx *gin.Context) {
return
}

bookmarkID, _ := info.LastInsertId()
linkID, _ := info.LastInsertId()

statement, err = tx.Prepare("INSERT OR IGNORE INTO tags (path, created, last_updated, is_root) VALUES (?, ?, ?, ?)")
utils.Must(err)
Expand All @@ -79,25 +76,24 @@ func AddBookmark(ctx *gin.Context) {
tags := make([]any, len(json.Tags))

for i, tag := range json.Tags {
isRoot := !strings.ContainsRune(tag.Path, '/')
json.Tags[i].IsRoot = isRoot
statement.Exec(tag.Path, now, now, isRoot)
tags[i] = tag.Path
isRoot := !strings.ContainsRune(tag, '/')
statement.Exec(tag, now, now, isRoot)
tags[i] = tag
}

query := fmt.Sprintf("SELECT id FROM tags WHERE path IN (%s)", strings.TrimRight(strings.Repeat("?,", len(tags)), ","))
tagIDs, err := tx.Query(query, tags...)
defer tagIDs.Close()
utils.Must(err)

statement, err = tx.Prepare("INSERT INTO bookmarks_tags (tag_id, bookmark_id) VALUES (?, ?)")
statement, err = tx.Prepare("INSERT INTO links_tags (tag_id, link_id) VALUES (?, ?)")
utils.Must(err)
defer statement.Close()

for tagIDs.Next() {
var tagID int
tagIDs.Scan(&tagID)
statement.Exec(tagID, bookmarkID)
statement.Exec(tagID, linkID)
}
err = tagIDs.Err()
utils.Must(err)
Expand Down

0 comments on commit ae30154

Please sign in to comment.