From 6b5d15001d1aaf999be016dd4de5ca1ca1d32219 Mon Sep 17 00:00:00 2001 From: blint <rethelyibalint@gmail.com> Date: Sun, 20 Aug 2023 20:50:36 +0200 Subject: [PATCH] Update User model and add additional database functions Extended the User model to include 'IsPultosch' field with a default value as 'false'. Several new utility functions were added to provide better interaction and control with the database such as GetDB(), GetAvailableProducts(), GetSpendsWithLimit(), and a user specific function called Load(). The logging mechanism was commented out as it was no longer necessary in the context of these changes. --- db/db.go | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/db/db.go b/db/db.go index d946fa3..16df09d 100644 --- a/db/db.go +++ b/db/db.go @@ -2,8 +2,9 @@ package db import ( "encoding/json" - "git.sch.bme.hu/disappointment-industries/becskasszasch/helpers" + //"git.sch.bme.hu/disappointment-industries/becskasszasch/helpers" "github.com/go-pg/pg/v10" + _ "github.com/go-pg/pg/v10" "github.com/go-pg/pg/v10/orm" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" @@ -37,10 +38,11 @@ type Product struct { } type User struct { - SchAcc string `pg:"schacc,pk"` - Name string `pg:"name"` - Money int64 `pg:"money,use_zero"` - IsAdmin bool `pg:"is_admin,default:false"` + SchAcc string `pg:"schacc,pk"` + Name string `pg:"name"` + Money int64 `pg:"money,use_zero"` + IsAdmin bool `pg:"is_admin,default:false"` + IsPultosch bool `pg:"is_pultosch,default:false"` Spends []*Spend `pg:"rel:has-many,join_fk:user_schacc"` } @@ -66,6 +68,10 @@ var db = pg.Connect(&pg.Options{ Database: env.String("POSTGRES_DB", "postgres"), }) +func GetDB() *pg.DB { + return db +} + var spendsNow = promauto.NewCounterVec(prometheus.CounterOpts{ Namespace: "becskasszasch", Name: "products_bought", @@ -227,6 +233,14 @@ func GetProducts() ([]*Product, error) { return products, e } +func GetAvailableProducts() ([]*Product, error) { + var products []*Product + + e := db.Model(&products).Where("Price > ? AND Buyable = ?", 0, true).Select() + + return products, e +} + /* select product_id,sum(amount*price) from spends @@ -271,6 +285,14 @@ func GetSpends(typ string) ([]*Spend, error) { return spends, e } +func GetSpendsWithLimit(limit int) ([]*Spend, error) { + var spends []*Spend + + e := db.Model(&spends).Relation("User").Relation("Product").Limit(limit).Order("date DESC").Select() + + return spends, e +} + func update(something interface{}) error { _, e := db.Model(something).Insert() if e != nil { @@ -298,6 +320,13 @@ func (p *Product) Load() error { return e } +func (u *User) Load() error { + + e := db.Model(u).WherePK().First() + + return e +} + func (sp *Spend) Save() error { p := &Product{ ID: sp.ProdID, @@ -307,12 +336,12 @@ func (sp *Spend) Save() error { return e } - action := "bought" + /*action := "bought" if p.Price < 0 { action = "uploaded" - } + }*/ - helpers.SpendLog.Printf("%s %s %d %s %dx%d=%d\n", sp.SchAcc, action, sp.Amount, sp.ProdID, sp.Amount, p.Price, sp.Amount*p.Price) + //helpers.SpendLog.Printf("%s %s %d %s %dx%d=%d\n", sp.SchAcc, action, sp.Amount, sp.ProdID, sp.Amount, p.Price, sp.Amount*p.Price) spendsNow.With(prometheus.Labels{"product": p.Name}).Inc() return update(sp) -- GitLab