Commit be61bc93 authored by fodorpatrik2000's avatar fodorpatrik2000 👻
Browse files

get payout, get balance

parent a6699f30
package db
import (
"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"
"gitlab.com/MikeTTh/env"
"strings"
"time"
)
const TopupID = "topup"
type Spend struct {
ID string `pg:"id,pk,default:gen_random_uuid()"`
SchAcc string `pg:"user_schacc"`
ProdID string `pg:"product_id"`
Amount int64 `pg:"amount,default:1"`
Date time.Time `pg:"date,default:now()"`
Notes string `pg:"notes"`
User *User `pg:"rel:has-one,join_fk:user_schacc"`
Product *Product `pg:"rel:has-one,join_fk:product_id"`
}
type Product struct {
ID string `pg:"id,pk"`
Name string `pg:"name"`
IconURI string `pg:"icon_uri"`
Price int64 `pg:"price,use_zero"`
Buyable bool `pg:"buyable,use_zero"`
ProductGroupsID string `pg:"product_group_id, notnull"`
ProductGroup *ProductGroup `pg:"rel:has-one,join_fk:product_group_id"`
}
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"`
Spends []*Spend `pg:"rel:has-many,join_fk:user_schacc"`
}
type Session struct {
Id string `pg:"id,pk,default:gen_random_uuid()"`
SchAcc string `pg:"user_schacc"`
DarkTheme bool `pg:"dark_theme,default:true"`
User *User `pg:"rel:has-one,join_fk:user_schacc"`
}
type Payout struct {
Id string `pg:"id,pk,default:gen_random_uuid()"`
ProdGroupID string `pg:"product_group_id, notnull"`
SchAcc string `pg:"user_schacc, notnull"`
Price int64 `pg:"price, notnull"`
Date time.Time `pg:"date,default:now(), notnull"`
ProductGroup *ProductGroup `pg:"rel:has-one,join_fk:product_group_id"`
User *User `pg:"rel:has-one,join_fk:user_schacc"`
}
type ProductGroup struct {
Id string `pg:"id,pk,default:gen_random_uuid(), unique"`
Name string `pg:"name"`
}
/*type ProductCategories struct {
Id string `pg:"id,pk,default:gen_random_uuid()"`
ProdID string `pg:"product_id"`
ProdGroupID string `pg:"product_group_id"`
Product *Product `pg:"rel:has-one,join_fk:product_id"`
ProductGroup *ProductGroup `pg:"rel:has-one,join_fk:product_group_id"`
}*/
var db = pg.Connect(&pg.Options{
Addr: func() string {
host := env.String("POSTGRES", "localhost")
if !strings.Contains(host, ":") {
host += ":5432"
}
return host
}(),
User: env.String("POSTGRES_USER", "postgres"),
Password: env.String("POSTGRES_PASS", "postgres"),
Database: env.String("POSTGRES_DB", "postgres"),
})
var spendsNow = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "becskasszasch",
Name: "products_bought",
Help: "Products bought",
}, []string{"product"})
func init() {
models := []interface{}{
(*Product)(nil),
(*User)(nil),
(*Spend)(nil),
(*Session)(nil),
(*ProductGroup)(nil),
/*(*ProductCategories)(nil),*/
(*Payout)(nil),
}
for _, model := range models {
err := db.Model(model).CreateTable(&orm.CreateTableOptions{
IfNotExists: true,
FKConstraints: true,
})
if err != nil {
panic(err)
}
}
// TODO: Auto-migrate: https://github.com/go-pg/pg/issues/728#issuecomment-433345074
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment