From 022cf4ba061a3204080fde1cecc337cf7023b3df Mon Sep 17 00:00:00 2001 From: ScuroNeko Date: Thu, 12 Feb 2026 15:09:06 +0300 Subject: [PATCH] database changes --- database/mdb/ai_chats.go | 4 +- database/mdb/code.go | 4 +- database/mdb/logs.go | 6 +- database/mdb/rp_chats.go | 6 +- database/psql/ai.go | 16 +++--- database/psql/fraction.go | 18 +++--- database/psql/groups.go | 17 ++++-- database/psql/rp.go | 79 ++++++++++++++------------ database/psql/shop.go | 46 +++++++-------- database/psql/users.go | 114 ++++++++++++-------------------------- database/psql/waifus.go | 41 +++++++------- database/psql/works.go | 17 +++--- database/red/ai_chats.go | 10 ++-- database/red/rp_chats.go | 34 ++++++------ plugins/economy.go | 2 +- plugins/logs.go | 2 +- plugins/rp.go | 6 +- plugins/waifus.go | 3 +- 18 files changed, 202 insertions(+), 223 deletions(-) diff --git a/database/mdb/ai_chats.go b/database/mdb/ai_chats.go index 1a4a789..66c4df2 100644 --- a/database/mdb/ai_chats.go +++ b/database/mdb/ai_chats.go @@ -9,7 +9,7 @@ import ( "go.mongodb.org/mongo-driver/v2/bson" ) -func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMessage, error) { +func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMessage, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() col := database.GetMongoCollection(db, "gpt_chat_messages") @@ -17,7 +17,7 @@ func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMe if err != nil { return nil, err } - result := make([]*AiChatMessage, 0) + result := make([]AiChatMessage, 0) err = cursor.All(ctx, &result) return result, err } diff --git a/database/mdb/code.go b/database/mdb/code.go index bfb4357..8dd50ba 100644 --- a/database/mdb/code.go +++ b/database/mdb/code.go @@ -22,12 +22,12 @@ type CodeEntry struct { TotalUsages uint `bson:"totalUsages"` } -func FindCode(db *laniakea.DatabaseContext, code string) (*CodeEntry, error) { +func FindCode(db *laniakea.DatabaseContext, code string) (CodeEntry, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() collection := database.GetMongoCollection(db, "codes") res := collection.FindOne(ctx, bson.M{"code": code}) - entry := new(CodeEntry) + entry := CodeEntry{} err := res.Decode(entry) return entry, err } diff --git a/database/mdb/logs.go b/database/mdb/logs.go index e0e6cfd..d7339e3 100644 --- a/database/mdb/logs.go +++ b/database/mdb/logs.go @@ -20,14 +20,14 @@ type ConsoleLogEntry struct { TimeStamp int64 `bson:"timeStamp" json:"time_stamp"` } -func WriteConsoleLog(db *laniakea.DatabaseContext, e *ConsoleLogEntry) error { +func WriteConsoleLog(db *laniakea.DatabaseContext, e ConsoleLogEntry) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() col := database.GetMongoCollection(db, "logs") _, err := col.InsertOne(ctx, e) return err } -func GetConsoleLogs(db *laniakea.DatabaseContext) (extypes.Slice[*ConsoleLogEntry], error) { +func GetConsoleLogs(db *laniakea.DatabaseContext) ([]ConsoleLogEntry, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() col := database.GetMongoCollection(db, "logs") @@ -39,7 +39,7 @@ func GetConsoleLogs(db *laniakea.DatabaseContext) (extypes.Slice[*ConsoleLogEntr return nil, err } defer cursor.Close(ctx) - result := make(extypes.Slice[*ConsoleLogEntry], 0) + result := make([]ConsoleLogEntry, 0) err = cursor.All(ctx, &result) return result, nil } diff --git a/database/mdb/rp_chats.go b/database/mdb/rp_chats.go index be146a4..fd15626 100644 --- a/database/mdb/rp_chats.go +++ b/database/mdb/rp_chats.go @@ -17,7 +17,7 @@ type AiChatMessage struct { Index int `bson:"index"` } -func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMessage, error) { +func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMessage, error) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() col := database.GetMongoCollection(db, "rp_chat_messages") @@ -25,7 +25,7 @@ func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]*AiChatMes if err != nil { return nil, err } - result := make([]*AiChatMessage, 0) + result := make([]AiChatMessage, 0) err = cursor.All(ctx, &result) return result, err } @@ -47,7 +47,7 @@ func GetRPChatHistorySize(db *laniakea.DatabaseContext, chatId string) (int64, e col := database.GetMongoCollection(db, "rp_chat_messages") return col.CountDocuments(ctx, bson.M{"chatId": chatId}) } -func DeleteRPChatEntry(db *laniakea.DatabaseContext, entry *AiChatMessage) error { +func DeleteRPChatEntry(db *laniakea.DatabaseContext, entry AiChatMessage) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() col := database.GetMongoCollection(db, "rp_chat_messages") diff --git a/database/psql/ai.go b/database/psql/ai.go index 703b591..de7b1a7 100644 --- a/database/psql/ai.go +++ b/database/psql/ai.go @@ -16,20 +16,20 @@ type AIRepository struct { db *sqlx.DB } -func newAiRepository(db *sqlx.DB) *AIRepository { - return &AIRepository{db} +func newAiRepository(db *sqlx.DB) AIRepository { + return AIRepository{db} } -func NewAIRepository(db *laniakea.DatabaseContext) *AIRepository { +func NewAIRepository(db *laniakea.DatabaseContext) AIRepository { return newAiRepository(db.PostgresSQL) } -func (rep *AIRepository) GetModel(id string) (*AIModel, error) { - model := new(AIModel) - err := rep.db.Get(model, "SELECT * FROM ai_models WHERE id=$1;", id) +func (rep AIRepository) GetModel(id string) (AIModel, error) { + model := AIModel{} + err := rep.db.Get(&model, "SELECT * FROM ai_models WHERE id=$1;", id) return model, err } -func (rep *AIRepository) GetAllModels() ([]*AIModel, error) { - models := make([]*AIModel, 0) +func (rep AIRepository) GetAllModels() ([]AIModel, error) { + models := make([]AIModel, 0) err := rep.db.Select(&models, "SELECT * FROM ai_models ORDER BY id;") return models, err } diff --git a/database/psql/fraction.go b/database/psql/fraction.go index 95b54bd..fc00e36 100644 --- a/database/psql/fraction.go +++ b/database/psql/fraction.go @@ -1,6 +1,7 @@ package psql import ( + "git.nix13.pw/scuroneko/laniakea" "github.com/shopspring/decimal" "github.com/vinovest/sqlx" ) @@ -18,17 +19,20 @@ type FractionRepository struct { db *sqlx.DB } -func NewFractionRepository(db *sqlx.DB) *FractionRepository { - return &FractionRepository{db: db} +func newFractionRepository(db *sqlx.DB) FractionRepository { + return FractionRepository{db} +} +func NewFractionRepository(db *laniakea.DatabaseContext) FractionRepository { + return newFractionRepository(db.PostgresSQL) } -func (rep *FractionRepository) GetAll() ([]*Fraction, error) { - fractions := make([]*Fraction, 0) +func (rep FractionRepository) GetAll() ([]Fraction, error) { + fractions := make([]Fraction, 0) err := rep.db.Select(&fractions, "SELECT * FROM fractions ORDER BY id DESC;") return fractions, err } -func (rep *FractionRepository) GetById(id int32) (*Fraction, error) { - fraction := new(Fraction) - err := rep.db.Get(fraction, "SELECT * FROM fractions WHERE id = $1", id) +func (rep FractionRepository) GetById(id int32) (Fraction, error) { + fraction := Fraction{} + err := rep.db.Get(&fraction, "SELECT * FROM fractions WHERE id = $1", id) return fraction, err } diff --git a/database/psql/groups.go b/database/psql/groups.go index 70112e6..2ec1fe0 100644 --- a/database/psql/groups.go +++ b/database/psql/groups.go @@ -1,6 +1,7 @@ package psql import ( + "git.nix13.pw/scuroneko/laniakea" "github.com/shopspring/decimal" "github.com/vinovest/sqlx" ) @@ -21,15 +22,19 @@ type GroupRepository struct { db *sqlx.DB } -func NewGroupRepository(db *sqlx.DB) *GroupRepository { - return &GroupRepository{db: db} +func newGroupRepository(db *sqlx.DB) GroupRepository { + return GroupRepository{db} } -func (rep *GroupRepository) GetAll() ([]*Group, error) { - groups := make([]*Group, 0) +func NewGroupRepository(db *laniakea.DatabaseContext) GroupRepository { + return newGroupRepository(db.PostgresSQL) +} + +func (rep GroupRepository) GetAll() ([]Group, error) { + groups := make([]Group, 0) err := rep.db.Select(&groups, "SELECT * FROM groups ORDER BY id DESC;") return groups, err } -func (rep *GroupRepository) GetById(id int) (*Group, error) { +func (rep GroupRepository) GetById(id int) (Group, error) { group, err := sqlx.One[Group](rep.db, "SELECT * FROM groups WHERE id = $1", id) - return &group, err + return group, err } diff --git a/database/psql/rp.go b/database/psql/rp.go index 49feb55..4d490e9 100644 --- a/database/psql/rp.go +++ b/database/psql/rp.go @@ -45,46 +45,57 @@ type RPRepository struct { db *sqlx.DB } -func NewRPRepository(db *laniakea.DatabaseContext) *RPRepository { - return &RPRepository{db.PostgresSQL} +func newRpRepository(db *sqlx.DB) RPRepository { + return RPRepository{db} +} +func NewRPRepository(db *laniakea.DatabaseContext) RPRepository { + return newRpRepository(db.PostgresSQL) } -func (rep *RPRepository) GetOrCreateUser(id int64) (*RPUser, error) { +func (rep RPRepository) GetOrCreateUser(id int64) (RPUser, error) { user, err := rep.GetUser(id) if errors.Is(err, sql.ErrNoRows) { return rep.CreateUser(id) } return user, err } -func (rep *RPRepository) CreateUser(id int64) (*RPUser, error) { - user := new(RPUser) - err := rep.db.Get(user, "INSERT INTO rp_users(user_id) VALUES ($1) RETURNING *;", id) +func (rep RPRepository) CreateUser(id int64) (RPUser, error) { + user := RPUser{} + err := rep.db.Get(&user, "INSERT INTO rp_users(user_id) VALUES ($1) RETURNING *;", id) if err != nil { return user, err } - user.Preset, err = rep.GetPreset(user.SelectedPreset) + preset, err := rep.GetPreset(user.SelectedPreset) if err != nil { return user, err } aiRep := newAiRepository(rep.db) - user.Model, err = aiRep.GetModel(user.SelectedModel) - return user, err -} -func (rep *RPRepository) GetUser(id int64) (*RPUser, error) { - user := new(RPUser) - err := rep.db.Get(user, "SELECT * FROM rp_users WHERE user_id=$1", id) + model, err := aiRep.GetModel(user.SelectedModel) if err != nil { return user, err } - user.Preset, err = rep.GetPreset(user.SelectedPreset) + + user.Preset = &preset + user.Model = &model + return user, err +} +func (rep RPRepository) GetUser(id int64) (RPUser, error) { + user := RPUser{} + err := rep.db.Get(&user, "SELECT * FROM rp_users WHERE user_id=$1", id) + if err != nil { + return user, err + } + preset, err := rep.GetPreset(user.SelectedPreset) if err != nil { return user, err } aiRep := newAiRepository(rep.db) - user.Model, err = aiRep.GetModel(user.SelectedModel) + model, err := aiRep.GetModel(user.SelectedModel) + user.Model = &model + user.Preset = &preset return user, err } -func (rep *RPRepository) UpdateUser(user *RPUser) error { +func (rep RPRepository) UpdateUser(user RPUser) error { s, args, err := sqlx.In( "UPDATE rp_users SET selected_preset=?, used_tokens=?, user_prompt=?, selected_model=? WHERE user_id=?;", user.SelectedPreset, user.UsedTokens, user.UserPrompt, user.SelectedModel, user.UserID, @@ -96,7 +107,7 @@ func (rep *RPRepository) UpdateUser(user *RPUser) error { _, err = rep.db.Exec(s, args...) return err } -func (rep *RPRepository) UpdateUserPreset(user *RPUser, presetId string) (*RPPreset, error) { +func (rep RPRepository) UpdateUserPreset(user RPUser, presetId string) (RPPreset, error) { preset, err := rep.GetPreset(presetId) if err != nil { return preset, err @@ -112,7 +123,7 @@ func (rep *RPRepository) UpdateUserPreset(user *RPUser, presetId string) (*RPPre _, err = rep.db.Exec(s, args...) return preset, err } -func (rep *RPRepository) GetUserPreset(user *RPUser) (*RPPreset, error) { +func (rep RPRepository) GetUserPreset(user RPUser) (RPPreset, error) { preset, err := rep.GetPreset(user.SelectedPreset) if errors.Is(err, sql.ErrNoRows) { return rep.UpdateUserPreset(user, "soft") @@ -120,40 +131,40 @@ func (rep *RPRepository) GetUserPreset(user *RPUser) (*RPPreset, error) { return preset, err } -func (rep *RPRepository) GetAllPresets() ([]*RPPreset, error) { - presets := make([]*RPPreset, 0) +func (rep RPRepository) GetAllPresets() ([]RPPreset, error) { + presets := make([]RPPreset, 0) err := rep.db.Select(&presets, "SELECT * FROM rp_presets ORDER BY id;") return presets, err } -func (rep *RPRepository) GetPreset(id string) (*RPPreset, error) { - preset := new(RPPreset) - err := rep.db.Get(preset, "SELECT * FROM rp_presets WHERE id=$1;", id) +func (rep RPRepository) GetPreset(id string) (RPPreset, error) { + preset := RPPreset{} + err := rep.db.Get(&preset, "SELECT * FROM rp_presets WHERE id=$1;", id) return preset, err } -func (rep *RPRepository) GetAllScenarios() ([]*RPScenario, error) { - scenarios := make([]*RPScenario, 0) +func (rep RPRepository) GetAllScenarios() ([]RPScenario, error) { + scenarios := make([]RPScenario, 0) err := rep.db.Select(&scenarios, "SELECT * FROM rp_scenarios ORDER BY id;") return scenarios, err } -func (rep *RPRepository) GetScenario(id int) (*RPScenario, error) { - scenario := new(RPScenario) - err := rep.db.Get(scenario, "SELECT * FROM rp_scenarios WHERE id=$1;", id) +func (rep RPRepository) GetScenario(id int) (RPScenario, error) { + scenario := RPScenario{} + err := rep.db.Get(&scenario, "SELECT * FROM rp_scenarios WHERE id=$1;", id) return scenario, err } -func (rep *RPRepository) GetAllSettings() ([]*RPSetting, error) { - settings := make([]*RPSetting, 0) +func (rep RPRepository) GetAllSettings() ([]RPSetting, error) { + settings := make([]RPSetting, 0) err := rep.db.Select(&settings, "SELECT * FROM rp_settings ORDER BY id;") return settings, err } -func (rep *RPRepository) GetSetting(id int) (*RPSetting, error) { - setting := new(RPSetting) - err := rep.db.Get(setting, "SELECT * FROM rp_settings WHERE id=$1;", id) +func (rep RPRepository) GetSetting(id int) (RPSetting, error) { + setting := RPSetting{} + err := rep.db.Get(&setting, "SELECT * FROM rp_settings WHERE id=$1;", id) return setting, err } -func (rep *RPRepository) UpdateUserCompressSettings(user *RPUser) (*RPUser, error) { +func (rep RPRepository) UpdateUserCompressSettings(user RPUser) (RPUser, error) { query, args, err := sqlx.In( "UPDATE rp_users SET compress_method=?, compress_limit=? WHERE user_id=?;", user.CompressMethod, user.CompressLimit, user.UserID, diff --git a/database/psql/shop.go b/database/psql/shop.go index 7be1ff3..7f13b1d 100644 --- a/database/psql/shop.go +++ b/database/psql/shop.go @@ -1,6 +1,7 @@ package psql import ( + "git.nix13.pw/scuroneko/laniakea" "github.com/shopspring/decimal" "github.com/vinovest/sqlx" ) @@ -36,47 +37,48 @@ type ShopRepository struct { db *sqlx.DB } -func NewShopRepository(db *sqlx.DB) *ShopRepository { - return &ShopRepository{db: db} +func NewShopRepository(db *laniakea.DatabaseContext) ShopRepository { + return newShopRepository(db.PostgresSQL) } +func newShopRepository(db *sqlx.DB) ShopRepository { return ShopRepository{db} } -func (rep *ShopRepository) GetAllAuto() ([]*ShopAuto, error) { - auto := make([]*ShopAuto, 0) +func (rep ShopRepository) GetAllAuto() ([]ShopAuto, error) { + auto := make([]ShopAuto, 0) err := rep.db.Select(&auto, "SELECT * FROM shop_auto ORDER BY id DESC;") return auto, err } -func (rep *ShopRepository) GetAuto(id int32) (*ShopAuto, error) { - auto := new(ShopAuto) - err := rep.db.Get(auto, "SELECT * FROM shop_auto WHERE id = $1;", id) +func (rep ShopRepository) GetAuto(id int32) (ShopAuto, error) { + auto := ShopAuto{} + err := rep.db.Get(&auto, "SELECT * FROM shop_auto WHERE id = $1;", id) return auto, err } -func (rep *ShopRepository) GetAllBusinesses() ([]*ShopBusiness, error) { - businesses := make([]*ShopBusiness, 0) +func (rep ShopRepository) GetAllBusinesses() ([]ShopBusiness, error) { + businesses := make([]ShopBusiness, 0) err := rep.db.Select(&businesses, "SELECT * FROM shop_business ORDER BY id DESC;") return businesses, err } -func (rep *ShopRepository) GetBusiness(id int32) (*ShopBusiness, error) { - business := new(ShopBusiness) - err := rep.db.Get(business, "SELECT * FROM shop_business WHERE id = $1;", id) +func (rep ShopRepository) GetBusiness(id int32) (ShopBusiness, error) { + business := ShopBusiness{} + err := rep.db.Get(&business, "SELECT * FROM shop_business WHERE id = $1;", id) return business, err } -func (rep *ShopRepository) GetAllMaids() ([]*ShopMaid, error) { - maids := make([]*ShopMaid, 0) +func (rep ShopRepository) GetAllMaids() ([]ShopMaid, error) { + maids := make([]ShopMaid, 0) err := rep.db.Select(&maids, "SELECT * FROM shop_maid ORDER BY id DESC;") return maids, err } -func (rep *ShopRepository) GetMaid(id int32) (*ShopMaid, error) { - maid := new(ShopMaid) - err := rep.db.Get(maid, "SELECT * FROM shop_maid WHERE id = $1;", id) +func (rep ShopRepository) GetMaid(id int32) (ShopMaid, error) { + maid := ShopMaid{} + err := rep.db.Get(&maid, "SELECT * FROM shop_maid WHERE id = $1;", id) return maid, err } -func (rep *ShopRepository) GetAllMiners() ([]*ShopMiner, error) { - miners := make([]*ShopMiner, 0) +func (rep ShopRepository) GetAllMiners() ([]ShopMiner, error) { + miners := make([]ShopMiner, 0) err := rep.db.Select(&miners, "SELECT * FROM shop_miner ORDER BY id DESC;") return miners, err } -func (rep *ShopRepository) GetMiner(id int32) (*ShopMiner, error) { - miner := new(ShopMiner) - err := rep.db.Get(miner, "SELECT * FROM shop_miner WHERE id = $1;", id) +func (rep ShopRepository) GetMiner(id int32) (ShopMiner, error) { + miner := ShopMiner{} + err := rep.db.Get(&miner, "SELECT * FROM shop_miner WHERE id = $1;", id) return miner, err } diff --git a/database/psql/users.go b/database/psql/users.go index f488050..f8d61aa 100644 --- a/database/psql/users.go +++ b/database/psql/users.go @@ -55,11 +55,12 @@ type UserRepository struct { db *sqlx.DB } -func NewUserRepository(db *laniakea.DatabaseContext) *UserRepository { - return &UserRepository{db: db.PostgresSQL} +func newUserRepository(db *sqlx.DB) UserRepository { return UserRepository{db} } +func NewUserRepository(db *laniakea.DatabaseContext) UserRepository { + return newUserRepository(db.PostgresSQL) } -func (rep *UserRepository) GetOrCreate(tgId int, name string) (*User, error) { +func (rep UserRepository) GetOrCreate(tgId int, name string) (*User, error) { user, err := rep.GetById(tgId) if errors.Is(err, sql.ErrNoRows) { user, err = rep.Create(tgId, name) @@ -67,73 +68,22 @@ func (rep *UserRepository) GetOrCreate(tgId int, name string) (*User, error) { return user, err } -func (rep *UserRepository) Create(id int, name string) (*User, error) { - user := new(User) +func (rep UserRepository) Create(id int, name string) (*User, error) { + user := User{} err := rep.db.Get(user, "INSERT INTO users (id, name) VALUES ($1, $2) RETURNING *;", id, name) - return user, err + return &user, err } -func (rep *UserRepository) GetById(telegramId int) (*User, error) { - user := new(User) - 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 = rep.db.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID) - if err != nil { - return err - } - user.Work = new(Work) - err = rep.db.Get(user.Work, "SELECT * FROM works WHERE id=$1;", user.WorkID) - if err != nil { - return err - } - shopRep := NewShopRepository(rep.db) - if user.AutoID.Valid { - user.Auto, err = shopRep.GetAuto(user.AutoID.Int32) - if err != nil { - return err - } - } - if user.BusinessID.Valid { - user.Business, err = shopRep.GetBusiness(user.BusinessID.Int32) - if err != nil { - return err - } - } - if user.MaidID.Valid { - user.Maid, err = shopRep.GetMaid(user.MaidID.Int32) - if err != nil { - return err - } - } - if user.MinerID.Valid { - user.Miner, err = shopRep.GetMiner(user.MinerID.Int32) - if err != nil { - return err - } - } - if user.PairID.Valid { - user.Pair, err = rep.GetById(int(user.PairID.Int64)) - if err != nil { - return err - } - } - if user.FractionID.Valid { - fractionRep := NewFractionRepository(rep.db) - user.Fraction, err = fractionRep.GetById(user.FractionID.Int32) - if err != nil { - return err - } - } - return nil - }) - return user, err +func (rep UserRepository) GetById(telegramId int) (*User, error) { + user := User{} + err := rep.db.Get(&user, "SELECT * FROM users WHERE id=$1;", telegramId) + if err != nil { + return &user, err + } + return rep.GetJoins(&user) } -func (rep *UserRepository) GetJoins(user *User) (*User, error) { +func (rep UserRepository) GetJoins(user *User) (*User, error) { err := sqlx.Transact(rep.db, func(ctx context.Context, db sqlx.Queryable) error { user.Group = new(Group) err := rep.db.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID) @@ -145,50 +95,56 @@ func (rep *UserRepository) GetJoins(user *User) (*User, error) { if err != nil { return err } - shopRep := NewShopRepository(rep.db) + shopRep := newShopRepository(rep.db) if user.AutoID.Valid { - user.Auto, err = shopRep.GetAuto(user.AutoID.Int32) + auto, err := shopRep.GetAuto(user.AutoID.Int32) if err != nil { return err } + user.Auto = &auto } if user.BusinessID.Valid { - user.Business, err = shopRep.GetBusiness(user.BusinessID.Int32) + business, err := shopRep.GetBusiness(user.BusinessID.Int32) if err != nil { return err } + user.Business = &business } if user.MaidID.Valid { - user.Maid, err = shopRep.GetMaid(user.MaidID.Int32) + maid, err := shopRep.GetMaid(user.MaidID.Int32) if err != nil { return err } + user.Maid = &maid } if user.MinerID.Valid { - user.Miner, err = shopRep.GetMiner(user.MinerID.Int32) + miner, err := shopRep.GetMiner(user.MinerID.Int32) if err != nil { return err } + user.Miner = &miner } if user.PairID.Valid { - user.Pair, err = rep.GetById(int(user.PairID.Int64)) + pair, err := rep.GetById(int(user.PairID.Int64)) if err != nil { return err } + user.Pair = pair } if user.FractionID.Valid { - fractionRep := NewFractionRepository(rep.db) - user.Fraction, err = fractionRep.GetById(user.FractionID.Int32) + fractionRep := newFractionRepository(rep.db) + fraction, err := fractionRep.GetById(user.FractionID.Int32) if err != nil { return err } + user.Fraction = &fraction } return nil }) return user, err } -func (rep *UserRepository) Update(user *User) (*User, error) { +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, @@ -205,20 +161,20 @@ func (rep *UserRepository) Update(user *User) (*User, error) { return rep.GetById(user.ID) } -func (rep *UserRepository) GetAll() ([]*User, error) { - users := make([]*User, 0) +func (rep UserRepository) GetAll() ([]User, error) { + users := make([]User, 0) err := rep.db.Select(&users, "SELECT * FROM users ORDER BY id;") if err != nil { return nil, err } - outUsers := make([]*User, len(users)) + outUsers := make([]User, len(users)) for i, user := range users { - u, err := rep.GetJoins(user) + u, err := rep.GetJoins(&user) if err != nil { return nil, err } - outUsers[i] = u + outUsers[i] = *u } return outUsers, nil diff --git a/database/psql/waifus.go b/database/psql/waifus.go index 1626be8..b17b344 100644 --- a/database/psql/waifus.go +++ b/database/psql/waifus.go @@ -3,7 +3,6 @@ package psql import ( "database/sql" - "git.nix13.pw/scuroneko/extypes" "git.nix13.pw/scuroneko/laniakea" "github.com/shopspring/decimal" "github.com/vinovest/sqlx" @@ -31,11 +30,9 @@ func NewWaifuRepository(db *laniakea.DatabaseContext) *WaifuRepository { return &WaifuRepository{db: db.PostgresSQL} } -func (rep *WaifuRepository) GetAll() (extypes.Slice[*Waifu], error) { - waifus, err := sqlx.List[*Waifu](rep.db, "SELECT waifus.* FROM waifus;") - if err != nil { - return nil, err - } +func (rep *WaifuRepository) GetAll() ([]Waifu, error) { + waifus, err := sqlx.List[Waifu](rep.db, "SELECT * FROM waifus;") + //userRep := newUserRepository(rep.db) for _, waifu := range waifus { if !waifu.OwnerID.Valid { @@ -47,18 +44,20 @@ func (rep *WaifuRepository) GetAll() (extypes.Slice[*Waifu], error) { return waifus, err } -func (rep *WaifuRepository) GetByUserId(userId int) ([]*Waifu, error) { - waifus, err := sqlx.List[*Waifu](rep.db, "SELECT waifus.* FROM waifus WHERE owner_id=$1;", userId) +func (rep *WaifuRepository) GetByUserId(userId int) ([]Waifu, error) { + waifus, err := sqlx.List[Waifu](rep.db, "SELECT waifus.* FROM waifus WHERE owner_id=$1;", userId) + if err != nil { + return nil, err + } + + userRep := newUserRepository(rep.db) + user, err := userRep.GetById(userId) if err != nil { return nil, err } for _, waifu := range waifus { - waifu.Owner = new(User) - err = rep.db.Get(waifu.Owner, "SELECT * FROM users WHERE id=$1;", waifu.OwnerID.Int64) - if err != nil { - return waifus, err - } + waifu.Owner = user } return waifus, nil } @@ -69,8 +68,8 @@ func (rep *WaifuRepository) GetCountByUserId(userId int) (int64, error) { return count, err } -func (rep *WaifuRepository) GetFree() ([]*Waifu, error) { - waifus, err := sqlx.List[*Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL;") +func (rep *WaifuRepository) GetFree() ([]Waifu, error) { + waifus, err := sqlx.List[Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL;") return waifus, err } @@ -80,16 +79,16 @@ func (rep *WaifuRepository) GetFreeCount() (int64, error) { return count, err } -func (rep *WaifuRepository) GetFreeByRarity(rarity int) ([]*Waifu, error) { - waifus, err := sqlx.List[*Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL AND rarity=$1;", rarity) +func (rep *WaifuRepository) GetFreeByRarity(rarity int) ([]Waifu, error) { + waifus, err := sqlx.List[Waifu](rep.db, "SELECT * FROM waifus WHERE owner_id IS NULL AND rarity=$1;", rarity) return waifus, err } -func (rep *WaifuRepository) GetById(id int) (*Waifu, error) { - waifu := new(Waifu) - err := rep.db.Get(waifu, "SELECT * FROM waifus WHERE id=$1;", id) +func (rep *WaifuRepository) GetById(id int) (Waifu, error) { + waifu := Waifu{} + err := rep.db.Get(&waifu, "SELECT * FROM waifus WHERE id=$1;", id) if err != nil { - return nil, err + return waifu, err } if !waifu.OwnerID.Valid { return waifu, err diff --git a/database/psql/works.go b/database/psql/works.go index 4ee9295..d8dc65c 100644 --- a/database/psql/works.go +++ b/database/psql/works.go @@ -1,8 +1,6 @@ package psql import ( - "kurumibot/database" - "git.nix13.pw/scuroneko/laniakea" "github.com/shopspring/decimal" "github.com/vinovest/sqlx" @@ -21,18 +19,21 @@ type WorkRepository struct { db *sqlx.DB } -func NewWorkRepository(db *laniakea.DatabaseContext) *WorkRepository { - return &WorkRepository{db: db.PostgresSQL} +func newWorkRepository(db *sqlx.DB) WorkRepository { + return WorkRepository{db} +} +func NewWorkRepository(db *laniakea.DatabaseContext) WorkRepository { + return newWorkRepository(db.PostgresSQL) } -func (rep *WorkRepository) GetById(id int) (Work, error) { +func (rep WorkRepository) GetById(id int) (Work, error) { work := Work{} - err := database.PostgresDatabase.Get(&work, "SELECT * FROM works WHERE id = $1;", id) + err := rep.db.Get(&work, "SELECT * FROM works WHERE id = $1;", id) return work, err } -func (rep *WorkRepository) GetAll() ([]Work, error) { +func (rep WorkRepository) GetAll() ([]Work, error) { works := make([]Work, 0) - err := database.PostgresDatabase.Select(&works, "SELECT * FROM works;") + err := rep.db.Select(&works, "SELECT * FROM works;") return works, err } diff --git a/database/red/ai_chats.go b/database/red/ai_chats.go index ab248cd..8cf9018 100644 --- a/database/red/ai_chats.go +++ b/database/red/ai_chats.go @@ -12,19 +12,19 @@ type AiRepository struct { client *redis.Client } -func NewAiRepository(db *laniakea.DatabaseContext) *AiRepository { - return &AiRepository{client: db.Redis} +func NewAiRepository(db *laniakea.DatabaseContext) AiRepository { + return AiRepository{client: db.Redis} } -func (rep *AiRepository) SetChatId(userId int, chatId string) error { +func (rep AiRepository) SetChatId(userId int, chatId string) error { key := fmt.Sprintf("ai.chats.gpt.%d", userId) return rep.client.Set(ctx, key, chatId, 0).Err() } -func (rep *AiRepository) GetChatId(userId int) (string, error) { +func (rep AiRepository) GetChatId(userId int) (string, error) { key := fmt.Sprintf("ai.chats.gpt.%d", userId) return rep.client.Get(ctx, key).Result() } -func (rep *AiRepository) GetOrCreateChatId(userId int) (string, error) { +func (rep AiRepository) GetOrCreateChatId(userId int) (string, error) { key := fmt.Sprintf("ai.chats.gpt.%d", userId) res := rep.client.Get(ctx, key) if res.Err() != nil { diff --git a/database/red/rp_chats.go b/database/red/rp_chats.go index f0c2233..0fb64e9 100644 --- a/database/red/rp_chats.go +++ b/database/red/rp_chats.go @@ -17,15 +17,15 @@ type RPRepository struct { client *redis.Client } -func NewRPRepository(db *laniakea.DatabaseContext) *RPRepository { - return &RPRepository{client: db.Redis} +func NewRPRepository(db *laniakea.DatabaseContext) RPRepository { + return RPRepository{client: db.Redis} } -func (rep *RPRepository) SetSelectedWaifu(userId, waifuId int) error { +func (rep RPRepository) SetSelectedWaifu(userId, waifuId int) error { key := fmt.Sprintf("ai.chats.rp.%d", userId) return rep.client.Set(ctx, key, waifuId, 0).Err() } -func (rep *RPRepository) GetSelectedWaifu(userId int) int { +func (rep RPRepository) GetSelectedWaifu(userId int) int { key := fmt.Sprintf("ai.chats.rp.%d", userId) res := rep.client.Get(ctx, key) if res.Err() != nil { @@ -35,11 +35,11 @@ func (rep *RPRepository) GetSelectedWaifu(userId int) int { return i } -func (rep *RPRepository) SetChatId(userId, waifuId int, chatId string) error { +func (rep RPRepository) SetChatId(userId, waifuId int, chatId string) error { key := fmt.Sprintf("ai.chats.rp.%d.%d.chat", userId, waifuId) return rep.client.Set(context.Background(), key, chatId, 0).Err() } -func (rep *RPRepository) GetChatId(userId, waifuId int) string { +func (rep RPRepository) GetChatId(userId, waifuId int) string { key := fmt.Sprintf("ai.chats.rp.%d.%d.chat", userId, waifuId) res := rep.client.Get(ctx, key) if res.Err() != nil { @@ -47,7 +47,7 @@ func (rep *RPRepository) GetChatId(userId, waifuId int) string { } return res.Val() } -func (rep *RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error) { +func (rep RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error) { chatId := rep.GetChatId(userId, waifuId) if chatId == "" { chatId = uuid.New().String() @@ -56,11 +56,11 @@ func (rep *RPRepository) GetOrCreateChatId(userId, waifuId int) (string, error) return chatId, err } -func (rep *RPRepository) SetChatPrompt(userId, waifuId int, prompt string) error { +func (rep RPRepository) SetChatPrompt(userId, waifuId int, prompt string) error { key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId) return rep.client.Set(context.Background(), key, prompt, 0).Err() } -func (rep *RPRepository) GetChatPrompt(userId, waifuId int) string { +func (rep RPRepository) GetChatPrompt(userId, waifuId int) string { key := fmt.Sprintf("ai.chats.rp.%d.%d.prompt", userId, waifuId) res := rep.client.Get(ctx, key) if res.Err() != nil { @@ -69,11 +69,11 @@ func (rep *RPRepository) GetChatPrompt(userId, waifuId int) string { return res.Val() } -func (rep *RPRepository) SetCounter(userId, waifuId, counter int) error { +func (rep RPRepository) SetCounter(userId, waifuId, counter int) error { key := fmt.Sprintf("ai.chats.rp.%d.%d.counter", userId, waifuId) return rep.client.Set(ctx, key, counter, 0).Err() } -func (rep *RPRepository) GetCounter(userId, waifuId int) int { +func (rep RPRepository) GetCounter(userId, waifuId int) int { key := fmt.Sprintf("ai.chats.rp.%d.%d.counter", userId, waifuId) res := rep.client.Get(ctx, key) if res.Err() != nil { @@ -83,11 +83,11 @@ func (rep *RPRepository) GetCounter(userId, waifuId int) int { return i } -func (rep *RPRepository) SetChatTokens(userId, waifuId, tokens int) error { +func (rep RPRepository) SetChatTokens(userId, waifuId, tokens int) error { key := fmt.Sprintf("ai.chats.rp.%d.%d.tokens", userId, waifuId) return rep.client.Set(ctx, key, tokens, 0).Err() } -func (rep *RPRepository) GetChatTokens(userId, waifuId int) int { +func (rep RPRepository) GetChatTokens(userId, waifuId int) int { key := fmt.Sprintf("ai.chats.rp.%d.%d.tokens", userId, waifuId) res := rep.client.Get(ctx, key) if res.Err() != nil { @@ -97,11 +97,11 @@ func (rep *RPRepository) GetChatTokens(userId, waifuId int) int { return i } -func (rep *RPRepository) SetChatSettingID(userId, waifuId, settingId int) error { +func (rep RPRepository) SetChatSettingID(userId, waifuId, settingId int) error { key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId) return rep.client.Set(ctx, key, settingId, 0).Err() } -func (rep *RPRepository) GetChatSettingID(userId, waifuId int) int { +func (rep RPRepository) GetChatSettingID(userId, waifuId int) int { key := fmt.Sprintf("ai.chats.rp.%d.%d.setting_id", userId, waifuId) res := rep.client.Get(ctx, key) if res.Err() != nil { @@ -111,11 +111,11 @@ func (rep *RPRepository) GetChatSettingID(userId, waifuId int) int { return i } -func (rep *RPRepository) SetChatScenariosIDs(userId, waifuId int, scenarioIds string) error { +func (rep RPRepository) SetChatScenariosIDs(userId, waifuId int, scenarioIds string) error { key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId) return rep.client.Set(ctx, key, scenarioIds, 0).Err() } -func (rep *RPRepository) GetChatScenariosIDs(userId, waifuId int) []int { +func (rep RPRepository) GetChatScenariosIDs(userId, waifuId int) []int { key := fmt.Sprintf("ai.chats.rp.%d.%d.scenario_id", userId, waifuId) res := rep.client.Get(ctx, key) if res.Err() != nil { diff --git a/plugins/economy.go b/plugins/economy.go index 171225f..5baa91d 100644 --- a/plugins/economy.go +++ b/plugins/economy.go @@ -100,7 +100,7 @@ func passiveIncome(b *laniakea.Bot) error { user.BtcIncome = user.BtcIncome.Add(btcIncome) user.IncomeTime = time.Now() - _, err = userRep.Update(user) + _, err = userRep.Update(&user) if err != nil { b.Logger().Error(err) continue diff --git a/plugins/logs.go b/plugins/logs.go index 4c1a625..8f0c467 100644 --- a/plugins/logs.go +++ b/plugins/logs.go @@ -88,7 +88,7 @@ func (w *DatabaseWriter) Print(level slog.LogLevel, prefix string, traceback []* if messages[len(messages)-1] == "\n" { messages = messages[:len(messages)-1] } - entry := &mdb.ConsoleLogEntry{ + entry := mdb.ConsoleLogEntry{ Level: level.GetName(), Prefix: prefix, Traceback: slog.FormatFullTraceback(traceback), diff --git a/plugins/rp.go b/plugins/rp.go index 79637d0..2cd7f73 100644 --- a/plugins/rp.go +++ b/plugins/rp.go @@ -73,7 +73,7 @@ func rpInfo(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) { return } - var waifu *psql.Waifu + var waifu psql.Waifu waifuId := rpRepRed.GetSelectedWaifu(ctx.FromID) if waifuId == 0 { waifus, err := waifuRep.GetByUserId(ctx.FromID) @@ -130,7 +130,7 @@ func rpInfo(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) { func rpWaifuList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) { waifuRep := psql.NewWaifuRepository(db) - waifus := make(extypes.Slice[*psql.Waifu], 0) + waifus := make(extypes.Slice[psql.Waifu], 0) var err error userRep := psql.NewUserRepository(db) @@ -148,7 +148,7 @@ func rpWaifuList(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) { ctx.Error(err) return } - waifus = waifus.Filter(func(w *psql.Waifu) bool { + waifus = waifus.Filter(func(w psql.Waifu) bool { return len(w.RpPrompt) > 0 }) diff --git a/plugins/waifus.go b/plugins/waifus.go index 9e14317..a9aeb37 100644 --- a/plugins/waifus.go +++ b/plugins/waifus.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "git.nix13.pw/scuroneko/extypes" "git.nix13.pw/scuroneko/laniakea" ) @@ -224,7 +225,7 @@ func waifuSearch(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) { return } - var freeWaifus []*psql.Waifu + var freeWaifus extypes.Slice[psql.Waifu] rarity := 3 if rand == 0 { rarity = 5