Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from vshn/facts-updatable
Browse files Browse the repository at this point in the history
Make fact object updatable in database
  • Loading branch information
zugao authored Dec 1, 2022
2 parents a71bcfa + c9bb703 commit 9e99e29
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/factsmodel/facts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/appuio/appuio-cloud-reporting/pkg/db"
"github.com/jmoiron/sqlx"
"reflect"
)

func GetByFact(ctx context.Context, tx *sqlx.Tx, fact *db.Fact) (*db.Fact, error) {
Expand All @@ -31,6 +32,15 @@ func Ensure(ctx context.Context, tx *sqlx.Tx, ensureFact *db.Fact) (*db.Fact, er
if err != nil {
return nil, err
}
} else {
ensureFact.Id = fact.Id
if !reflect.DeepEqual(fact, ensureFact) {
fmt.Printf("updating facts\n")
err = Update(tx, ensureFact)
if err != nil {
return nil, err
}
}
}
return fact, nil
}
Expand All @@ -45,6 +55,16 @@ func Create(p db.NamedPreparer, in *db.Fact) (*db.Fact, error) {
return &category, err
}

func Update(p db.NamedPreparer, in *db.Fact) error {
var fact db.Fact
err := db.GetNamed(p, &fact,
"UPDATE facts SET date_time_id=:date_time_id, query_id=:query_id, tenant_id=:tenant_id, category_id=:category_id, product_id=:product_id, discount_id=:discount_id, quantity=:quantity WHERE id=:id RETURNING *", in)
if err != nil {
err = fmt.Errorf("cannot update fact %v: %w", in, err)
}
return err
}

func New(dateTime *db.DateTime, query *db.Query, tenant *db.Tenant, category *db.Category, product *db.Product, discount *db.Discount, quanity float64) *db.Fact {
return &db.Fact{
DateTimeId: dateTime.Id,
Expand Down

0 comments on commit 9e99e29

Please sign in to comment.