Skip to content

Commit

Permalink
rewrites BookmarkStore to write to DB instead of local file. changed …
Browse files Browse the repository at this point in the history
…Bookmarks struct to use BookmarkId instead of Id in order to disambiguate primary key
  • Loading branch information
kenliu committed Jan 1, 2024
1 parent f932989 commit 71db0b9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 46 deletions.
86 changes: 48 additions & 38 deletions data/database.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,82 @@
package data

import (
"encoding/json"
"io"
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
"os"
)

type Bookmark struct {
Id string
Title string
Url string
BookmarkId string
Title string
Url string
}

type BookmarkStore struct {
bookmarks map[string]Bookmark
conn *sql.DB
}

type DBConfig struct {
Username string
Password string
Host string
Port string
Database string
}

func Init() BookmarkStore {
return BookmarkStore{
bookmarks: make(map[string]Bookmark),
func CreateDBConfigFromEnv() DBConfig {
// populate an instance of config struct using environment variables
config := DBConfig{
Username: os.Getenv("DB_USERNAME"),
Password: os.Getenv("DB_PASSWORD"),
Host: os.Getenv("DB_HOST"),
Port: os.Getenv("DB_PORT"),
Database: os.Getenv("DB_NAME"),
}
return config
}

func (store BookmarkStore) InitStore(config DBConfig) error {
err := loadDB(&store.bookmarks)
func (store *BookmarkStore) InitStore(config DBConfig) error {
// create connection string in this format: "postgresql://username:password@hostname:port/dbname?sslmode=require"
connStr := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=require", config.Username, config.Password, config.Host, config.Port, config.Database)

// Open a database connection
db, err := sql.Open("postgres", connStr)
if err != nil {
panic(err)
log.Fatal("error connecting to the database: ", err)
}
return nil
// defer db.Close()

store.conn = db
fmt.Println("Successfully connected to the database")
return err
}

func (store BookmarkStore) StoreBookmark(bookmark Bookmark) error {
store.bookmarks[bookmark.Id] = bookmark
_, err := store.conn.Exec("INSERT INTO bookmarks (bookmark_id, title, url) VALUES ($1, $2, $3)",
bookmark.BookmarkId, bookmark.Title, bookmark.Url)

j, err := json.Marshal(store.bookmarks)
if err != nil {
panic(err)
}

_ = os.WriteFile("database.json", j, 0644)
return nil
}

func (store BookmarkStore) FindBookmark(id string) (bool, error) {
_, found := store.bookmarks[id]
if found {
return true, nil
} else {
return false, nil
log.Fatal("error inserting into the database: ", err)
}
}

func (store BookmarkStore) UpdatePostedBookmark() error {
return nil
}

func loadDB(db *map[string]Bookmark) error {
jsonFile, err := os.Open("database.json")
func (store BookmarkStore) FindBookmark(bookmarkId string) (bool, error) {
// query the database to see if the bookmark exists for the given bookmarkId
rows, err := store.conn.Query("SELECT id FROM bookmarks WHERE bookmark_id = $1", bookmarkId)
if err != nil {
panic(err)
return false, err
}
defer jsonFile.Close()
defer rows.Close()

byteValue, _ := io.ReadAll(jsonFile)
// iterate over the rows and return true if a row was found
for rows.Next() {
return true, nil
}

json.Unmarshal(byteValue, &db)
return nil
// return false if no row was found
return false, nil
}
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func main() {
}

// initialize the bookmark store
db := data.Init()
db.InitStore(data.DBConfig{})
//db := data.Init()
var db = data.BookmarkStore{}
db.InitStore(data.CreateDBConfigFromEnv())

postCount, err := postNewLinks(popular, db, mastodonCredentials, *dryRun)
if err != nil {
Expand Down Expand Up @@ -68,12 +69,13 @@ func buildMastodonCredentials() (MastodonCredentials, error) {
}

func postNewLinks(popular []*data.Bookmark, db data.BookmarkStore, mastodonCredentials MastodonCredentials, dryRun bool) (int, error) {
//TODO there should be an if statement here
log.Println("dryrun: not posting any new bookmarks")
var postCount int
for i := 0; i < len(popular); i++ {
found, err := db.FindBookmark(popular[i].Id)
found, err := db.FindBookmark(popular[i].BookmarkId)
if err != nil {
log.Println("error finding bookmark in store: " + popular[i].Id)
log.Println("error finding bookmark in store: " + popular[i].BookmarkId)
return postCount, err
}
if !found {
Expand Down Expand Up @@ -101,7 +103,7 @@ func fetchCurrentPinboardPopular() ([]*data.Bookmark, error) {

log.Println("current popular bookmarks: ")
for i := 0; i < len(popular); i++ {
log.Println(popular[i].Id)
log.Println(popular[i].BookmarkId)
log.Println(popular[i].Title)
log.Println(popular[i].Url)
log.Println()
Expand Down
6 changes: 3 additions & 3 deletions scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func ScrapePinboardPopular() ([]*data.Bookmark, error) {
//log.Println(title)
//log.Println(href)
bookmarks = append(bookmarks, &data.Bookmark{
Id: id,
Title: title,
Url: href,
BookmarkId: id,
Title: title,
Url: href,
})
})

Expand Down

0 comments on commit 71db0b9

Please sign in to comment.