|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/Masterminds/squirrel" |
| 7 | + trmpgx "github.com/avito-tech/go-transaction-manager/drivers/pgxv5/v2" |
| 8 | + "github.com/jackc/pgx/v5" |
| 9 | + "github.com/jackc/pgx/v5/pgxpool" |
| 10 | + "github.com/twirapp/twir/libs/repositories" |
| 11 | + "github.com/twirapp/twir/libs/repositories/streams" |
| 12 | + "github.com/twirapp/twir/libs/repositories/streams/model" |
| 13 | +) |
| 14 | + |
| 15 | +type Opts struct { |
| 16 | + PgxPool *pgxpool.Pool |
| 17 | +} |
| 18 | + |
| 19 | +func New(opts Opts) *Pgx { |
| 20 | + return &Pgx{ |
| 21 | + pool: opts.PgxPool, |
| 22 | + getter: trmpgx.DefaultCtxGetter, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func NewFx(pool *pgxpool.Pool) *Pgx { |
| 27 | + return New(Opts{PgxPool: pool}) |
| 28 | +} |
| 29 | + |
| 30 | +var _ streams.Repository = (*Pgx)(nil) |
| 31 | +var sq = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar) |
| 32 | + |
| 33 | +type Pgx struct { |
| 34 | + pool *pgxpool.Pool |
| 35 | + getter *trmpgx.CtxGetter |
| 36 | +} |
| 37 | + |
| 38 | +func (c *Pgx) GetList(ctx context.Context) ([]model.Stream, error) { |
| 39 | + query := ` |
| 40 | +SELECT id, "userId", "userLogin", "userName", "gameId", "gameName", "communityIds", type, title, "viewerCount", "startedAt", "language", "thumbnailUrl", "tagIds", tags, "isMature" |
| 41 | +FROM channels_streams |
| 42 | +` |
| 43 | + |
| 44 | + conn := c.getter.DefaultTrOrDB(ctx, c.pool) |
| 45 | + rows, err := conn.Query(ctx, query) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + result, err := pgx.CollectRows(rows, pgx.RowToStructByName[model.Stream]) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + return result, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (c *Pgx) Update(ctx context.Context, channelID string, input streams.UpdateInput) error { |
| 59 | + updateBuilder := sq.Update("channels_streams"). |
| 60 | + Where(squirrel.Eq{`"userId"`: channelID}) |
| 61 | + |
| 62 | + updateBuilder = repositories.SquirrelApplyPatch( |
| 63 | + updateBuilder, map[string]interface{}{ |
| 64 | + `"gameId"`: input.GameId, |
| 65 | + `"gameName"`: input.GameName, |
| 66 | + `"communityIds"`: input.CommunityIds, |
| 67 | + `"type"`: input.Type, |
| 68 | + `"title"`: input.Title, |
| 69 | + `"viewerCount"`: input.ViewerCount, |
| 70 | + `"startedAt"`: input.StartedAt, |
| 71 | + `"language"`: input.Language, |
| 72 | + `"thumbnailUrl"`: input.ThumbnailUrl, |
| 73 | + `"tagIds"`: input.TagIds, |
| 74 | + `"tags"`: input.Tags, |
| 75 | + `"isMature"`: input.IsMature, |
| 76 | + }, |
| 77 | + ) |
| 78 | + |
| 79 | + query, args, err := updateBuilder.ToSql() |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + conn := c.getter.DefaultTrOrDB(ctx, c.pool) |
| 85 | + _, err = conn.Exec(ctx, query, args...) |
| 86 | + return err |
| 87 | +} |
0 commit comments