package tg import ( "context" "database/sql" "gitea.pena/SQuiz/common/dal/sqlcgen" "gitea.pena/SQuiz/common/model" "gitea.pena/SQuiz/common/pj_errors" ) type TgRepo struct { queries *sqlcgen.Queries pool *sql.DB } type Deps struct { Queries *sqlcgen.Queries Pool *sql.DB } func NewTgRepo(deps Deps) *TgRepo { return &TgRepo{ queries: deps.Queries, pool: deps.Pool, } } func (r *TgRepo) CreateTgAccount(ctx context.Context, data model.TgAccount) (int64, error) { id, err := r.queries.CreateTgAccount(ctx, sqlcgen.CreateTgAccountParams{ Apiid: data.ApiID, Apihash: data.ApiHash, Phonenumber: data.PhoneNumber, Status: data.Status, Password: data.Password, }) if err != nil { return 0, err } return id, err } func (r *TgRepo) GetAllTgAccounts(ctx context.Context) ([]model.TgAccount, error) { rows, err := r.queries.GetAllTgAccounts(ctx) if err != nil { if err == sql.ErrNoRows { return nil, pj_errors.ErrNotFound } return nil, err } var result []model.TgAccount for _, row := range rows { var status model.TgAccountStatus s := string(row.Status.([]byte)) status = model.TgAccountStatus(s) result = append(result, model.TgAccount{ ID: row.ID, ApiID: row.Apiid, ApiHash: row.Apihash, PhoneNumber: row.Phonenumber, Status: status, Deleted: row.Deleted, CreatedAt: row.Createdat, Password: row.Password, }) } return result, nil } func (r *TgRepo) UpdateStatusTg(ctx context.Context, id int64, status model.TgAccountStatus) error { err := r.queries.UpdateStatusTg(ctx, sqlcgen.UpdateStatusTgParams{ Status: status, ID: id, }) if err != nil { return err } return nil } func (r *TgRepo) SoftDeleteTgAccount(ctx context.Context, id int64) error { err := r.queries.SoftDeleteTgAccount(ctx, id) if err != nil { return err } return nil } func (r *TgRepo) SearchIDByAppIDanAppHash(ctx context.Context, appID int32, appHash string) (*model.TgAccount, error) { row, err := r.queries.SearchIDByAppIDanAppHash(ctx, sqlcgen.SearchIDByAppIDanAppHashParams{ Apiid: appID, Apihash: appHash, }) if err != nil { return nil, err } var status model.TgAccountStatus s := string(row.Status.([]byte)) status = model.TgAccountStatus(s) return &model.TgAccount{ ID: row.ID, ApiID: row.Apiid, ApiHash: row.Apihash, PhoneNumber: row.Phonenumber, Status: status, Deleted: row.Deleted, CreatedAt: row.Createdat, Password: row.Password, }, nil }