Skip to content
Snippets Groups Projects
Unverified Commit 6b5d1500 authored by Réthelyi Bálint's avatar Réthelyi Bálint :no_mouth:
Browse files

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.
parent 52300cf1
No related branches found
No related tags found
1 merge request!25Add API endpoints and middleware for user and product manipulation
......@@ -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"
......@@ -41,6 +42,7 @@ type User struct {
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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment