all database switched to repository model. some other fixes and features

This commit is contained in:
2026-01-22 20:58:46 +03:00
parent 804d683f9e
commit 331f6854b6
14 changed files with 283 additions and 187 deletions

View File

@@ -4,7 +4,7 @@ import (
"context"
"database/sql"
"errors"
"kurumibot/database"
"kurumibot/laniakea"
"math"
"time"
@@ -51,42 +51,46 @@ type User struct {
Fraction *Fraction
}
func GetOrCreateUser(tgId int, name string) (*User, error) {
user, err := GetUser(tgId)
type UserRepository struct {
db *sqlx.DB
}
func NewUserRepository(db *laniakea.DatabaseContext) *UserRepository {
return &UserRepository{db: db.PostgresSQL}
}
func (rep *UserRepository) GetOrCreate(tgId int, name string) (*User, error) {
user, err := rep.GetById(tgId)
if errors.Is(err, sql.ErrNoRows) {
_, err = CreateUser(tgId, name)
if err != nil {
return nil, err
}
user, err = GetUser(tgId)
user, err = rep.Create(tgId, name)
}
return user, err
}
func CreateUser(id int, name string) (*User, error) {
func (rep *UserRepository) Create(id int, name string) (*User, error) {
user := new(User)
err := database.PostgresDatabase.Get(user, "INSERT INTO users (id, name) VALUES (?, ?) RETURNING *;", id, name)
err := rep.db.Get(user, "INSERT INTO users (id, name) VALUES (?, ?) RETURNING *;", id, name)
return user, err
}
func GetUser(telegramId int) (*User, error) {
func (rep *UserRepository) GetById(telegramId int) (*User, error) {
user := new(User)
err := sqlx.Transact(database.PostgresDatabase, func(ctx context.Context, db sqlx.Queryable) error {
err := database.PostgresDatabase.Get(user, "SELECT * FROM users WHERE id=$1;", telegramId)
err := sqlx.Transact(rep.db, func(ctx context.Context, db sqlx.Queryable) error {
err := rep.db.Get(user, "SELECT * FROM users WHERE id=$1;", telegramId)
if err != nil {
return err
}
user.Group = new(Group)
err = database.PostgresDatabase.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID)
err = rep.db.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID)
if err != nil {
return err
}
user.Work = new(Work)
err = database.PostgresDatabase.Get(user.Work, "SELECT * FROM works WHERE id=$1;", user.WorkID)
err = rep.db.Get(user.Work, "SELECT * FROM works WHERE id=$1;", user.WorkID)
if err != nil {
return err
}
shopRep := NewShopRepository(database.PostgresDatabase)
shopRep := NewShopRepository(rep.db)
if user.AutoID.Valid {
user.Auto, err = shopRep.GetAuto(user.AutoID.Int32)
if err != nil {
@@ -112,13 +116,13 @@ func GetUser(telegramId int) (*User, error) {
}
}
if user.PairID.Valid {
user.Pair, err = GetUser(int(user.PairID.Int64))
user.Pair, err = rep.GetById(int(user.PairID.Int64))
if err != nil {
return err
}
}
if user.FractionID.Valid {
fractionRep := NewFractionRepository(database.PostgresDatabase)
fractionRep := NewFractionRepository(rep.db)
user.Fraction, err = fractionRep.GetById(user.FractionID.Int32)
if err != nil {
return err
@@ -129,8 +133,8 @@ func GetUser(telegramId int) (*User, error) {
return user, err
}
func UpdateUser(user *User) (*User, error) {
_, err := database.PostgresDatabase.NamedExec(
func (rep *UserRepository) Update(user *User) (*User, error) {
_, err := rep.db.NamedExec(
`UPDATE users SET balance=:balance, name=:name, group_id=:group_id, level=:level, exp=:exp,
work_id=:work_id, work_time=:work_time, auto_id=:auto_id, business_id=:business_id,
maid_id=:maid_id, miner_id=:miner_id, income_time=:income_time, btc=:btc, invested=:invested,
@@ -143,7 +147,7 @@ func UpdateUser(user *User) (*User, error) {
if err != nil {
return nil, err
}
return GetUser(user.ID)
return rep.GetById(user.ID)
}
func GetAllUsers() ([]*User, error) {