database changes
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user