Skip to content

Commit bc11766

Browse files
committed
fix(eventsub): add categoryId update on channel update
1 parent 43e5f11 commit bc11766

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

apps/eventsub/internal/handler/channel_update.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (c *Handler) handleChannelUpdate(
5252
map[string]any{
5353
"title": event.Title,
5454
"gameName": event.CategoryName,
55+
"gameId": event.CategoryID,
5556
},
5657
).Error
5758
if err != nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package model
2+
3+
import (
4+
"time"
5+
)
6+
7+
type Stream struct {
8+
ID string
9+
UserId string
10+
UserLogin string
11+
UserName string
12+
GameId string
13+
GameName string
14+
CommunityIds []string
15+
Type string
16+
Title string
17+
ViewerCount int
18+
StartedAt time.Time
19+
Language string
20+
ThumbnailUrl string
21+
TagIds []string
22+
Tags []string
23+
IsMature bool
24+
}
25+
26+
var Nil = Stream{}

libs/repositories/streams/streams.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package streams
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/twirapp/twir/libs/repositories/streams/model"
8+
)
9+
10+
type Repository interface {
11+
GetList(ctx context.Context) ([]model.Stream, error)
12+
Update(ctx context.Context, channelID string, input UpdateInput) error
13+
}
14+
15+
type UpdateInput struct {
16+
GameId *string
17+
GameName *string
18+
CommunityIds []string
19+
Type *string
20+
Title *string
21+
ViewerCount *int
22+
StartedAt *time.Time
23+
Language *string
24+
ThumbnailUrl *string
25+
TagIds []string
26+
Tags []string
27+
IsMature *bool
28+
}

0 commit comments

Comments
 (0)