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