Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/user favorite tags #266

Merged
merged 5 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added backend support for user favorite tags
  • Loading branch information
chadweimer committed Jan 31, 2021
commit 39baa86f6cd4593650ec3725f51647a8e1e92a14
23 changes: 23 additions & 0 deletions db/user-sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"database/sql"
"errors"
"fmt"

"github.com/chadweimer/gomp/models"
"github.com/jmoiron/sqlx"
Expand Down Expand Up @@ -105,6 +106,12 @@ func (d *sqlUserDriver) ReadSettings(id int64) (*models.UserSettings, error) {
return nil, err
}

var tags []string
if err := d.Db.Select(&tags, "SELECT tag FROM app_user_favorite_tag WHERE user_id = $1 ORDER BY tag ASC", id); err != nil {
return nil, err
}
userSettings.FavoriteTags = tags

return userSettings, nil
}

Expand All @@ -123,6 +130,22 @@ func (d *sqlUserDriver) updateSettingstx(settings *models.UserSettings, tx *sqlx
return err
}

// Deleting and recreating seems inefficient. Maybe make this smarter.
_, err = tx.Exec(
"DELETE FROM app_user_favorite_tag WHERE user_id = $1",
settings.UserID)
if err != nil {
return fmt.Errorf("deleting favorite tags before updating on user: %v", err)
}
for _, tag := range settings.FavoriteTags {
_, err = tx.Exec(
"INSERT INTO app_user_favorite_tag (user_id, tag) VALUES ($1, $2)",
settings.UserID, tag)
if err != nil {
return fmt.Errorf("updating favorite tags on user: %v", err)
}
}

return nil
}

Expand Down
7 changes: 4 additions & 3 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ type User struct {

// UserSettings represents the settings for an individual user
type UserSettings struct {
UserID int64 `json:"userId" db:"user_id"`
HomeTitle *string `json:"homeTitle" db:"home_title"`
HomeImageURL *string `json:"homeImageUrl" db:"home_image_url"`
UserID int64 `json:"userId" db:"user_id"`
HomeTitle *string `json:"homeTitle" db:"home_title"`
HomeImageURL *string `json:"homeImageUrl" db:"home_image_url"`
FavoriteTags []string `json:"favoriteTags"`
}

// Scan implements the sql.Scanner interface
Expand Down