Skip to content

Commit

Permalink
pico pro (picosh#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap authored Jan 25, 2024
1 parent bb9229a commit d812172
Show file tree
Hide file tree
Showing 17 changed files with 360 additions and 143 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ migrate:
$(DOCKER_CMD) exec -i $(DB_CONTAINER) psql -U $(PGUSER) -d $(PGDATABASE) < ./sql/migrations/20230326_add_feed_items.sql
$(DOCKER_CMD) exec -i $(DB_CONTAINER) psql -U $(PGUSER) -d $(PGDATABASE) < ./sql/migrations/20230707_add_projects_table.sql
$(DOCKER_CMD) exec -i $(DB_CONTAINER) psql -U $(PGUSER) -d $(PGDATABASE) < ./sql/migrations/20230921_add_tokens_table.sql
$(DOCKER_CMD) exec -i $(DB_CONTAINER) psql -U $(PGUSER) -d $(PGDATABASE) < ./sql/migrations/20240120_add_payment_history.sql
.PHONY: migrate

latest:
$(DOCKER_CMD) exec -i $(DB_CONTAINER) psql -U $(PGUSER) -d $(PGDATABASE) < ./sql/migrations/20230921_add_tokens_table.sql
$(DOCKER_CMD) exec -i $(DB_CONTAINER) psql -U $(PGUSER) -d $(PGDATABASE) < ./sql/migrations/20240120_add_payment_history.sql
.PHONY: latest

psql:
Expand Down
51 changes: 50 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (p *FeedItemData) Scan(value interface{}) error {
if !ok {
return errors.New("type assertion to []byte failed")
}

return json.Unmarshal(b, &p)
}

Expand Down Expand Up @@ -148,6 +147,55 @@ type Token struct {
ExpiresAt *time.Time
}

type FeatureFlag struct {
ID string
UserID string
PaymentHistoryID string
Name string
CreatedAt *time.Time
ExpiresAt *time.Time
Data FeatureFlagData
}

func NewFeatureFlag(userID, name string, storageMax uint64, fileMax int64) *FeatureFlag {
return &FeatureFlag{
UserID: userID,
Name: name,
Data: FeatureFlagData{
StorageMax: storageMax,
FileMax: fileMax,
},
}
}

func (ff *FeatureFlag) IsValid() bool {
if ff.ExpiresAt.IsZero() {
return false
}
return ff.ExpiresAt.After(time.Now())
}

type FeatureFlagData struct {
StorageMax uint64 `json:"storage_max"`
FileMax int64 `json:"file_max"`
}

// Make the Attrs struct implement the driver.Valuer interface. This method
// simply returns the JSON-encoded representation of the struct.
func (p FeatureFlagData) Value() (driver.Value, error) {
return json.Marshal(p)
}

// Make the Attrs struct implement the sql.Scanner interface. This method
// simply decodes a JSON-encoded value into the struct fields.
func (p *FeatureFlagData) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}
return json.Unmarshal(b, &p)
}

type ErrMultiplePublicKeys struct{}

func (m *ErrMultiplePublicKeys) Error() string {
Expand Down Expand Up @@ -216,6 +264,7 @@ type DB interface {

AddViewCount(postID string) (int, error)

FindFeatureForUser(userID string, feature string) (*FeatureFlag, error)
HasFeatureForUser(userID string, feature string) bool
FindTotalSizeForUser(userID string) (int, error)

Expand Down
30 changes: 26 additions & 4 deletions db/postgres/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const (
sqlSelectTotalPostsAfterDate = `SELECT count(id) FROM posts WHERE created_at >= $1 AND cur_space = $2`
sqlSelectUsersWithPost = `SELECT count(app_users.id) FROM app_users WHERE EXISTS (SELECT 1 FROM posts WHERE user_id = app_users.id AND cur_space = $1);`

sqlSelectFeatureForUser = `SELECT id FROM feature_flags WHERE user_id = $1 AND name = $2`
sqlSelectFeatureForUser = `SELECT id, user_id, payment_history_id, name, data, created_at, expires_at FROM feature_flags WHERE user_id = $1 AND name = $2 ORDER BY expires_at DESC LIMIT 1`
sqlSelectSizeForUser = `SELECT COALESCE(sum(file_size), 0) FROM posts WHERE user_id = $1`

sqlSelectPostIdByAliasSlug = `SELECT post_id FROM post_aliases WHERE slug = $1`
Expand Down Expand Up @@ -1138,13 +1138,35 @@ func (me *PsqlDB) FindTagsForPost(postID string) ([]string, error) {
return tags, nil
}

func (me *PsqlDB) FindFeatureForUser(userID string, feature string) (*db.FeatureFlag, error) {
ff := &db.FeatureFlag{}
// payment history is allowed to be null
// https://devtidbits.com/2020/08/03/go-sql-error-converting-null-to-string-is-unsupported/
var paymentHistoryID sql.NullString
err := me.Db.QueryRow(sqlSelectFeatureForUser, userID, feature).Scan(
&ff.ID,
&ff.UserID,
&paymentHistoryID,
&ff.Name,
&ff.Data,
&ff.CreatedAt,
&ff.ExpiresAt,
)
if err != nil {
return nil, err
}

ff.PaymentHistoryID = paymentHistoryID.String

return ff, nil
}

func (me *PsqlDB) HasFeatureForUser(userID string, feature string) bool {
var id string
err := me.Db.QueryRow(sqlSelectFeatureForUser, userID, feature).Scan(&id)
ff, err := me.FindFeatureForUser(userID, feature)
if err != nil {
return false
}
return id != ""
return ff.IsValid()
}

func (me *PsqlDB) FindTotalSizeForUser(userID string) (int, error) {
Expand Down
95 changes: 0 additions & 95 deletions filehandlers/assets/asset.go

This file was deleted.

Loading

0 comments on commit d812172

Please sign in to comment.