common/repository/tg/tg.go

84 lines
1.8 KiB
Go
Raw Normal View History

2024-06-27 19:30:57 +00:00
package tg
import (
"context"
"database/sql"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen"
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
)
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,
2024-06-27 19:44:40 +00:00
Password: data.Password,
2024-06-27 19:30:57 +00:00
})
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 {
return nil, err
}
var result []model.TgAccount
for _, row := range rows {
2024-07-01 09:12:50 +00:00
var status model.TgAccountStatus
s := string(row.Status.([]byte))
status = model.TgAccountStatus(s)
2024-06-27 19:30:57 +00:00
result = append(result, model.TgAccount{
ID: row.ID,
ApiID: row.Apiid,
ApiHash: row.Apihash,
PhoneNumber: row.Phonenumber,
2024-07-01 09:12:50 +00:00
Status: status,
2024-06-27 19:30:57 +00:00
Deleted: row.Deleted,
CreatedAt: row.Createdat,
2024-06-27 19:44:40 +00:00
Password: row.Password,
2024-06-27 19:30:57 +00:00
})
}
return result, nil
}
2024-07-01 12:35:46 +00:00
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
}